blob: 1f2cde0d025f407bbb089f1aa923ed2df07a6fd5 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/link.c: TIPC link code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloye74a3862016-03-03 14:23:21 -05004 * Copyright (c) 1996-2007, 2012-2016, Ericsson AB
Ying Xue198d73b2013-06-17 10:54:42 -04005 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -040038#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "link.h"
Richard Alpe7be57fc2014-11-20 10:29:12 +010040#include "bcast.h"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040041#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#include "discover.h"
Richard Alpe0655f6a2014-11-20 10:29:07 +010044#include "netlink.h"
Jon Paul Maloy35c55c92016-06-13 20:46:22 -040045#include "monitor.h"
Tuong Lienb4b97712018-12-19 09:17:56 +070046#include "trace.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
Ying Xue796c75d2013-06-17 10:54:48 -040048#include <linux/pkt_sched.h>
49
Jon Paul Maloy38206d52015-11-19 14:30:46 -050050struct tipc_stats {
Jon Paul Maloy95901122016-11-25 10:35:02 -050051 u32 sent_pkts;
52 u32 recv_pkts;
Jon Paul Maloy38206d52015-11-19 14:30:46 -050053 u32 sent_states;
54 u32 recv_states;
55 u32 sent_probes;
56 u32 recv_probes;
57 u32 sent_nacks;
58 u32 recv_nacks;
59 u32 sent_acks;
60 u32 sent_bundled;
61 u32 sent_bundles;
62 u32 recv_bundled;
63 u32 recv_bundles;
64 u32 retransmitted;
65 u32 sent_fragmented;
66 u32 sent_fragments;
67 u32 recv_fragmented;
68 u32 recv_fragments;
69 u32 link_congs; /* # port sends blocked by congestion */
70 u32 deferred_recv;
71 u32 duplicates;
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 */
78};
79
80/**
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
85 * @timer: link timer
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]
Jon Paul Maloy38206d52015-11-19 14:30:46 -050092 * @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')
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400100 * @mon_state: cookie with information needed by link monitor
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500101 * @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
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -0500109 * @prev_from: sequence number of most previous retransmission request
Jon Maloya4dc70d2018-07-06 15:22:36 +0200110 * @stale_cnt: counter for number of identical retransmit attempts
111 * @stale_limit: time when repeated identical retransmits must force link reset
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500112 * @ackers: # of peers that needs to ack each packet before it can be released
113 * @acked: # last packet acked by a certain peer. Used for broadcast.
114 * @rcv_nxt: next sequence number to expect for inbound messages
115 * @deferred_queue: deferred queue saved OOS b'cast message received from node
116 * @unacked_window: # of inbound messages rx'd without ack'ing back to peer
117 * @inputq: buffer queue for messages to be delivered upwards
118 * @namedq: buffer queue for name table messages to be delivered upwards
119 * @next_out: ptr to first unsent outbound message in queue
120 * @wakeupq: linked list of wakeup msgs waiting for link congestion to abate
121 * @long_msg_seq_no: next identifier to use for outbound fragmented messages
122 * @reasm_buf: head of partially reassembled inbound message fragments
123 * @bc_rcvr: marks that this is a broadcast receiver link
124 * @stats: collects statistics regarding link activity
125 */
126struct tipc_link {
127 u32 addr;
128 char name[TIPC_MAX_LINK_NAME];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500129 struct net *net;
130
131 /* Management and link supervision data */
Jon Maloy7ea817f2018-07-10 01:07:36 +0200132 u16 peer_session;
133 u16 session;
Jon Maloy9012de52018-07-10 01:07:35 +0200134 u16 snd_nxt_state;
135 u16 rcv_nxt_state;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500136 u32 peer_bearer_id;
137 u32 bearer_id;
138 u32 tolerance;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500139 u32 abort_limit;
140 u32 state;
141 u16 peer_caps;
Jon Maloy7ea817f2018-07-10 01:07:36 +0200142 bool in_session;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500143 bool active;
144 u32 silent_intv_cnt;
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500145 char if_name[TIPC_MAX_IF_NAME];
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500146 u32 priority;
147 char net_plane;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400148 struct tipc_mon_state mon_state;
Jon Paul Maloy88e8ac72016-04-15 13:33:04 -0400149 u16 rst_cnt;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500150
151 /* Failover/synch */
152 u16 drop_point;
153 struct sk_buff *failover_reasm_skb;
154
155 /* Max packet negotiation */
156 u16 mtu;
157 u16 advertised_mtu;
158
159 /* Sending */
160 struct sk_buff_head transmq;
161 struct sk_buff_head backlogq;
162 struct {
163 u16 len;
164 u16 limit;
165 } backlog[5];
166 u16 snd_nxt;
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -0500167 u16 prev_from;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500168 u16 window;
Jon Maloya4dc70d2018-07-06 15:22:36 +0200169 u16 stale_cnt;
170 unsigned long stale_limit;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500171
172 /* Reception */
173 u16 rcv_nxt;
174 u32 rcv_unacked;
175 struct sk_buff_head deferdq;
176 struct sk_buff_head *inputq;
177 struct sk_buff_head *namedq;
178
179 /* Congestion handling */
180 struct sk_buff_head wakeupq;
181
182 /* Fragmentation/reassembly */
183 struct sk_buff *reasm_buf;
184
185 /* Broadcast */
186 u16 ackers;
187 u16 acked;
188 struct tipc_link *bc_rcvlink;
189 struct tipc_link *bc_sndlink;
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -0400190 u8 nack_state;
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500191 bool bc_peer_is_up;
192
193 /* Statistics */
194 struct tipc_stats stats;
195};
196
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400197/*
198 * Error message prefixes
199 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400200static const char *link_co_err = "Link tunneling error, ";
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400201static const char *link_rst_msg = "Resetting link ";
Richard Alpe7be57fc2014-11-20 10:29:12 +0100202
Jon Paul Maloy52666982015-10-22 08:51:41 -0400203/* Send states for broadcast NACKs
204 */
205enum {
206 BC_NACK_SND_CONDITIONAL,
207 BC_NACK_SND_UNCONDITIONAL,
208 BC_NACK_SND_SUPPRESS,
209};
210
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -0500211#define TIPC_BC_RETR_LIM msecs_to_jiffies(10) /* [ms] */
Tuong Lien382f5982019-04-04 11:09:52 +0700212#define TIPC_UC_RETR_TIME (jiffies + msecs_to_jiffies(1))
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -0400213
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900214/*
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400215 * Interval between NACKs when packets arrive out of order
216 */
217#define TIPC_NACK_INTV (TIPC_MIN_LINK_WIN * 2)
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500218
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400219/* Link FSM states:
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400220 */
221enum {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400222 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
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400229};
230
231/* Link FSM state checking routines
232 */
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400233static int link_is_up(struct tipc_link *l)
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400234{
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400235 return l->state & (LINK_ESTABLISHED | LINK_SYNCHING);
Jon Paul Maloyd3504c32015-07-16 16:54:25 -0400236}
237
Jon Paul Maloyd9992972015-07-16 16:54:31 -0400238static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
239 struct sk_buff_head *xmitq);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -0400240static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100241 bool probe_reply, u16 rcvgap,
242 int tolerance, int priority,
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -0400243 struct sk_buff_head *xmitq);
Jon Paul Maloy1a906322015-11-19 14:30:47 -0500244static void link_print(struct tipc_link *l, const char *str);
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -0400245static int tipc_link_build_nack_msg(struct tipc_link *l,
246 struct sk_buff_head *xmitq);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400247static void tipc_link_build_bc_init_msg(struct tipc_link *l,
248 struct sk_buff_head *xmitq);
249static bool tipc_link_release_pkts(struct tipc_link *l, u16 to);
Tuong Lien91959482019-04-04 11:09:51 +0700250static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data);
251static void 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);
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -0400254
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800256 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 */
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400258bool tipc_link_is_up(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259{
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400260 return link_is_up(l);
261}
262
Jon Paul Maloyc8199302015-10-15 14:52:46 -0400263bool tipc_link_peer_is_down(struct tipc_link *l)
264{
265 return l->state == LINK_PEER_RESET;
266}
267
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400268bool tipc_link_is_reset(struct tipc_link *l)
269{
270 return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
271}
272
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400273bool tipc_link_is_establishing(struct tipc_link *l)
274{
275 return l->state == LINK_ESTABLISHING;
276}
277
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400278bool tipc_link_is_synching(struct tipc_link *l)
279{
280 return l->state == LINK_SYNCHING;
281}
282
283bool tipc_link_is_failingover(struct tipc_link *l)
284{
285 return l->state == LINK_FAILINGOVER;
286}
287
288bool tipc_link_is_blocked(struct tipc_link *l)
289{
290 return l->state & (LINK_RESETTING | LINK_PEER_RESET | LINK_FAILINGOVER);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291}
292
Wu Fengguang742e0382015-10-24 22:56:01 +0800293static bool link_is_bc_sndlink(struct tipc_link *l)
Jon Paul Maloy52666982015-10-22 08:51:41 -0400294{
295 return !l->bc_sndlink;
296}
297
Wu Fengguang742e0382015-10-24 22:56:01 +0800298static bool link_is_bc_rcvlink(struct tipc_link *l)
Jon Paul Maloy52666982015-10-22 08:51:41 -0400299{
300 return ((l->bc_rcvlink == l) && !link_is_bc_sndlink(l));
301}
302
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400303void tipc_link_set_active(struct tipc_link *l, bool active)
304{
305 l->active = active;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306}
307
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500308u32 tipc_link_id(struct tipc_link *l)
309{
310 return l->peer_bearer_id << 16 | l->bearer_id;
311}
312
313int tipc_link_window(struct tipc_link *l)
314{
315 return l->window;
316}
317
318int tipc_link_prio(struct tipc_link *l)
319{
320 return l->priority;
321}
322
323unsigned long tipc_link_tolerance(struct tipc_link *l)
324{
325 return l->tolerance;
326}
327
328struct sk_buff_head *tipc_link_inputq(struct tipc_link *l)
329{
330 return l->inputq;
331}
332
333char tipc_link_plane(struct tipc_link *l)
334{
335 return l->net_plane;
336}
337
Jon Maloy9012de52018-07-10 01:07:35 +0200338void tipc_link_update_caps(struct tipc_link *l, u16 capabilities)
339{
340 l->peer_caps = capabilities;
341}
342
Jon Paul Maloy52666982015-10-22 08:51:41 -0400343void tipc_link_add_bc_peer(struct tipc_link *snd_l,
344 struct tipc_link *uc_l,
345 struct sk_buff_head *xmitq)
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400346{
Jon Paul Maloy52666982015-10-22 08:51:41 -0400347 struct tipc_link *rcv_l = uc_l->bc_rcvlink;
348
349 snd_l->ackers++;
350 rcv_l->acked = snd_l->snd_nxt - 1;
Jon Paul Maloy9a650832015-11-19 14:12:50 -0500351 snd_l->state = LINK_ESTABLISHED;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400352 tipc_link_build_bc_init_msg(uc_l, xmitq);
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400353}
354
Jon Paul Maloy52666982015-10-22 08:51:41 -0400355void tipc_link_remove_bc_peer(struct tipc_link *snd_l,
356 struct tipc_link *rcv_l,
357 struct sk_buff_head *xmitq)
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400358{
Jon Paul Maloy52666982015-10-22 08:51:41 -0400359 u16 ack = snd_l->snd_nxt - 1;
360
361 snd_l->ackers--;
Jon Paul Maloya71eb722016-07-11 16:08:36 -0400362 rcv_l->bc_peer_is_up = true;
363 rcv_l->state = LINK_ESTABLISHED;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400364 tipc_link_bc_ack_rcv(rcv_l, ack, xmitq);
Tuong Lien26574db2018-12-19 09:17:57 +0700365 trace_tipc_link_reset(rcv_l, TIPC_DUMP_ALL, "bclink removed!");
Jon Paul Maloy52666982015-10-22 08:51:41 -0400366 tipc_link_reset(rcv_l);
367 rcv_l->state = LINK_RESET;
368 if (!snd_l->ackers) {
Tuong Lien26574db2018-12-19 09:17:57 +0700369 trace_tipc_link_reset(snd_l, TIPC_DUMP_ALL, "zero ackers!");
Jon Paul Maloy52666982015-10-22 08:51:41 -0400370 tipc_link_reset(snd_l);
Jon Paul Maloy9a650832015-11-19 14:12:50 -0500371 snd_l->state = LINK_RESET;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400372 __skb_queue_purge(xmitq);
373 }
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400374}
375
376int tipc_link_bc_peers(struct tipc_link *l)
377{
378 return l->ackers;
379}
380
YueHaibinge064cce2018-07-19 17:16:59 +0800381static u16 link_bc_rcv_gap(struct tipc_link *l)
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -0400382{
383 struct sk_buff *skb = skb_peek(&l->deferdq);
384 u16 gap = 0;
385
386 if (more(l->snd_nxt, l->rcv_nxt))
387 gap = l->snd_nxt - l->rcv_nxt;
388 if (skb)
389 gap = buf_seqno(skb) - l->rcv_nxt;
390 return gap;
391}
392
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400393void tipc_link_set_mtu(struct tipc_link *l, int mtu)
394{
395 l->mtu = mtu;
396}
397
398int tipc_link_mtu(struct tipc_link *l)
399{
400 return l->mtu;
401}
402
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500403u16 tipc_link_rcv_nxt(struct tipc_link *l)
404{
405 return l->rcv_nxt;
406}
407
408u16 tipc_link_acked(struct tipc_link *l)
409{
410 return l->acked;
411}
412
413char *tipc_link_name(struct tipc_link *l)
414{
415 return l->name;
416}
417
LUU Duc Canhc140eb12018-09-26 21:00:54 +0200418u32 tipc_link_state(struct tipc_link *l)
419{
420 return l->state;
421}
422
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423/**
Per Liden4323add2006-01-18 00:38:21 +0100424 * tipc_link_create - create a new link
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400425 * @n: pointer to associated node
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400426 * @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
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400434 * @ownnode: identity of own node
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400435 * @peer: node id of peer node
Jon Paul Maloyfd556f22015-10-22 08:51:40 -0400436 * @peer_caps: bitmap describing peer node capabilities
Jon Paul Maloy52666982015-10-22 08:51:41 -0400437 * @bc_sndlink: the namespace global link used for broadcast sending
438 * @bc_rcvlink: the peer specific link used for broadcast reception
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400439 * @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
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900442 *
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400443 * Returns true if link was created, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 */
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400445bool tipc_link_create(struct net *net, char *if_name, int bearer_id,
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400446 int tolerance, char net_plane, u32 mtu, int priority,
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100447 int window, u32 session, u32 self,
448 u32 peer, u8 *peer_id, u16 peer_caps,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400449 struct tipc_link *bc_sndlink,
450 struct tipc_link *bc_rcvlink,
451 struct sk_buff_head *inputq,
452 struct sk_buff_head *namedq,
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400453 struct tipc_link **link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454{
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100455 char peer_str[NODE_ID_STR_LEN] = {0,};
456 char self_str[NODE_ID_STR_LEN] = {0,};
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400457 struct tipc_link *l;
Allan Stephens37b9c082011-02-28 11:32:27 -0500458
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400459 l = kzalloc(sizeof(*l), GFP_ATOMIC);
460 if (!l)
461 return false;
462 *link = l;
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500463 l->session = session;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400464
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100465 /* Set link name for unicast links only */
466 if (peer_id) {
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);
473 }
474 /* Peer i/f name will be completed by reset/activate message */
Jon Maloy7494cfa2018-03-29 23:20:45 +0200475 snprintf(l->name, sizeof(l->name), "%s:%s-%s:unknown",
476 self_str, if_name, peer_str);
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100477
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500478 strcpy(l->if_name, if_name);
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400479 l->addr = peer;
Jon Paul Maloyfd556f22015-10-22 08:51:40 -0400480 l->peer_caps = peer_caps;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400481 l->net = net;
Jon Maloy7ea817f2018-07-10 01:07:36 +0200482 l->in_session = false;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400483 l->bearer_id = bearer_id;
484 l->tolerance = tolerance;
Jon Maloy047491e2018-10-10 17:34:01 +0200485 if (bc_rcvlink)
486 bc_rcvlink->tolerance = tolerance;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400487 l->net_plane = net_plane;
488 l->advertised_mtu = mtu;
489 l->mtu = mtu;
490 l->priority = priority;
491 tipc_link_set_queue_limits(l, window);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -0400492 l->ackers = 1;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400493 l->bc_sndlink = bc_sndlink;
494 l->bc_rcvlink = bc_rcvlink;
Jon Paul Maloy0e054982015-10-22 08:51:36 -0400495 l->inputq = inputq;
496 l->namedq = namedq;
497 l->state = LINK_RESETTING;
Jon Paul Maloy440d8962015-07-30 18:24:26 -0400498 __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->wakeupq);
502 skb_queue_head_init(l->inputq);
503 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504}
505
Jon Paul Maloy32301902015-10-22 08:51:37 -0400506/**
507 * tipc_link_bc_create - create new link to be used for broadcast
508 * @n: pointer to associated node
Jon Maloy4c94cc22017-11-30 16:47:25 +0100509 * @mtu: mtu to be used initially if no peers
Jon Paul Maloy32301902015-10-22 08:51:37 -0400510 * @window: send window to be used
511 * @inputq: queue to put messages ready for delivery
512 * @namedq: queue to put binding table update messages ready for delivery
513 * @link: return value, pointer to put the created link
514 *
515 * Returns true if link was created, otherwise false
516 */
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400517bool tipc_link_bc_create(struct net *net, u32 ownnode, u32 peer,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400518 int mtu, int window, u16 peer_caps,
Jon Paul Maloy32301902015-10-22 08:51:37 -0400519 struct sk_buff_head *inputq,
520 struct sk_buff_head *namedq,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400521 struct tipc_link *bc_sndlink,
Jon Paul Maloy32301902015-10-22 08:51:37 -0400522 struct tipc_link **link)
523{
524 struct tipc_link *l;
525
Jon Paul Maloyc72fa872015-10-22 08:51:46 -0400526 if (!tipc_link_create(net, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
Jon Maloy25b0b9c2018-03-22 20:42:51 +0100527 0, ownnode, peer, NULL, peer_caps, bc_sndlink,
Jon Paul Maloy52666982015-10-22 08:51:41 -0400528 NULL, inputq, namedq, link))
Jon Paul Maloy32301902015-10-22 08:51:37 -0400529 return false;
530
531 l = *link;
532 strcpy(l->name, tipc_bclink_name);
Tuong Lien26574db2018-12-19 09:17:57 +0700533 trace_tipc_link_reset(l, TIPC_DUMP_ALL, "bclink created!");
Jon Paul Maloy32301902015-10-22 08:51:37 -0400534 tipc_link_reset(l);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400535 l->state = LINK_RESET;
Jon Paul Maloy2f566122015-10-22 08:51:39 -0400536 l->ackers = 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400537 l->bc_rcvlink = l;
538
539 /* Broadcast send link is always up */
540 if (link_is_bc_sndlink(l))
541 l->state = LINK_ESTABLISHED;
542
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500543 /* Disable replicast if even a single peer doesn't support it */
544 if (link_is_bc_rcvlink(l) && !(peer_caps & TIPC_BCAST_RCAST))
545 tipc_bcast_disable_rcast(net);
546
Jon Paul Maloy32301902015-10-22 08:51:37 -0400547 return true;
548}
549
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550/**
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400551 * tipc_link_fsm_evt - link finite state machine
552 * @l: pointer to link
553 * @evt: state machine event to be processed
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400554 */
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400555int tipc_link_fsm_evt(struct tipc_link *l, int evt)
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400556{
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400557 int rc = 0;
Tuong Lien26574db2018-12-19 09:17:57 +0700558 int old_state = l->state;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400559
560 switch (l->state) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400561 case LINK_RESETTING:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400562 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400563 case LINK_PEER_RESET_EVT:
564 l->state = LINK_PEER_RESET;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400565 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400566 case LINK_RESET_EVT:
567 l->state = LINK_RESET;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400568 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400569 case LINK_FAILURE_EVT:
570 case LINK_FAILOVER_BEGIN_EVT:
571 case LINK_ESTABLISH_EVT:
572 case LINK_FAILOVER_END_EVT:
573 case LINK_SYNCH_BEGIN_EVT:
574 case LINK_SYNCH_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400575 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400576 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400577 }
578 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400579 case LINK_RESET:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400580 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400581 case LINK_PEER_RESET_EVT:
582 l->state = LINK_ESTABLISHING;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400583 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400584 case LINK_FAILOVER_BEGIN_EVT:
585 l->state = LINK_FAILINGOVER;
586 case LINK_FAILURE_EVT:
587 case LINK_RESET_EVT:
588 case LINK_ESTABLISH_EVT:
589 case LINK_FAILOVER_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400590 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400591 case LINK_SYNCH_BEGIN_EVT:
592 case LINK_SYNCH_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400593 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400594 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400595 }
596 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400597 case LINK_PEER_RESET:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400598 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400599 case LINK_RESET_EVT:
600 l->state = LINK_ESTABLISHING;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400601 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400602 case LINK_PEER_RESET_EVT:
603 case LINK_ESTABLISH_EVT:
604 case LINK_FAILURE_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400605 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400606 case LINK_SYNCH_BEGIN_EVT:
607 case LINK_SYNCH_END_EVT:
608 case LINK_FAILOVER_BEGIN_EVT:
609 case LINK_FAILOVER_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400610 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400611 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400612 }
613 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400614 case LINK_FAILINGOVER:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400615 switch (evt) {
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400616 case LINK_FAILOVER_END_EVT:
617 l->state = LINK_RESET;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400618 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400619 case LINK_PEER_RESET_EVT:
620 case LINK_RESET_EVT:
621 case LINK_ESTABLISH_EVT:
622 case LINK_FAILURE_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400623 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400624 case LINK_FAILOVER_BEGIN_EVT:
625 case LINK_SYNCH_BEGIN_EVT:
626 case LINK_SYNCH_END_EVT:
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400627 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400628 goto illegal_evt;
629 }
630 break;
631 case LINK_ESTABLISHING:
632 switch (evt) {
633 case LINK_ESTABLISH_EVT:
634 l->state = LINK_ESTABLISHED;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400635 break;
636 case LINK_FAILOVER_BEGIN_EVT:
637 l->state = LINK_FAILINGOVER;
638 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400639 case LINK_RESET_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400640 l->state = LINK_RESET;
641 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400642 case LINK_FAILURE_EVT:
Jon Paul Maloy73f646c2015-10-15 14:52:44 -0400643 case LINK_PEER_RESET_EVT:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400644 case LINK_SYNCH_BEGIN_EVT:
645 case LINK_FAILOVER_END_EVT:
646 break;
647 case LINK_SYNCH_END_EVT:
648 default:
649 goto illegal_evt;
650 }
651 break;
652 case LINK_ESTABLISHED:
653 switch (evt) {
654 case LINK_PEER_RESET_EVT:
655 l->state = LINK_PEER_RESET;
656 rc |= TIPC_LINK_DOWN_EVT;
657 break;
658 case LINK_FAILURE_EVT:
659 l->state = LINK_RESETTING;
660 rc |= TIPC_LINK_DOWN_EVT;
661 break;
662 case LINK_RESET_EVT:
663 l->state = LINK_RESET;
664 break;
665 case LINK_ESTABLISH_EVT:
Jon Paul Maloy5ae2f8e2015-08-20 02:12:55 -0400666 case LINK_SYNCH_END_EVT:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400667 break;
668 case LINK_SYNCH_BEGIN_EVT:
669 l->state = LINK_SYNCHING;
670 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400671 case LINK_FAILOVER_BEGIN_EVT:
672 case LINK_FAILOVER_END_EVT:
673 default:
674 goto illegal_evt;
675 }
676 break;
677 case LINK_SYNCHING:
678 switch (evt) {
679 case LINK_PEER_RESET_EVT:
680 l->state = LINK_PEER_RESET;
681 rc |= TIPC_LINK_DOWN_EVT;
682 break;
683 case LINK_FAILURE_EVT:
684 l->state = LINK_RESETTING;
685 rc |= TIPC_LINK_DOWN_EVT;
686 break;
687 case LINK_RESET_EVT:
688 l->state = LINK_RESET;
689 break;
690 case LINK_ESTABLISH_EVT:
691 case LINK_SYNCH_BEGIN_EVT:
692 break;
693 case LINK_SYNCH_END_EVT:
694 l->state = LINK_ESTABLISHED;
695 break;
696 case LINK_FAILOVER_BEGIN_EVT:
697 case LINK_FAILOVER_END_EVT:
698 default:
699 goto illegal_evt;
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400700 }
701 break;
702 default:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400703 pr_err("Unknown FSM state %x in %s\n", l->state, l->name);
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400704 }
Tuong Lien26574db2018-12-19 09:17:57 +0700705 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400706 return rc;
707illegal_evt:
708 pr_err("Illegal FSM event %x in state %x on link %s\n",
709 evt, l->state, l->name);
Tuong Lien26574db2018-12-19 09:17:57 +0700710 trace_tipc_link_fsm(l->name, old_state, l->state, evt);
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400711 return rc;
712}
713
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400714/* link_profile_stats - update statistical profiling of traffic
715 */
716static void link_profile_stats(struct tipc_link *l)
717{
718 struct sk_buff *skb;
719 struct tipc_msg *msg;
720 int length;
721
722 /* Update counters used in statistical profiling of send traffic */
723 l->stats.accu_queue_sz += skb_queue_len(&l->transmq);
724 l->stats.queue_sz_counts++;
725
726 skb = skb_peek(&l->transmq);
727 if (!skb)
728 return;
729 msg = buf_msg(skb);
730 length = msg_size(msg);
731
732 if (msg_user(msg) == MSG_FRAGMENTER) {
733 if (msg_type(msg) != FIRST_FRAGMENT)
734 return;
735 length = msg_size(msg_get_wrapped(msg));
736 }
737 l->stats.msg_lengths_total += length;
738 l->stats.msg_length_counts++;
739 if (length <= 64)
740 l->stats.msg_length_profile[0]++;
741 else if (length <= 256)
742 l->stats.msg_length_profile[1]++;
743 else if (length <= 1024)
744 l->stats.msg_length_profile[2]++;
745 else if (length <= 4096)
746 l->stats.msg_length_profile[3]++;
747 else if (length <= 16384)
748 l->stats.msg_length_profile[4]++;
749 else if (length <= 32768)
750 l->stats.msg_length_profile[5]++;
751 else
752 l->stats.msg_length_profile[6]++;
753}
754
Tuong Lien26574db2018-12-19 09:17:57 +0700755/**
756 * tipc_link_too_silent - check if link is "too silent"
757 * @l: tipc link to be checked
758 *
759 * Returns true if the link 'silent_intv_cnt' is about to reach the
760 * 'abort_limit' value, otherwise false
761 */
762bool tipc_link_too_silent(struct tipc_link *l)
763{
764 return (l->silent_intv_cnt + 2 > l->abort_limit);
765}
766
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400767/* tipc_link_timeout - perform periodic task as instructed from node timeout
768 */
769int tipc_link_timeout(struct tipc_link *l, struct sk_buff_head *xmitq)
770{
Ying Xuec91522f2016-06-15 14:11:31 +0800771 int mtyp = 0;
772 int rc = 0;
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400773 bool state = false;
774 bool probe = false;
775 bool setup = false;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400776 u16 bc_snt = l->bc_sndlink->snd_nxt - 1;
777 u16 bc_acked = l->bc_rcvlink->acked;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400778 struct tipc_mon_state *mstate = &l->mon_state;
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400779
Tuong Lien26574db2018-12-19 09:17:57 +0700780 trace_tipc_link_timeout(l, TIPC_DUMP_NONE, " ");
781 trace_tipc_link_too_silent(l, TIPC_DUMP_ALL, " ");
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400782 switch (l->state) {
783 case LINK_ESTABLISHED:
784 case LINK_SYNCHING:
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400785 mtyp = STATE_MSG;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400786 link_profile_stats(l);
787 tipc_mon_get_state(l->net, l->addr, mstate, l->bearer_id);
788 if (mstate->reset || (l->silent_intv_cnt > l->abort_limit))
789 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400790 state = bc_acked != bc_snt;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400791 state |= l->bc_rcvlink->rcv_unacked;
792 state |= l->rcv_unacked;
793 state |= !skb_queue_empty(&l->transmq);
794 state |= !skb_queue_empty(&l->deferdq);
795 probe = mstate->probing;
796 probe |= l->silent_intv_cnt;
797 if (probe || mstate->monitoring)
798 l->silent_intv_cnt++;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400799 break;
800 case LINK_RESET:
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400801 setup = l->rst_cnt++ <= 4;
802 setup |= !(l->rst_cnt % 16);
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400803 mtyp = RESET_MSG;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400804 break;
805 case LINK_ESTABLISHING:
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400806 setup = true;
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400807 mtyp = ACTIVATE_MSG;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400808 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400809 case LINK_PEER_RESET:
Jon Paul Maloy598411d2015-07-30 18:24:23 -0400810 case LINK_RESETTING:
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400811 case LINK_FAILINGOVER:
812 break;
813 default:
814 break;
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400815 }
Jon Paul Maloy662921c2015-07-30 18:24:21 -0400816
Jon Paul Maloy42b18f62016-04-15 13:33:05 -0400817 if (state || probe || setup)
Jon Maloy8d6e79d2017-11-08 09:59:26 +0100818 tipc_link_build_proto_msg(l, mtyp, probe, 0, 0, 0, 0, xmitq);
Jon Paul Maloy5045f7b2015-07-30 18:24:20 -0400819
Jon Paul Maloy333ef692015-07-16 16:54:28 -0400820 return rc;
821}
822
Jon Paul Maloy6ab30f92015-07-16 16:54:27 -0400823/**
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400824 * link_schedule_user - schedule a message sender for wakeup after congestion
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500825 * @l: congested link
826 * @hdr: header of message that is being sent
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400827 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500829static int link_schedule_user(struct tipc_link *l, struct tipc_msg *hdr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830{
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500831 u32 dnode = tipc_own_addr(l->net);
832 u32 dport = msg_origport(hdr);
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400833 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400835 /* Create and schedule wakeup pseudo message */
836 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500837 dnode, l->addr, dport, 0, 0);
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400838 if (!skb)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400839 return -ENOBUFS;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500840 msg_set_dest_droppable(buf_msg(skb), true);
841 TIPC_SKB_CB(skb)->chain_imp = msg_importance(hdr);
842 skb_queue_tail(&l->wakeupq, skb);
843 l->stats.link_congs++;
Tuong Lien26574db2018-12-19 09:17:57 +0700844 trace_tipc_link_conges(l, TIPC_DUMP_ALL, "wakeup scheduled!");
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400845 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846}
847
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400848/**
849 * link_prepare_wakeup - prepare users for wakeup after congestion
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500850 * @l: congested link
851 * Wake up a number of waiting users, as permitted by available space
852 * in the send queue
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400853 */
YueHaibinge064cce2018-07-19 17:16:59 +0800854static void link_prepare_wakeup(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855{
Ying Xue58d78b32014-11-26 11:41:51 +0800856 struct sk_buff *skb, *tmp;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500857 int imp, i = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400859 skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
860 imp = TIPC_SKB_CB(skb)->chain_imp;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500861 if (l->backlog[imp].len < l->backlog[imp].limit) {
862 skb_unlink(skb, &l->wakeupq);
863 skb_queue_tail(l->inputq, skb);
864 } else if (i++ > 10) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 break;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500866 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868}
869
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400870void tipc_link_reset(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871{
Ying Xuea1f8dd32018-10-11 19:57:56 +0800872 struct sk_buff_head list;
873
874 __skb_queue_head_init(&list);
875
Jon Maloy7ea817f2018-07-10 01:07:36 +0200876 l->in_session = false;
Jon Paul Maloye74a3862016-03-03 14:23:21 -0500877 l->session++;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400878 l->mtu = l->advertised_mtu;
Ying Xuea1f8dd32018-10-11 19:57:56 +0800879
Parthasarathy Bhuvaragan3f32d0b2018-09-25 22:09:10 +0200880 spin_lock_bh(&l->wakeupq.lock);
Ying Xuea1f8dd32018-10-11 19:57:56 +0800881 skb_queue_splice_init(&l->wakeupq, &list);
Parthasarathy Bhuvaragan3f32d0b2018-09-25 22:09:10 +0200882 spin_unlock_bh(&l->wakeupq.lock);
883
Ying Xuea1f8dd32018-10-11 19:57:56 +0800884 spin_lock_bh(&l->inputq->lock);
885 skb_queue_splice_init(&list, l->inputq);
886 spin_unlock_bh(&l->inputq->lock);
887
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400888 __skb_queue_purge(&l->transmq);
889 __skb_queue_purge(&l->deferdq);
Jon Paul Maloy2af5ae32015-10-22 08:51:48 -0400890 __skb_queue_purge(&l->backlogq);
891 l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
892 l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
893 l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
894 l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
895 l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400896 kfree_skb(l->reasm_buf);
897 kfree_skb(l->failover_reasm_skb);
898 l->reasm_buf = NULL;
899 l->failover_reasm_skb = NULL;
900 l->rcv_unacked = 0;
901 l->snd_nxt = 1;
902 l->rcv_nxt = 1;
Jon Maloy9012de52018-07-10 01:07:35 +0200903 l->snd_nxt_state = 1;
904 l->rcv_nxt_state = 1;
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -0400905 l->acked = 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -0400906 l->silent_intv_cnt = 0;
Jon Paul Maloy88e8ac72016-04-15 13:33:04 -0400907 l->rst_cnt = 0;
Jon Maloya4dc70d2018-07-06 15:22:36 +0200908 l->stale_cnt = 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400909 l->bc_peer_is_up = false;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -0400910 memset(&l->mon_state, 0, sizeof(l->mon_state));
Jon Paul Maloy38206d52015-11-19 14:30:46 -0500911 tipc_link_reset_stats(l);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912}
913
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914/**
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400915 * tipc_link_xmit(): enqueue buffer list according to queue situation
916 * @link: link to use
917 * @list: chain of buffers containing message
918 * @xmitq: returned list of packets to be sent by caller
919 *
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500920 * Consumes the buffer chain.
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400921 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
922 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
923 */
924int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
925 struct sk_buff_head *xmitq)
926{
927 struct tipc_msg *hdr = buf_msg(skb_peek(list));
928 unsigned int maxwin = l->window;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500929 int imp = msg_importance(hdr);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400930 unsigned int mtu = l->mtu;
931 u16 ack = l->rcv_nxt - 1;
932 u16 seqno = l->snd_nxt;
Jon Paul Maloy52666982015-10-22 08:51:41 -0400933 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400934 struct sk_buff_head *transmq = &l->transmq;
935 struct sk_buff_head *backlogq = &l->backlogq;
936 struct sk_buff *skb, *_skb, *bskb;
Jon Paul Maloy95901122016-11-25 10:35:02 -0500937 int pkt_cnt = skb_queue_len(list);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500938 int rc = 0;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400939
Richard Alpe4952cd32016-02-11 10:43:15 +0100940 if (unlikely(msg_size(hdr) > mtu)) {
941 skb_queue_purge(list);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400942 return -EMSGSIZE;
Richard Alpe4952cd32016-02-11 10:43:15 +0100943 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400944
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500945 /* Allow oversubscription of one data msg per source at congestion */
946 if (unlikely(l->backlog[imp].len >= l->backlog[imp].limit)) {
947 if (imp == TIPC_SYSTEM_IMPORTANCE) {
948 pr_warn("%s<%s>, link overflow", link_rst_msg, l->name);
949 return -ENOBUFS;
950 }
951 rc = link_schedule_user(l, hdr);
952 }
953
Jon Paul Maloy95901122016-11-25 10:35:02 -0500954 if (pkt_cnt > 1) {
955 l->stats.sent_fragmented++;
956 l->stats.sent_fragments += pkt_cnt;
957 }
958
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400959 /* Prepare each packet for sending, and add to relevant queue: */
960 while (skb_queue_len(list)) {
961 skb = skb_peek(list);
962 hdr = buf_msg(skb);
963 msg_set_seqno(hdr, seqno);
964 msg_set_ack(hdr, ack);
Jon Paul Maloy52666982015-10-22 08:51:41 -0400965 msg_set_bcast_ack(hdr, bc_ack);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400966
967 if (likely(skb_queue_len(transmq) < maxwin)) {
968 _skb = skb_clone(skb, GFP_ATOMIC);
Richard Alpe4952cd32016-02-11 10:43:15 +0100969 if (!_skb) {
970 skb_queue_purge(list);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400971 return -ENOBUFS;
Richard Alpe4952cd32016-02-11 10:43:15 +0100972 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400973 __skb_dequeue(list);
974 __skb_queue_tail(transmq, skb);
Hoang Le05572272018-12-19 11:42:19 +0700975 /* next retransmit attempt */
976 if (link_is_bc_sndlink(l))
977 TIPC_SKB_CB(skb)->nxt_retr =
978 jiffies + TIPC_BC_RETR_LIM;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400979 __skb_queue_tail(xmitq, _skb);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -0400980 TIPC_SKB_CB(skb)->ackers = l->ackers;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400981 l->rcv_unacked = 0;
Jon Paul Maloy95901122016-11-25 10:35:02 -0500982 l->stats.sent_pkts++;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400983 seqno++;
984 continue;
985 }
986 if (tipc_msg_bundle(skb_peek_tail(backlogq), hdr, mtu)) {
987 kfree_skb(__skb_dequeue(list));
988 l->stats.sent_bundled++;
989 continue;
990 }
991 if (tipc_msg_make_bundle(&bskb, hdr, mtu, l->addr)) {
992 kfree_skb(__skb_dequeue(list));
993 __skb_queue_tail(backlogq, bskb);
994 l->backlog[msg_importance(buf_msg(bskb))].len++;
995 l->stats.sent_bundled++;
996 l->stats.sent_bundles++;
997 continue;
998 }
999 l->backlog[imp].len += skb_queue_len(list);
1000 skb_queue_splice_tail_init(list, backlogq);
1001 }
1002 l->snd_nxt = seqno;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001003 return rc;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001004}
1005
YueHaibinge064cce2018-07-19 17:16:59 +08001006static void tipc_link_advance_backlog(struct tipc_link *l,
1007 struct sk_buff_head *xmitq)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001008{
1009 struct sk_buff *skb, *_skb;
1010 struct tipc_msg *hdr;
1011 u16 seqno = l->snd_nxt;
1012 u16 ack = l->rcv_nxt - 1;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001013 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001014
1015 while (skb_queue_len(&l->transmq) < l->window) {
1016 skb = skb_peek(&l->backlogq);
1017 if (!skb)
1018 break;
1019 _skb = skb_clone(skb, GFP_ATOMIC);
1020 if (!_skb)
1021 break;
1022 __skb_dequeue(&l->backlogq);
1023 hdr = buf_msg(skb);
1024 l->backlog[msg_importance(hdr)].len--;
1025 __skb_queue_tail(&l->transmq, skb);
Hoang Le05572272018-12-19 11:42:19 +07001026 /* next retransmit attempt */
1027 if (link_is_bc_sndlink(l))
1028 TIPC_SKB_CB(skb)->nxt_retr = jiffies + TIPC_BC_RETR_LIM;
1029
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001030 __skb_queue_tail(xmitq, _skb);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001031 TIPC_SKB_CB(skb)->ackers = l->ackers;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001032 msg_set_seqno(hdr, seqno);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001033 msg_set_ack(hdr, ack);
1034 msg_set_bcast_ack(hdr, bc_ack);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001035 l->rcv_unacked = 0;
Jon Paul Maloy95901122016-11-25 10:35:02 -05001036 l->stats.sent_pkts++;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001037 seqno++;
1038 }
1039 l->snd_nxt = seqno;
1040}
1041
Jon Paul Maloy52666982015-10-22 08:51:41 -04001042static void link_retransmit_failure(struct tipc_link *l, struct sk_buff *skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001043{
Jon Paul Maloy52666982015-10-22 08:51:41 -04001044 struct tipc_msg *hdr = buf_msg(skb);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001045
Jon Paul Maloy52666982015-10-22 08:51:41 -04001046 pr_warn("Retransmission failure on link <%s>\n", l->name);
Jon Paul Maloy40501f902017-08-21 17:59:30 +02001047 link_print(l, "State of link ");
Jon Paul Maloy52666982015-10-22 08:51:41 -04001048 pr_info("Failed msg: usr %u, typ %u, len %u, err %u\n",
1049 msg_user(hdr), msg_type(hdr), msg_size(hdr), msg_errcode(hdr));
1050 pr_info("sqno %u, prev: %x, src: %x\n",
1051 msg_seqno(hdr), msg_prevnode(hdr), msg_orignode(hdr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001052}
1053
Jon Maloya4dc70d2018-07-06 15:22:36 +02001054/* tipc_link_retrans() - retransmit one or more packets
1055 * @l: the link to transmit on
1056 * @r: the receiving link ordering the retransmit. Same as l if unicast
1057 * @from: retransmit from (inclusive) this sequence number
1058 * @to: retransmit to (inclusive) this sequence number
1059 * xmitq: queue for accumulating the retransmitted packets
1060 */
YueHaibinge064cce2018-07-19 17:16:59 +08001061static int tipc_link_retrans(struct tipc_link *l, struct tipc_link *r,
1062 u16 from, u16 to, struct sk_buff_head *xmitq)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001063{
1064 struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001065 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
Jon Maloya4dc70d2018-07-06 15:22:36 +02001066 u16 ack = l->rcv_nxt - 1;
1067 struct tipc_msg *hdr;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001068
1069 if (!skb)
1070 return 0;
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -05001071 if (less(to, from))
1072 return 0;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001073
Tuong Lien26574db2018-12-19 09:17:57 +07001074 trace_tipc_link_retrans(r, from, to, &l->transmq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001075 /* Detect repeated retransmit failures on same packet */
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -05001076 if (r->prev_from != from) {
1077 r->prev_from = from;
Jon Maloy047491e2018-10-10 17:34:01 +02001078 r->stale_limit = jiffies + msecs_to_jiffies(r->tolerance);
Jon Maloy4af00f42018-10-11 22:02:29 +02001079 r->stale_cnt = 0;
Jon Maloya4dc70d2018-07-06 15:22:36 +02001080 } else if (++r->stale_cnt > 99 && time_after(jiffies, r->stale_limit)) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001081 link_retransmit_failure(l, skb);
Tuong Lien26574db2018-12-19 09:17:57 +07001082 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!");
Jon Paul Maloy40501f902017-08-21 17:59:30 +02001085 if (link_is_bc_sndlink(l))
1086 return TIPC_LINK_DOWN_EVT;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001087 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001088 }
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001089
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001090 skb_queue_walk(&l->transmq, skb) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001091 hdr = buf_msg(skb);
Jon Maloya4dc70d2018-07-06 15:22:36 +02001092 if (less(msg_seqno(hdr), from))
1093 continue;
1094 if (more(msg_seqno(hdr), to))
1095 break;
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -05001096 if (link_is_bc_sndlink(l)) {
1097 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1098 continue;
1099 TIPC_SKB_CB(skb)->nxt_retr = jiffies + TIPC_BC_RETR_LIM;
1100 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001101 _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
1102 if (!_skb)
1103 return 0;
1104 hdr = buf_msg(_skb);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001105 msg_set_ack(hdr, ack);
1106 msg_set_bcast_ack(hdr, bc_ack);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001107 _skb->priority = TC_PRIO_CONTROL;
1108 __skb_queue_tail(xmitq, _skb);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001109 l->stats.retransmitted++;
1110 }
1111 return 0;
1112}
1113
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001114/* tipc_data_input - deliver data and name distr msgs to upper layer
Erik Hugne7ae934b2014-07-01 10:22:40 +02001115 *
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001116 * Consumes buffer if message is of right type
Erik Hugne7ae934b2014-07-01 10:22:40 +02001117 * Node lock must be held
1118 */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001119static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb,
Jon Paul Maloy9073fb82015-07-30 18:24:25 -04001120 struct sk_buff_head *inputq)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001121{
Jon Maloy399574d2017-10-13 11:04:32 +02001122 struct sk_buff_head *mc_inputq = l->bc_rcvlink->inputq;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001123 struct tipc_msg *hdr = buf_msg(skb);
1124
1125 switch (msg_user(hdr)) {
Erik Hugne7ae934b2014-07-01 10:22:40 +02001126 case TIPC_LOW_IMPORTANCE:
1127 case TIPC_MEDIUM_IMPORTANCE:
1128 case TIPC_HIGH_IMPORTANCE:
1129 case TIPC_CRITICAL_IMPORTANCE:
Jon Maloy2f487712017-10-13 11:04:31 +02001130 if (unlikely(msg_in_group(hdr) || msg_mcast(hdr))) {
Jon Maloy399574d2017-10-13 11:04:32 +02001131 skb_queue_tail(mc_inputq, skb);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -05001132 return true;
1133 }
Gustavo A. R. Silvaf79e3362019-01-23 01:09:31 -06001134 /* fall through */
Jon Maloy2f487712017-10-13 11:04:31 +02001135 case CONN_MANAGER:
Jon Maloy36c0a9d2017-10-16 16:04:51 +02001136 skb_queue_tail(inputq, skb);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001137 return true;
Jon Maloy399574d2017-10-13 11:04:32 +02001138 case GROUP_PROTOCOL:
1139 skb_queue_tail(mc_inputq, skb);
1140 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001141 case NAME_DISTRIBUTOR:
Jon Paul Maloy52666982015-10-22 08:51:41 -04001142 l->bc_rcvlink->state = LINK_ESTABLISHED;
1143 skb_queue_tail(l->namedq, skb);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001144 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001145 case MSG_BUNDLER:
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001146 case TUNNEL_PROTOCOL:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001147 case MSG_FRAGMENTER:
1148 case BCAST_PROTOCOL:
1149 return false;
1150 default:
1151 pr_warn("Dropping received illegal msg type\n");
1152 kfree_skb(skb);
Hoang Le7384b532019-02-11 09:18:28 +07001153 return true;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001154 };
1155}
1156
1157/* tipc_link_input - process packet that has passed link protocol check
1158 *
1159 * Consumes buffer
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001160 */
Jon Paul Maloy9073fb82015-07-30 18:24:25 -04001161static int tipc_link_input(struct tipc_link *l, struct sk_buff *skb,
1162 struct sk_buff_head *inputq)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001163{
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001164 struct tipc_msg *hdr = buf_msg(skb);
1165 struct sk_buff **reasm_skb = &l->reasm_buf;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001166 struct sk_buff *iskb;
Jon Paul Maloy9945e802015-10-15 14:52:40 -04001167 struct sk_buff_head tmpq;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001168 int usr = msg_user(hdr);
Jon Paul Maloy6144a992015-07-30 18:24:16 -04001169 int rc = 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001170 int pos = 0;
1171 int ipos = 0;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001172
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001173 if (unlikely(usr == TUNNEL_PROTOCOL)) {
1174 if (msg_type(hdr) == SYNCH_MSG) {
1175 __skb_queue_purge(&l->deferdq);
1176 goto drop;
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -04001177 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001178 if (!tipc_msg_extract(skb, &iskb, &ipos))
1179 return rc;
1180 kfree_skb(skb);
1181 skb = iskb;
1182 hdr = buf_msg(skb);
1183 if (less(msg_seqno(hdr), l->drop_point))
1184 goto drop;
Jon Paul Maloy9073fb82015-07-30 18:24:25 -04001185 if (tipc_data_input(l, skb, inputq))
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001186 return rc;
1187 usr = msg_user(hdr);
1188 reasm_skb = &l->failover_reasm_skb;
1189 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001190
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001191 if (usr == MSG_BUNDLER) {
Jon Paul Maloy9945e802015-10-15 14:52:40 -04001192 skb_queue_head_init(&tmpq);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001193 l->stats.recv_bundles++;
1194 l->stats.recv_bundled += msg_msgcnt(hdr);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001195 while (tipc_msg_extract(skb, &iskb, &pos))
Jon Paul Maloy9945e802015-10-15 14:52:40 -04001196 tipc_data_input(l, iskb, &tmpq);
1197 tipc_skb_queue_splice_tail(&tmpq, inputq);
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001198 return 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001199 } else if (usr == MSG_FRAGMENTER) {
1200 l->stats.recv_fragments++;
1201 if (tipc_buf_append(reasm_skb, &skb)) {
1202 l->stats.recv_fragmented++;
Jon Paul Maloy9073fb82015-07-30 18:24:25 -04001203 tipc_data_input(l, skb, inputq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001204 } else if (!*reasm_skb && !link_is_bc_rcvlink(l)) {
1205 pr_warn_ratelimited("Unable to build fragment list\n");
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001206 return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001207 }
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001208 return 0;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001209 } else if (usr == BCAST_PROTOCOL) {
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001210 tipc_bcast_lock(l->net);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001211 tipc_link_bc_init_rcv(l->bc_rcvlink, hdr);
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04001212 tipc_bcast_unlock(l->net);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001213 }
1214drop:
1215 kfree_skb(skb);
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001216 return 0;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001217}
1218
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001219static bool tipc_link_release_pkts(struct tipc_link *l, u16 acked)
1220{
1221 bool released = false;
1222 struct sk_buff *skb, *tmp;
1223
1224 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1225 if (more(buf_seqno(skb), acked))
1226 break;
1227 __skb_unlink(skb, &l->transmq);
1228 kfree_skb(skb);
1229 released = true;
1230 }
1231 return released;
1232}
1233
Tuong Lien91959482019-04-04 11:09:51 +07001234/* tipc_build_gap_ack_blks - build Gap ACK blocks
1235 * @l: tipc link that data have come with gaps in sequence if any
1236 * @data: data buffer to store the Gap ACK blocks after built
1237 *
1238 * returns the actual allocated memory size
1239 */
1240static u16 tipc_build_gap_ack_blks(struct tipc_link *l, void *data)
1241{
1242 struct sk_buff *skb = skb_peek(&l->deferdq);
1243 struct tipc_gap_ack_blks *ga = data;
1244 u16 len, expect, seqno = 0;
1245 u8 n = 0;
1246
1247 if (!skb)
1248 goto exit;
1249
1250 expect = buf_seqno(skb);
1251 skb_queue_walk(&l->deferdq, skb) {
1252 seqno = buf_seqno(skb);
1253 if (unlikely(more(seqno, expect))) {
1254 ga->gacks[n].ack = htons(expect - 1);
1255 ga->gacks[n].gap = htons(seqno - expect);
1256 if (++n >= MAX_GAP_ACK_BLKS) {
1257 pr_info_ratelimited("Too few Gap ACK blocks!\n");
1258 goto exit;
1259 }
1260 } else if (unlikely(less(seqno, expect))) {
1261 pr_warn("Unexpected skb in deferdq!\n");
1262 continue;
1263 }
1264 expect = seqno + 1;
1265 }
1266
1267 /* last block */
1268 ga->gacks[n].ack = htons(seqno);
1269 ga->gacks[n].gap = 0;
1270 n++;
1271
1272exit:
1273 len = tipc_gap_ack_blks_sz(n);
1274 ga->len = htons(len);
1275 ga->gack_cnt = n;
1276 return len;
1277}
1278
1279/* tipc_link_advance_transmq - advance TIPC link transmq queue by releasing
1280 * acked packets, also doing retransmissions if
1281 * gaps found
1282 * @l: tipc link with transmq queue to be advanced
1283 * @acked: seqno of last packet acked by peer without any gaps before
1284 * @gap: # of gap packets
1285 * @ga: buffer pointer to Gap ACK blocks from peer
1286 * @xmitq: queue for accumulating the retransmitted packets if any
1287 */
1288static void tipc_link_advance_transmq(struct tipc_link *l, u16 acked, u16 gap,
1289 struct tipc_gap_ack_blks *ga,
1290 struct sk_buff_head *xmitq)
1291{
1292 struct sk_buff *skb, *_skb, *tmp;
1293 struct tipc_msg *hdr;
1294 u16 bc_ack = l->bc_rcvlink->rcv_nxt - 1;
1295 u16 ack = l->rcv_nxt - 1;
1296 u16 seqno;
1297 u16 n = 0;
1298
1299 skb_queue_walk_safe(&l->transmq, skb, tmp) {
1300 seqno = buf_seqno(skb);
1301
1302next_gap_ack:
1303 if (less_eq(seqno, acked)) {
1304 /* release skb */
1305 __skb_unlink(skb, &l->transmq);
1306 kfree_skb(skb);
1307 } else if (less_eq(seqno, acked + gap)) {
1308 /* retransmit skb */
Tuong Lien382f5982019-04-04 11:09:52 +07001309 if (time_before(jiffies, TIPC_SKB_CB(skb)->nxt_retr))
1310 continue;
1311 TIPC_SKB_CB(skb)->nxt_retr = TIPC_UC_RETR_TIME;
1312
Tuong Lien91959482019-04-04 11:09:51 +07001313 _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
1314 if (!_skb)
1315 continue;
1316 hdr = buf_msg(_skb);
1317 msg_set_ack(hdr, ack);
1318 msg_set_bcast_ack(hdr, bc_ack);
1319 _skb->priority = TC_PRIO_CONTROL;
1320 __skb_queue_tail(xmitq, _skb);
1321 l->stats.retransmitted++;
1322 } else {
1323 /* retry with Gap ACK blocks if any */
1324 if (!ga || n >= ga->gack_cnt)
1325 break;
1326 acked = ntohs(ga->gacks[n].ack);
1327 gap = ntohs(ga->gacks[n].gap);
1328 n++;
1329 goto next_gap_ack;
1330 }
1331 }
1332}
1333
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001334/* tipc_link_build_state_msg: prepare link state message for transmission
Jon Paul Maloy52666982015-10-22 08:51:41 -04001335 *
1336 * Note that sending of broadcast ack is coordinated among nodes, to reduce
1337 * risk of ack storms towards the sender
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001338 */
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001339int tipc_link_build_state_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001340{
Jon Paul Maloy52666982015-10-22 08:51:41 -04001341 if (!l)
1342 return 0;
1343
1344 /* Broadcast ACK must be sent via a unicast link => defer to caller */
1345 if (link_is_bc_rcvlink(l)) {
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001346 if (((l->rcv_nxt ^ tipc_own_addr(l->net)) & 0xf) != 0xf)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001347 return 0;
1348 l->rcv_unacked = 0;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001349
1350 /* Use snd_nxt to store peer's snd_nxt in broadcast rcv link */
1351 l->snd_nxt = l->rcv_nxt;
1352 return TIPC_LINK_SND_STATE;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001353 }
1354
1355 /* Unicast ACK */
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001356 l->rcv_unacked = 0;
1357 l->stats.sent_acks++;
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001358 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001359 return 0;
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001360}
1361
Jon Paul Maloy282b3a02015-10-15 14:52:45 -04001362/* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
1363 */
1364void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
1365{
1366 int mtyp = RESET_MSG;
Jon Paul Maloy634696b2016-04-15 13:33:03 -04001367 struct sk_buff *skb;
Jon Paul Maloy282b3a02015-10-15 14:52:45 -04001368
1369 if (l->state == LINK_ESTABLISHING)
1370 mtyp = ACTIVATE_MSG;
1371
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001372 tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, 0, xmitq);
Jon Paul Maloy634696b2016-04-15 13:33:03 -04001373
1374 /* Inform peer that this endpoint is going down if applicable */
1375 skb = skb_peek_tail(xmitq);
1376 if (skb && (l->state == LINK_RESET))
1377 msg_set_peer_stopping(buf_msg(skb), 1);
Jon Paul Maloy282b3a02015-10-15 14:52:45 -04001378}
1379
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001380/* tipc_link_build_nack_msg: prepare link nack message for transmission
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001381 * Note that sending of broadcast NACK is coordinated among nodes, to
1382 * reduce the risk of NACK storms towards the sender
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001383 */
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001384static int tipc_link_build_nack_msg(struct tipc_link *l,
1385 struct sk_buff_head *xmitq)
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001386{
1387 u32 def_cnt = ++l->stats.deferred_recv;
Tuong Lien382f5982019-04-04 11:09:52 +07001388 u32 defq_len = skb_queue_len(&l->deferdq);
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001389 int match1, match2;
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001390
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001391 if (link_is_bc_rcvlink(l)) {
1392 match1 = def_cnt & 0xf;
1393 match2 = tipc_own_addr(l->net) & 0xf;
1394 if (match1 == match2)
1395 return TIPC_LINK_SND_STATE;
1396 return 0;
1397 }
Jon Paul Maloy52666982015-10-22 08:51:41 -04001398
Tuong Lien382f5982019-04-04 11:09:52 +07001399 if (defq_len >= 3 && !((defq_len - 3) % 16))
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001400 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, 0, xmitq);
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001401 return 0;
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001402}
1403
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001404/* tipc_link_rcv - process TIPC packets/messages arriving from off-node
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001405 * @l: the link that should handle the message
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001406 * @skb: TIPC packet
1407 * @xmitq: queue to place packets to be sent after this call
1408 */
1409int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
1410 struct sk_buff_head *xmitq)
1411{
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001412 struct sk_buff_head *defq = &l->deferdq;
Tuong Lien382f5982019-04-04 11:09:52 +07001413 struct tipc_msg *hdr = buf_msg(skb);
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001414 u16 seqno, rcv_nxt, win_lim;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001415 int rc = 0;
1416
Tuong Lien382f5982019-04-04 11:09:52 +07001417 /* Verify and update link state */
1418 if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
1419 return tipc_link_proto_rcv(l, skb, xmitq);
1420
1421 /* Don't send probe at next timeout expiration */
1422 l->silent_intv_cnt = 0;
1423
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001424 do {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001425 hdr = buf_msg(skb);
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001426 seqno = msg_seqno(hdr);
1427 rcv_nxt = l->rcv_nxt;
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001428 win_lim = rcv_nxt + TIPC_MAX_LINK_WIN;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001429
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001430 if (unlikely(!link_is_up(l))) {
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001431 if (l->state == LINK_ESTABLISHING)
1432 rc = TIPC_LINK_UP_EVT;
1433 goto drop;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001434 }
1435
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001436 /* Drop if outside receive window */
1437 if (unlikely(less(seqno, rcv_nxt) || more(seqno, win_lim))) {
1438 l->stats.duplicates++;
1439 goto drop;
1440 }
1441
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001442 /* Forward queues and wake up waiting users */
1443 if (likely(tipc_link_release_pkts(l, msg_ack(hdr)))) {
Jon Maloya4dc70d2018-07-06 15:22:36 +02001444 l->stale_cnt = 0;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001445 tipc_link_advance_backlog(l, xmitq);
1446 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1447 link_prepare_wakeup(l);
1448 }
1449
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001450 /* Defer delivery if sequence gap */
1451 if (unlikely(seqno != rcv_nxt)) {
Jon Paul Maloy8306f992015-10-15 14:52:43 -04001452 __tipc_skb_queue_sorted(defq, seqno, skb);
Jon Paul Maloye0a05eb2016-09-01 13:52:51 -04001453 rc |= tipc_link_build_nack_msg(l, xmitq);
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001454 break;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001455 }
1456
Jon Paul Maloy81204c42015-10-15 14:52:42 -04001457 /* Deliver packet */
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001458 l->rcv_nxt++;
Jon Paul Maloy95901122016-11-25 10:35:02 -05001459 l->stats.recv_pkts++;
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001460 if (!tipc_data_input(l, skb, l->inputq))
Jon Paul Maloy52666982015-10-22 08:51:41 -04001461 rc |= tipc_link_input(l, skb, l->inputq);
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001462 if (unlikely(++l->rcv_unacked >= TIPC_MIN_LINK_WIN))
Jon Paul Maloy34b9cd62016-04-15 13:33:07 -04001463 rc |= tipc_link_build_state_msg(l, xmitq);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001464 if (unlikely(rc & ~TIPC_LINK_SND_STATE))
Jon Paul Maloy52666982015-10-22 08:51:41 -04001465 break;
Tuong Lien382f5982019-04-04 11:09:52 +07001466 } while ((skb = __tipc_skb_dequeue(defq, l->rcv_nxt)));
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001467
1468 return rc;
1469drop:
1470 kfree_skb(skb);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001471 return rc;
1472}
1473
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001474static void tipc_link_build_proto_msg(struct tipc_link *l, int mtyp, bool probe,
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001475 bool probe_reply, u16 rcvgap,
1476 int tolerance, int priority,
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001477 struct sk_buff_head *xmitq)
1478{
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001479 struct tipc_link *bcl = l->bc_rcvlink;
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001480 struct sk_buff *skb;
1481 struct tipc_msg *hdr;
1482 struct sk_buff_head *dfq = &l->deferdq;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001483 bool node_up = link_is_up(bcl);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001484 struct tipc_mon_state *mstate = &l->mon_state;
1485 int dlen = 0;
1486 void *data;
Tuong Lien91959482019-04-04 11:09:51 +07001487 u16 glen = 0;
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001488
1489 /* Don't send protocol message during reset or link failover */
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001490 if (tipc_link_is_blocked(l))
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001491 return;
1492
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001493 if (!tipc_link_is_up(l) && (mtyp == STATE_MSG))
1494 return;
1495
1496 if (!skb_queue_empty(dfq))
1497 rcvgap = buf_seqno(skb_peek(dfq)) - l->rcv_nxt;
1498
1499 skb = tipc_msg_create(LINK_PROTOCOL, mtyp, INT_H_SIZE,
Tuong Lien91959482019-04-04 11:09:51 +07001500 tipc_max_domain_size + MAX_GAP_ACK_BLKS_SZ,
1501 l->addr, tipc_own_addr(l->net), 0, 0, 0);
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001502 if (!skb)
1503 return;
1504
1505 hdr = buf_msg(skb);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001506 data = msg_data(hdr);
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001507 msg_set_session(hdr, l->session);
1508 msg_set_bearer_id(hdr, l->bearer_id);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001509 msg_set_net_plane(hdr, l->net_plane);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001510 msg_set_next_sent(hdr, l->snd_nxt);
1511 msg_set_ack(hdr, l->rcv_nxt - 1);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001512 msg_set_bcast_ack(hdr, bcl->rcv_nxt - 1);
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001513 msg_set_bc_ack_invalid(hdr, !node_up);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001514 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001515 msg_set_link_tolerance(hdr, tolerance);
1516 msg_set_linkprio(hdr, priority);
1517 msg_set_redundant_link(hdr, node_up);
1518 msg_set_seq_gap(hdr, 0);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001519 msg_set_seqno(hdr, l->snd_nxt + U16_MAX / 2);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001520
1521 if (mtyp == STATE_MSG) {
Jon Maloy9012de52018-07-10 01:07:35 +02001522 if (l->peer_caps & TIPC_LINK_PROTO_SEQNO)
1523 msg_set_seqno(hdr, l->snd_nxt_state++);
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001524 msg_set_seq_gap(hdr, rcvgap);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001525 msg_set_bc_gap(hdr, link_bc_rcv_gap(bcl));
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001526 msg_set_probe(hdr, probe);
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001527 msg_set_is_keepalive(hdr, probe || probe_reply);
Tuong Lien91959482019-04-04 11:09:51 +07001528 if (l->peer_caps & TIPC_GAP_ACK_BLOCK)
1529 glen = tipc_build_gap_ack_blks(l, data);
1530 tipc_mon_prep(l->net, data + glen, &dlen, mstate, l->bearer_id);
1531 msg_set_size(hdr, INT_H_SIZE + glen + dlen);
1532 skb_trim(skb, INT_H_SIZE + glen + dlen);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001533 l->stats.sent_states++;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001534 l->rcv_unacked = 0;
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001535 } else {
1536 /* RESET_MSG or ACTIVATE_MSG */
Tuong Lien91986ee2019-02-11 13:29:43 +07001537 if (mtyp == ACTIVATE_MSG) {
1538 msg_set_dest_session_valid(hdr, 1);
1539 msg_set_dest_session(hdr, l->peer_session);
1540 }
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001541 msg_set_max_pkt(hdr, l->advertised_mtu);
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001542 strcpy(data, l->if_name);
1543 msg_set_size(hdr, INT_H_SIZE + TIPC_MAX_IF_NAME);
1544 skb_trim(skb, INT_H_SIZE + TIPC_MAX_IF_NAME);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001545 }
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001546 if (probe)
1547 l->stats.sent_probes++;
1548 if (rcvgap)
1549 l->stats.sent_nacks++;
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001550 skb->priority = TC_PRIO_CONTROL;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001551 __skb_queue_tail(xmitq, skb);
Tuong Lien26574db2018-12-19 09:17:57 +07001552 trace_tipc_proto_build(skb, false, l->name);
Jon Paul Maloy426cc2b2015-07-16 16:54:26 -04001553}
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554
LUU Duc Canhc140eb12018-09-26 21:00:54 +02001555void tipc_link_create_dummy_tnl_msg(struct tipc_link *l,
1556 struct sk_buff_head *xmitq)
1557{
1558 u32 onode = tipc_own_addr(l->net);
1559 struct tipc_msg *hdr, *ihdr;
1560 struct sk_buff_head tnlq;
1561 struct sk_buff *skb;
1562 u32 dnode = l->addr;
1563
1564 skb_queue_head_init(&tnlq);
1565 skb = tipc_msg_create(TUNNEL_PROTOCOL, FAILOVER_MSG,
1566 INT_H_SIZE, BASIC_H_SIZE,
1567 dnode, onode, 0, 0, 0);
1568 if (!skb) {
1569 pr_warn("%sunable to create tunnel packet\n", link_co_err);
1570 return;
1571 }
1572
1573 hdr = buf_msg(skb);
1574 msg_set_msgcnt(hdr, 1);
1575 msg_set_bearer_id(hdr, l->peer_bearer_id);
1576
1577 ihdr = (struct tipc_msg *)msg_data(hdr);
1578 tipc_msg_init(onode, ihdr, TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
1579 BASIC_H_SIZE, dnode);
1580 msg_set_errcode(ihdr, TIPC_ERR_NO_PORT);
1581 __skb_queue_tail(&tnlq, skb);
1582 tipc_link_xmit(l, &tnlq, xmitq);
1583}
1584
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001585/* tipc_link_tnl_prepare(): prepare and return a list of tunnel packets
Jon Paul Maloyf9aa3582015-10-15 14:52:41 -04001586 * with contents of the link's transmit and backlog queues.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 */
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001588void tipc_link_tnl_prepare(struct tipc_link *l, struct tipc_link *tnl,
1589 int mtyp, struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590{
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001591 struct sk_buff *skb, *tnlskb;
1592 struct tipc_msg *hdr, tnlhdr;
1593 struct sk_buff_head *queue = &l->transmq;
1594 struct sk_buff_head tmpxq, tnlq;
1595 u16 pktlen, pktcnt, seqno = l->snd_nxt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001597 if (!tnl)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 return;
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001599
1600 skb_queue_head_init(&tnlq);
1601 skb_queue_head_init(&tmpxq);
1602
1603 /* At least one packet required for safe algorithm => add dummy */
1604 skb = tipc_msg_create(TIPC_LOW_IMPORTANCE, TIPC_DIRECT_MSG,
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001605 BASIC_H_SIZE, 0, l->addr, tipc_own_addr(l->net),
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001606 0, 0, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +08001607 if (!skb) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001608 pr_warn("%sunable to create tunnel packet\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001610 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001611 skb_queue_tail(&tnlq, skb);
1612 tipc_link_xmit(l, &tnlq, &tmpxq);
1613 __skb_queue_purge(&tmpxq);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001615 /* Initialize reusable tunnel packet header */
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001616 tipc_msg_init(tipc_own_addr(l->net), &tnlhdr, TUNNEL_PROTOCOL,
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001617 mtyp, INT_H_SIZE, l->addr);
1618 pktcnt = skb_queue_len(&l->transmq) + skb_queue_len(&l->backlogq);
1619 msg_set_msgcnt(&tnlhdr, pktcnt);
1620 msg_set_bearer_id(&tnlhdr, l->peer_bearer_id);
1621tnl:
1622 /* Wrap each packet into a tunnel packet */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001623 skb_queue_walk(queue, skb) {
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001624 hdr = buf_msg(skb);
1625 if (queue == &l->backlogq)
1626 msg_set_seqno(hdr, seqno++);
1627 pktlen = msg_size(hdr);
1628 msg_set_size(&tnlhdr, pktlen + INT_H_SIZE);
Parthasarathy Bhuvaragan57d5f642017-01-13 15:46:25 +01001629 tnlskb = tipc_buf_acquire(pktlen + INT_H_SIZE, GFP_ATOMIC);
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001630 if (!tnlskb) {
1631 pr_warn("%sunable to send packet\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 return;
1633 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001634 skb_copy_to_linear_data(tnlskb, &tnlhdr, INT_H_SIZE);
1635 skb_copy_to_linear_data_offset(tnlskb, INT_H_SIZE, hdr, pktlen);
1636 __skb_queue_tail(&tnlq, tnlskb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 }
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001638 if (queue != &l->backlogq) {
1639 queue = &l->backlogq;
1640 goto tnl;
Jon Paul Maloydd3f9e72015-05-14 10:46:18 -04001641 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001643 tipc_link_xmit(tnl, &tnlq, xmitq);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001644
Jon Paul Maloy6e498152015-07-30 18:24:19 -04001645 if (mtyp == FAILOVER_MSG) {
1646 tnl->drop_point = l->rcv_nxt;
1647 tnl->failover_reasm_skb = l->reasm_buf;
1648 l->reasm_buf = NULL;
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001649 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650}
1651
Jon Maloy7ea817f2018-07-10 01:07:36 +02001652/* tipc_link_validate_msg(): validate message against current link state
1653 * Returns true if message should be accepted, otherwise false
1654 */
1655bool tipc_link_validate_msg(struct tipc_link *l, struct tipc_msg *hdr)
1656{
1657 u16 curr_session = l->peer_session;
1658 u16 session = msg_session(hdr);
1659 int mtyp = msg_type(hdr);
1660
1661 if (msg_user(hdr) != LINK_PROTOCOL)
1662 return true;
1663
1664 switch (mtyp) {
1665 case RESET_MSG:
1666 if (!l->in_session)
1667 return true;
1668 /* Accept only RESET with new session number */
1669 return more(session, curr_session);
1670 case ACTIVATE_MSG:
1671 if (!l->in_session)
1672 return true;
1673 /* Accept only ACTIVATE with new or current session number */
1674 return !less(session, curr_session);
1675 case STATE_MSG:
1676 /* Accept only STATE with current session number */
1677 if (!l->in_session)
1678 return false;
1679 if (session != curr_session)
1680 return false;
LUU Duc Canhd949cfe2018-09-26 22:28:52 +02001681 /* Extra sanity check */
1682 if (!link_is_up(l) && msg_ack(hdr))
1683 return false;
Jon Maloy7ea817f2018-07-10 01:07:36 +02001684 if (!(l->peer_caps & TIPC_LINK_PROTO_SEQNO))
1685 return true;
1686 /* Accept only STATE with new sequence number */
1687 return !less(msg_seqno(hdr), l->rcv_nxt_state);
1688 default:
1689 return false;
1690 }
1691}
1692
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001693/* tipc_link_proto_rcv(): receive link level protocol message :
1694 * Note that network plane id propagates through the network, and may
1695 * change at any time. The node with lowest numerical id determines
1696 * network plane
1697 */
1698static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
1699 struct sk_buff_head *xmitq)
1700{
1701 struct tipc_msg *hdr = buf_msg(skb);
Tuong Lien91959482019-04-04 11:09:51 +07001702 struct tipc_gap_ack_blks *ga = NULL;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001703 u16 rcvgap = 0;
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04001704 u16 ack = msg_ack(hdr);
1705 u16 gap = msg_seq_gap(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001706 u16 peers_snd_nxt = msg_next_sent(hdr);
1707 u16 peers_tol = msg_link_tolerance(hdr);
1708 u16 peers_prio = msg_linkprio(hdr);
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001709 u16 rcv_nxt = l->rcv_nxt;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001710 u16 dlen = msg_data_sz(hdr);
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001711 int mtyp = msg_type(hdr);
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001712 bool reply = msg_probe(hdr);
Tuong Lien91959482019-04-04 11:09:51 +07001713 u16 glen = 0;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001714 void *data;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001715 char *if_name;
1716 int rc = 0;
1717
Tuong Lien26574db2018-12-19 09:17:57 +07001718 trace_tipc_proto_rcv(skb, false, l->name);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001719 if (tipc_link_is_blocked(l) || !xmitq)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001720 goto exit;
1721
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001722 if (tipc_own_addr(l->net) > msg_prevnode(hdr))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001723 l->net_plane = msg_net_plane(hdr);
1724
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001725 skb_linearize(skb);
1726 hdr = buf_msg(skb);
1727 data = msg_data(hdr);
1728
Tuong Lien26574db2018-12-19 09:17:57 +07001729 if (!tipc_link_validate_msg(l, hdr)) {
1730 trace_tipc_skb_dump(skb, false, "PROTO invalid (1)!");
1731 trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (1)!");
Jon Maloy7ea817f2018-07-10 01:07:36 +02001732 goto exit;
Tuong Lien26574db2018-12-19 09:17:57 +07001733 }
Jon Maloy7ea817f2018-07-10 01:07:36 +02001734
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001735 switch (mtyp) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001736 case RESET_MSG:
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001737 case ACTIVATE_MSG:
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001738 /* Complete own link name with peer's interface name */
1739 if_name = strrchr(l->name, ':') + 1;
1740 if (sizeof(l->name) - (if_name - l->name) <= TIPC_MAX_IF_NAME)
1741 break;
1742 if (msg_data_sz(hdr) < TIPC_MAX_IF_NAME)
1743 break;
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001744 strncpy(if_name, data, TIPC_MAX_IF_NAME);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001745
1746 /* Update own tolerance if peer indicates a non-zero value */
Jon Maloy047491e2018-10-10 17:34:01 +02001747 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001748 l->tolerance = peers_tol;
Jon Maloy047491e2018-10-10 17:34:01 +02001749 l->bc_rcvlink->tolerance = peers_tol;
1750 }
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001751 /* Update own priority if peer's priority is higher */
1752 if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
1753 l->priority = peers_prio;
1754
Jon Maloy7ab412d2018-11-10 17:30:24 -05001755 /* If peer is going down we want full re-establish cycle */
1756 if (msg_peer_stopping(hdr)) {
Jon Paul Maloy634696b2016-04-15 13:33:03 -04001757 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
Jon Maloy7ab412d2018-11-10 17:30:24 -05001758 break;
1759 }
Tuong Lien91986ee2019-02-11 13:29:43 +07001760
1761 /* If this endpoint was re-created while peer was ESTABLISHING
1762 * it doesn't know current session number. Force re-synch.
1763 */
1764 if (mtyp == ACTIVATE_MSG && msg_dest_session_valid(hdr) &&
1765 l->session != msg_dest_session(hdr)) {
1766 if (less(l->session, msg_dest_session(hdr)))
1767 l->session = msg_dest_session(hdr) + 1;
1768 break;
1769 }
1770
Jon Maloy7ab412d2018-11-10 17:30:24 -05001771 /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
1772 if (mtyp == RESET_MSG || !link_is_up(l))
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001773 rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
1774
1775 /* ACTIVATE_MSG takes up link if it was already locally reset */
Jon Maloy7ab412d2018-11-10 17:30:24 -05001776 if (mtyp == ACTIVATE_MSG && l->state == LINK_ESTABLISHING)
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001777 rc = TIPC_LINK_UP_EVT;
1778
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001779 l->peer_session = msg_session(hdr);
Jon Maloy7ea817f2018-07-10 01:07:36 +02001780 l->in_session = true;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001781 l->peer_bearer_id = msg_bearer_id(hdr);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001782 if (l->mtu > msg_max_pkt(hdr))
1783 l->mtu = msg_max_pkt(hdr);
1784 break;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001785
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001786 case STATE_MSG:
Jon Maloy9012de52018-07-10 01:07:35 +02001787 l->rcv_nxt_state = msg_seqno(hdr) + 1;
1788
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001789 /* Update own tolerance if peer indicates a non-zero value */
Jon Maloy047491e2018-10-10 17:34:01 +02001790 if (in_range(peers_tol, TIPC_MIN_LINK_TOL, TIPC_MAX_LINK_TOL)) {
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001791 l->tolerance = peers_tol;
Jon Maloy047491e2018-10-10 17:34:01 +02001792 l->bc_rcvlink->tolerance = peers_tol;
1793 }
Jon Paul Maloyf7967552016-11-23 21:05:26 -05001794 /* Update own prio if peer indicates a different value */
1795 if ((peers_prio != l->priority) &&
1796 in_range(peers_prio, 1, TIPC_MAX_LINK_PRI)) {
Richard Alpe81729812016-02-01 08:19:57 +01001797 l->priority = peers_prio;
1798 rc = tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
1799 }
1800
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001801 l->silent_intv_cnt = 0;
1802 l->stats.recv_states++;
1803 if (msg_probe(hdr))
1804 l->stats.recv_probes++;
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001805
1806 if (!link_is_up(l)) {
1807 if (l->state == LINK_ESTABLISHING)
1808 rc = TIPC_LINK_UP_EVT;
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001809 break;
Jon Paul Maloy73f646c2015-10-15 14:52:44 -04001810 }
Tuong Lien91959482019-04-04 11:09:51 +07001811
1812 /* Receive Gap ACK blocks from peer if any */
1813 if (l->peer_caps & TIPC_GAP_ACK_BLOCK) {
1814 ga = (struct tipc_gap_ack_blks *)data;
1815 glen = ntohs(ga->len);
1816 /* sanity check: if failed, ignore Gap ACK blocks */
1817 if (glen != tipc_gap_ack_blks_sz(ga->gack_cnt))
1818 ga = NULL;
1819 }
1820
1821 tipc_mon_rcv(l->net, data + glen, dlen - glen, l->addr,
Jon Paul Maloy35c55c92016-06-13 20:46:22 -04001822 &l->mon_state, l->bearer_id);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001823
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001824 /* Send NACK if peer has sent pkts we haven't received yet */
Jon Paul Maloy2be80c22015-08-20 02:12:56 -04001825 if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001826 rcvgap = peers_snd_nxt - l->rcv_nxt;
Jon Maloy8d6e79d2017-11-08 09:59:26 +01001827 if (rcvgap || reply)
1828 tipc_link_build_proto_msg(l, STATE_MSG, 0, reply,
1829 rcvgap, 0, 0, xmitq);
Tuong Lien91959482019-04-04 11:09:51 +07001830
1831 tipc_link_advance_transmq(l, ack, gap, ga, xmitq);
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001832
1833 /* If NACK, retransmit will now start at right position */
Tuong Lien91959482019-04-04 11:09:51 +07001834 if (gap)
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001835 l->stats.recv_nacks++;
Jon Paul Maloy662921c2015-07-30 18:24:21 -04001836
Jon Paul Maloyd9992972015-07-16 16:54:31 -04001837 tipc_link_advance_backlog(l, xmitq);
1838 if (unlikely(!skb_queue_empty(&l->wakeupq)))
1839 link_prepare_wakeup(l);
1840 }
1841exit:
1842 kfree_skb(skb);
1843 return rc;
1844}
1845
Jon Paul Maloy52666982015-10-22 08:51:41 -04001846/* tipc_link_build_bc_proto_msg() - create broadcast protocol message
1847 */
1848static bool tipc_link_build_bc_proto_msg(struct tipc_link *l, bool bcast,
1849 u16 peers_snd_nxt,
1850 struct sk_buff_head *xmitq)
1851{
1852 struct sk_buff *skb;
1853 struct tipc_msg *hdr;
1854 struct sk_buff *dfrd_skb = skb_peek(&l->deferdq);
1855 u16 ack = l->rcv_nxt - 1;
1856 u16 gap_to = peers_snd_nxt - 1;
1857
1858 skb = tipc_msg_create(BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE,
Jon Paul Maloye74a3862016-03-03 14:23:21 -05001859 0, l->addr, tipc_own_addr(l->net), 0, 0, 0);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001860 if (!skb)
1861 return false;
1862 hdr = buf_msg(skb);
1863 msg_set_last_bcast(hdr, l->bc_sndlink->snd_nxt - 1);
1864 msg_set_bcast_ack(hdr, ack);
1865 msg_set_bcgap_after(hdr, ack);
1866 if (dfrd_skb)
1867 gap_to = buf_seqno(dfrd_skb) - 1;
1868 msg_set_bcgap_to(hdr, gap_to);
1869 msg_set_non_seq(hdr, bcast);
1870 __skb_queue_tail(xmitq, skb);
1871 return true;
1872}
1873
1874/* tipc_link_build_bc_init_msg() - synchronize broadcast link endpoints.
1875 *
1876 * Give a newly added peer node the sequence number where it should
1877 * start receiving and acking broadcast packets.
1878 */
Wu Fengguang742e0382015-10-24 22:56:01 +08001879static void tipc_link_build_bc_init_msg(struct tipc_link *l,
1880 struct sk_buff_head *xmitq)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001881{
1882 struct sk_buff_head list;
1883
1884 __skb_queue_head_init(&list);
1885 if (!tipc_link_build_bc_proto_msg(l->bc_rcvlink, false, 0, &list))
1886 return;
Jon Paul Maloy06bd2b12016-10-27 18:51:55 -04001887 msg_set_bc_ack_invalid(buf_msg(skb_peek(&list)), true);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001888 tipc_link_xmit(l, &list, xmitq);
1889}
1890
1891/* tipc_link_bc_init_rcv - receive initial broadcast synch data from peer
1892 */
1893void tipc_link_bc_init_rcv(struct tipc_link *l, struct tipc_msg *hdr)
1894{
1895 int mtyp = msg_type(hdr);
1896 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
1897
1898 if (link_is_up(l))
1899 return;
1900
1901 if (msg_user(hdr) == BCAST_PROTOCOL) {
1902 l->rcv_nxt = peers_snd_nxt;
1903 l->state = LINK_ESTABLISHED;
1904 return;
1905 }
1906
1907 if (l->peer_caps & TIPC_BCAST_SYNCH)
1908 return;
1909
1910 if (msg_peer_node_is_up(hdr))
1911 return;
1912
1913 /* Compatibility: accept older, less safe initial synch data */
1914 if ((mtyp == RESET_MSG) || (mtyp == ACTIVATE_MSG))
1915 l->rcv_nxt = peers_snd_nxt;
1916}
1917
1918/* tipc_link_bc_sync_rcv - update rcv link according to peer's send state
1919 */
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001920int tipc_link_bc_sync_rcv(struct tipc_link *l, struct tipc_msg *hdr,
1921 struct sk_buff_head *xmitq)
Jon Paul Maloy52666982015-10-22 08:51:41 -04001922{
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -04001923 struct tipc_link *snd_l = l->bc_sndlink;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001924 u16 peers_snd_nxt = msg_bc_snd_nxt(hdr);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001925 u16 from = msg_bcast_ack(hdr) + 1;
1926 u16 to = from + msg_bc_gap(hdr) - 1;
1927 int rc = 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001928
1929 if (!link_is_up(l))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001930 return rc;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001931
1932 if (!msg_peer_node_is_up(hdr))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001933 return rc;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001934
Jon Paul Maloy2d18ac42016-07-11 16:08:35 -04001935 /* Open when peer ackowledges our bcast init msg (pkt #1) */
1936 if (msg_ack(hdr))
1937 l->bc_peer_is_up = true;
1938
1939 if (!l->bc_peer_is_up)
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001940 return rc;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001941
Jon Paul Maloy7c4a54b2016-09-01 13:52:50 -04001942 l->stats.recv_nacks++;
1943
Jon Paul Maloy52666982015-10-22 08:51:41 -04001944 /* Ignore if peers_snd_nxt goes beyond receive window */
1945 if (more(peers_snd_nxt, l->rcv_nxt + l->window))
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001946 return rc;
1947
LUU Duc Canh31c4f4c2018-11-10 14:23:50 -05001948 rc = tipc_link_retrans(snd_l, l, from, to, xmitq);
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001949
1950 l->snd_nxt = peers_snd_nxt;
1951 if (link_bc_rcv_gap(l))
1952 rc |= TIPC_LINK_SND_STATE;
1953
1954 /* Return now if sender supports nack via STATE messages */
1955 if (l->peer_caps & TIPC_BCAST_STATE_NACK)
1956 return rc;
1957
1958 /* Otherwise, be backwards compatible */
Jon Paul Maloy52666982015-10-22 08:51:41 -04001959
1960 if (!more(peers_snd_nxt, l->rcv_nxt)) {
1961 l->nack_state = BC_NACK_SND_CONDITIONAL;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001962 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001963 }
1964
1965 /* Don't NACK if one was recently sent or peeked */
1966 if (l->nack_state == BC_NACK_SND_SUPPRESS) {
1967 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001968 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001969 }
1970
1971 /* Conditionally delay NACK sending until next synch rcv */
1972 if (l->nack_state == BC_NACK_SND_CONDITIONAL) {
1973 l->nack_state = BC_NACK_SND_UNCONDITIONAL;
1974 if ((peers_snd_nxt - l->rcv_nxt) < TIPC_MIN_LINK_WIN)
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001975 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001976 }
1977
1978 /* Send NACK now but suppress next one */
1979 tipc_link_build_bc_proto_msg(l, true, peers_snd_nxt, xmitq);
1980 l->nack_state = BC_NACK_SND_SUPPRESS;
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04001981 return 0;
Jon Paul Maloy52666982015-10-22 08:51:41 -04001982}
1983
1984void tipc_link_bc_ack_rcv(struct tipc_link *l, u16 acked,
1985 struct sk_buff_head *xmitq)
1986{
1987 struct sk_buff *skb, *tmp;
1988 struct tipc_link *snd_l = l->bc_sndlink;
1989
1990 if (!link_is_up(l) || !l->bc_peer_is_up)
1991 return;
1992
1993 if (!more(acked, l->acked))
1994 return;
1995
Tuong Lien26574db2018-12-19 09:17:57 +07001996 trace_tipc_link_bc_ack(l, l->acked, acked, &snd_l->transmq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04001997 /* Skip over packets peer has already acked */
1998 skb_queue_walk(&snd_l->transmq, skb) {
1999 if (more(buf_seqno(skb), l->acked))
2000 break;
2001 }
2002
2003 /* Update/release the packets peer is acking now */
2004 skb_queue_walk_from_safe(&snd_l->transmq, skb, tmp) {
2005 if (more(buf_seqno(skb), acked))
2006 break;
2007 if (!--TIPC_SKB_CB(skb)->ackers) {
2008 __skb_unlink(skb, &snd_l->transmq);
2009 kfree_skb(skb);
2010 }
2011 }
2012 l->acked = acked;
2013 tipc_link_advance_backlog(snd_l, xmitq);
2014 if (unlikely(!skb_queue_empty(&snd_l->wakeupq)))
2015 link_prepare_wakeup(snd_l);
2016}
2017
2018/* tipc_link_bc_nack_rcv(): receive broadcast nack message
Jon Paul Maloy02d11ca2016-09-01 13:52:49 -04002019 * This function is here for backwards compatibility, since
2020 * no BCAST_PROTOCOL/STATE messages occur from TIPC v2.5.
Jon Paul Maloy52666982015-10-22 08:51:41 -04002021 */
2022int tipc_link_bc_nack_rcv(struct tipc_link *l, struct sk_buff *skb,
2023 struct sk_buff_head *xmitq)
2024{
2025 struct tipc_msg *hdr = buf_msg(skb);
2026 u32 dnode = msg_destnode(hdr);
2027 int mtyp = msg_type(hdr);
2028 u16 acked = msg_bcast_ack(hdr);
2029 u16 from = acked + 1;
2030 u16 to = msg_bcgap_to(hdr);
2031 u16 peers_snd_nxt = to + 1;
2032 int rc = 0;
2033
2034 kfree_skb(skb);
2035
2036 if (!tipc_link_is_up(l) || !l->bc_peer_is_up)
2037 return 0;
2038
2039 if (mtyp != STATE_MSG)
2040 return 0;
2041
Jon Paul Maloye74a3862016-03-03 14:23:21 -05002042 if (dnode == tipc_own_addr(l->net)) {
Jon Paul Maloy52666982015-10-22 08:51:41 -04002043 tipc_link_bc_ack_rcv(l, acked, xmitq);
Jon Paul Maloy40501f902017-08-21 17:59:30 +02002044 rc = tipc_link_retrans(l->bc_sndlink, l, from, to, xmitq);
Jon Paul Maloy52666982015-10-22 08:51:41 -04002045 l->stats.recv_nacks++;
2046 return rc;
2047 }
2048
2049 /* Msg for other node => suppress own NACK at next sync if applicable */
2050 if (more(peers_snd_nxt, l->rcv_nxt) && !less(l->rcv_nxt, from))
2051 l->nack_state = BC_NACK_SND_SUPPRESS;
2052
2053 return 0;
2054}
2055
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -04002056void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057{
Jon Maloy218527f2018-03-29 23:20:41 +02002058 int max_bulk = TIPC_MAX_PUBL / (l->mtu / ITEM_SIZE);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04002059
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -04002060 l->window = win;
Jon Paul Maloy5a0950c2016-08-16 11:53:51 -04002061 l->backlog[TIPC_LOW_IMPORTANCE].limit = max_t(u16, 50, win);
2062 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = max_t(u16, 100, win * 2);
2063 l->backlog[TIPC_HIGH_IMPORTANCE].limit = max_t(u16, 150, win * 3);
2064 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = max_t(u16, 200, win * 4);
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04002065 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066}
2067
Allan Stephens5c216e12011-10-18 11:34:29 -04002068/**
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002069 * link_reset_stats - reset link statistics
Jon Paul Maloy1a906322015-11-19 14:30:47 -05002070 * @l: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071 */
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002072void tipc_link_reset_stats(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073{
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002074 memset(&l->stats, 0, sizeof(l->stats));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075}
2076
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002077static void link_print(struct tipc_link *l, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078{
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002079 struct sk_buff *hskb = skb_peek(&l->transmq);
Jon Paul Maloyc1ab3f1d2015-10-22 08:51:38 -04002080 u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002081 u16 tail = l->snd_nxt - 1;
Ying Xue7a2f7d12014-04-21 10:55:46 +08002082
Jon Paul Maloy662921c2015-07-30 18:24:21 -04002083 pr_info("%s Link <%s> state %x\n", str, l->name, l->state);
Jon Paul Maloy1a20cc22015-07-16 16:54:30 -04002084 pr_info("XMTQ: %u [%u-%u], BKLGQ: %u, SNDNX: %u, RCVNX: %u\n",
2085 skb_queue_len(&l->transmq), head, tail,
2086 skb_queue_len(&l->backlogq), l->snd_nxt, l->rcv_nxt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002087}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002088
2089/* Parse and validate nested (link) properties valid for media, bearer and link
2090 */
2091int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2092{
2093 int err;
2094
2095 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
Johannes Bergfceb6432017-04-12 14:34:07 +02002096 tipc_nl_prop_policy, NULL);
Richard Alpe0655f6a2014-11-20 10:29:07 +01002097 if (err)
2098 return err;
2099
2100 if (props[TIPC_NLA_PROP_PRIO]) {
2101 u32 prio;
2102
2103 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2104 if (prio > TIPC_MAX_LINK_PRI)
2105 return -EINVAL;
2106 }
2107
2108 if (props[TIPC_NLA_PROP_TOL]) {
2109 u32 tol;
2110
2111 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2112 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2113 return -EINVAL;
2114 }
2115
2116 if (props[TIPC_NLA_PROP_WIN]) {
2117 u32 win;
2118
2119 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2120 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2121 return -EINVAL;
2122 }
2123
2124 return 0;
2125}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002126
Richard Alped8182802014-11-24 11:10:29 +01002127static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002128{
2129 int i;
2130 struct nlattr *stats;
2131
2132 struct nla_map {
2133 u32 key;
2134 u32 val;
2135 };
2136
2137 struct nla_map map[] = {
Jon Paul Maloy95901122016-11-25 10:35:02 -05002138 {TIPC_NLA_STATS_RX_INFO, 0},
Richard Alpe7be57fc2014-11-20 10:29:12 +01002139 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2140 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2141 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2142 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
Jon Paul Maloy95901122016-11-25 10:35:02 -05002143 {TIPC_NLA_STATS_TX_INFO, 0},
Richard Alpe7be57fc2014-11-20 10:29:12 +01002144 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2145 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2146 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2147 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2148 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2149 s->msg_length_counts : 1},
2150 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2151 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2152 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2153 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2154 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2155 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2156 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2157 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2158 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2159 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2160 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2161 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2162 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2163 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2164 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2165 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2166 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2167 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2168 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2169 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2170 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2171 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2172 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2173 };
2174
2175 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2176 if (!stats)
2177 return -EMSGSIZE;
2178
2179 for (i = 0; i < ARRAY_SIZE(map); i++)
2180 if (nla_put_u32(skb, map[i].key, map[i].val))
2181 goto msg_full;
2182
2183 nla_nest_end(skb, stats);
2184
2185 return 0;
2186msg_full:
2187 nla_nest_cancel(skb, stats);
2188
2189 return -EMSGSIZE;
2190}
2191
2192/* Caller should hold appropriate locks to protect the link */
Jon Paul Maloy5be9c082015-11-19 14:30:45 -05002193int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2194 struct tipc_link *link, int nlflags)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002195{
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002196 u32 self = tipc_own_addr(net);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002197 struct nlattr *attrs;
2198 struct nlattr *prop;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002199 void *hdr;
2200 int err;
Richard Alpe7be57fc2014-11-20 10:29:12 +01002201
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002202 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Nicolas Dichtelf2f67392015-04-28 18:33:50 +02002203 nlflags, TIPC_NL_LINK_GET);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002204 if (!hdr)
2205 return -EMSGSIZE;
2206
2207 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2208 if (!attrs)
2209 goto msg_full;
2210
2211 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2212 goto attr_msg_full;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002213 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, tipc_cluster_mask(self)))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002214 goto attr_msg_full;
Jon Paul Maloyed193ec2015-04-02 09:33:02 -04002215 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002216 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002217 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->stats.recv_pkts))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002218 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002219 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->stats.sent_pkts))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002220 goto attr_msg_full;
2221
2222 if (tipc_link_is_up(link))
2223 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2224 goto attr_msg_full;
Jon Paul Maloyc72fa872015-10-22 08:51:46 -04002225 if (link->active)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002226 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2227 goto attr_msg_full;
2228
2229 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2230 if (!prop)
2231 goto attr_msg_full;
2232 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2233 goto prop_msg_full;
2234 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2235 goto prop_msg_full;
2236 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04002237 link->window))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002238 goto prop_msg_full;
2239 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2240 goto prop_msg_full;
2241 nla_nest_end(msg->skb, prop);
2242
2243 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2244 if (err)
2245 goto attr_msg_full;
2246
2247 nla_nest_end(msg->skb, attrs);
2248 genlmsg_end(msg->skb, hdr);
2249
2250 return 0;
2251
2252prop_msg_full:
2253 nla_nest_cancel(msg->skb, prop);
2254attr_msg_full:
2255 nla_nest_cancel(msg->skb, attrs);
2256msg_full:
2257 genlmsg_cancel(msg->skb, hdr);
2258
2259 return -EMSGSIZE;
2260}
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002261
2262static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
2263 struct tipc_stats *stats)
2264{
2265 int i;
2266 struct nlattr *nest;
2267
2268 struct nla_map {
2269 __u32 key;
2270 __u32 val;
2271 };
2272
2273 struct nla_map map[] = {
Jon Paul Maloy95901122016-11-25 10:35:02 -05002274 {TIPC_NLA_STATS_RX_INFO, stats->recv_pkts},
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002275 {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
2276 {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
2277 {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
2278 {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
Jon Paul Maloy95901122016-11-25 10:35:02 -05002279 {TIPC_NLA_STATS_TX_INFO, stats->sent_pkts},
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002280 {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
2281 {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
2282 {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
2283 {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
2284 {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
2285 {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
2286 {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
2287 {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
2288 {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
2289 {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
2290 {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
2291 {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
2292 {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
2293 (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
2294 };
2295
2296 nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2297 if (!nest)
2298 return -EMSGSIZE;
2299
2300 for (i = 0; i < ARRAY_SIZE(map); i++)
2301 if (nla_put_u32(skb, map[i].key, map[i].val))
2302 goto msg_full;
2303
2304 nla_nest_end(skb, nest);
2305
2306 return 0;
2307msg_full:
2308 nla_nest_cancel(skb, nest);
2309
2310 return -EMSGSIZE;
2311}
2312
2313int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
2314{
2315 int err;
2316 void *hdr;
2317 struct nlattr *attrs;
2318 struct nlattr *prop;
2319 struct tipc_net *tn = net_generic(net, tipc_net_id);
Hoang Le02ec6ca2019-03-19 18:49:48 +07002320 u32 bc_mode = tipc_bcast_get_broadcast_mode(net);
2321 u32 bc_ratio = tipc_bcast_get_broadcast_ratio(net);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002322 struct tipc_link *bcl = tn->bcl;
2323
2324 if (!bcl)
2325 return 0;
2326
2327 tipc_bcast_lock(net);
2328
2329 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
2330 NLM_F_MULTI, TIPC_NL_LINK_GET);
Insu Yunb53ce3e2016-02-17 11:47:35 -05002331 if (!hdr) {
2332 tipc_bcast_unlock(net);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002333 return -EMSGSIZE;
Insu Yunb53ce3e2016-02-17 11:47:35 -05002334 }
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002335
2336 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2337 if (!attrs)
2338 goto msg_full;
2339
2340 /* The broadcast link is always up */
2341 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2342 goto attr_msg_full;
2343
2344 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
2345 goto attr_msg_full;
2346 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
2347 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002348 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, 0))
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002349 goto attr_msg_full;
Jon Paul Maloy95901122016-11-25 10:35:02 -05002350 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, 0))
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002351 goto attr_msg_full;
2352
2353 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2354 if (!prop)
2355 goto attr_msg_full;
2356 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
2357 goto prop_msg_full;
Hoang Le02ec6ca2019-03-19 18:49:48 +07002358 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST, bc_mode))
2359 goto prop_msg_full;
2360 if (bc_mode & BCLINK_MODE_SEL)
2361 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_BROADCAST_RATIO,
2362 bc_ratio))
2363 goto prop_msg_full;
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002364 nla_nest_end(msg->skb, prop);
2365
2366 err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
2367 if (err)
2368 goto attr_msg_full;
2369
2370 tipc_bcast_unlock(net);
2371 nla_nest_end(msg->skb, attrs);
2372 genlmsg_end(msg->skb, hdr);
2373
2374 return 0;
2375
2376prop_msg_full:
2377 nla_nest_cancel(msg->skb, prop);
2378attr_msg_full:
2379 nla_nest_cancel(msg->skb, attrs);
2380msg_full:
2381 tipc_bcast_unlock(net);
2382 genlmsg_cancel(msg->skb, hdr);
2383
2384 return -EMSGSIZE;
2385}
2386
Richard Alped01332f2016-02-01 08:19:56 +01002387void tipc_link_set_tolerance(struct tipc_link *l, u32 tol,
2388 struct sk_buff_head *xmitq)
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002389{
2390 l->tolerance = tol;
Jon Maloy047491e2018-10-10 17:34:01 +02002391 if (l->bc_rcvlink)
2392 l->bc_rcvlink->tolerance = tol;
Jon Maloy37c64cf2018-02-14 13:34:39 +01002393 if (link_is_up(l))
2394 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, tol, 0, xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002395}
2396
Richard Alped01332f2016-02-01 08:19:56 +01002397void tipc_link_set_prio(struct tipc_link *l, u32 prio,
2398 struct sk_buff_head *xmitq)
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002399{
2400 l->priority = prio;
Jon Maloy8d6e79d2017-11-08 09:59:26 +01002401 tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, prio, xmitq);
Jon Paul Maloy38206d52015-11-19 14:30:46 -05002402}
2403
2404void tipc_link_set_abort_limit(struct tipc_link *l, u32 limit)
2405{
2406 l->abort_limit = limit;
2407}
Tuong Lienb4b97712018-12-19 09:17:56 +07002408
2409char *tipc_link_name_ext(struct tipc_link *l, char *buf)
2410{
2411 if (!l)
2412 scnprintf(buf, TIPC_MAX_LINK_NAME, "null");
2413 else if (link_is_bc_sndlink(l))
2414 scnprintf(buf, TIPC_MAX_LINK_NAME, "broadcast-sender");
2415 else if (link_is_bc_rcvlink(l))
2416 scnprintf(buf, TIPC_MAX_LINK_NAME,
2417 "broadcast-receiver, peer %x", l->addr);
2418 else
2419 memcpy(buf, l->name, TIPC_MAX_LINK_NAME);
2420
2421 return buf;
2422}
2423
2424/**
2425 * tipc_link_dump - dump TIPC link data
2426 * @l: tipc link to be dumped
2427 * @dqueues: bitmask to decide if any link queue to be dumped?
2428 * - TIPC_DUMP_NONE: don't dump link queues
2429 * - TIPC_DUMP_TRANSMQ: dump link transmq queue
2430 * - TIPC_DUMP_BACKLOGQ: dump link backlog queue
2431 * - TIPC_DUMP_DEFERDQ: dump link deferd queue
2432 * - TIPC_DUMP_INPUTQ: dump link input queue
2433 * - TIPC_DUMP_WAKEUP: dump link wakeup queue
2434 * - TIPC_DUMP_ALL: dump all the link queues above
2435 * @buf: returned buffer of dump data in format
2436 */
2437int tipc_link_dump(struct tipc_link *l, u16 dqueues, char *buf)
2438{
2439 int i = 0;
2440 size_t sz = (dqueues) ? LINK_LMAX : LINK_LMIN;
2441 struct sk_buff_head *list;
2442 struct sk_buff *hskb, *tskb;
2443 u32 len;
2444
2445 if (!l) {
2446 i += scnprintf(buf, sz, "link data: (null)\n");
2447 return i;
2448 }
2449
2450 i += scnprintf(buf, sz, "link data: %x", l->addr);
2451 i += scnprintf(buf + i, sz - i, " %x", l->state);
2452 i += scnprintf(buf + i, sz - i, " %u", l->in_session);
2453 i += scnprintf(buf + i, sz - i, " %u", l->session);
2454 i += scnprintf(buf + i, sz - i, " %u", l->peer_session);
2455 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt);
2456 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt);
2457 i += scnprintf(buf + i, sz - i, " %u", l->snd_nxt_state);
2458 i += scnprintf(buf + i, sz - i, " %u", l->rcv_nxt_state);
2459 i += scnprintf(buf + i, sz - i, " %x", l->peer_caps);
2460 i += scnprintf(buf + i, sz - i, " %u", l->silent_intv_cnt);
2461 i += scnprintf(buf + i, sz - i, " %u", l->rst_cnt);
2462 i += scnprintf(buf + i, sz - i, " %u", l->prev_from);
2463 i += scnprintf(buf + i, sz - i, " %u", l->stale_cnt);
2464 i += scnprintf(buf + i, sz - i, " %u", l->acked);
2465
2466 list = &l->transmq;
2467 len = skb_queue_len(list);
2468 hskb = skb_peek(list);
2469 tskb = skb_peek_tail(list);
2470 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2471 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2472 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2473
2474 list = &l->deferdq;
2475 len = skb_queue_len(list);
2476 hskb = skb_peek(list);
2477 tskb = skb_peek_tail(list);
2478 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2479 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2480 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2481
2482 list = &l->backlogq;
2483 len = skb_queue_len(list);
2484 hskb = skb_peek(list);
2485 tskb = skb_peek_tail(list);
2486 i += scnprintf(buf + i, sz - i, " | %u %u %u", len,
2487 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2488 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2489
2490 list = l->inputq;
2491 len = skb_queue_len(list);
2492 hskb = skb_peek(list);
2493 tskb = skb_peek_tail(list);
2494 i += scnprintf(buf + i, sz - i, " | %u %u %u\n", len,
2495 (hskb) ? msg_seqno(buf_msg(hskb)) : 0,
2496 (tskb) ? msg_seqno(buf_msg(tskb)) : 0);
2497
2498 if (dqueues & TIPC_DUMP_TRANSMQ) {
2499 i += scnprintf(buf + i, sz - i, "transmq: ");
2500 i += tipc_list_dump(&l->transmq, false, buf + i);
2501 }
2502 if (dqueues & TIPC_DUMP_BACKLOGQ) {
2503 i += scnprintf(buf + i, sz - i,
2504 "backlogq: <%u %u %u %u %u>, ",
2505 l->backlog[TIPC_LOW_IMPORTANCE].len,
2506 l->backlog[TIPC_MEDIUM_IMPORTANCE].len,
2507 l->backlog[TIPC_HIGH_IMPORTANCE].len,
2508 l->backlog[TIPC_CRITICAL_IMPORTANCE].len,
2509 l->backlog[TIPC_SYSTEM_IMPORTANCE].len);
2510 i += tipc_list_dump(&l->backlogq, false, buf + i);
2511 }
2512 if (dqueues & TIPC_DUMP_DEFERDQ) {
2513 i += scnprintf(buf + i, sz - i, "deferdq: ");
2514 i += tipc_list_dump(&l->deferdq, false, buf + i);
2515 }
2516 if (dqueues & TIPC_DUMP_INPUTQ) {
2517 i += scnprintf(buf + i, sz - i, "inputq: ");
2518 i += tipc_list_dump(l->inputq, false, buf + i);
2519 }
2520 if (dqueues & TIPC_DUMP_WAKEUP) {
2521 i += scnprintf(buf + i, sz - i, "wakeup: ");
2522 i += tipc_list_dump(&l->wakeupq, false, buf + i);
2523 }
2524
2525 return i;
2526}