]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/tipc/link.c
tcp: switch snt_synack back to measuring transmit time of first SYNACK
[mirror_ubuntu-jammy-kernel.git] / net / tipc / link.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/link.c: TIPC link code
c4307285 3 *
170b3927 4 * Copyright (c) 1996-2007, 2012-2014, Ericsson AB
198d73b8 5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
b97bf3fd 19 *
9ea1fd3c
PL
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
b97bf3fd 38#include "link.h"
b97bf3fd 39#include "port.h"
9816f061 40#include "socket.h"
b97bf3fd 41#include "name_distr.h"
b97bf3fd
PL
42#include "discover.h"
43#include "config.h"
b97bf3fd 44
796c75d0
YX
45#include <linux/pkt_sched.h>
46
2cf8aa19
EH
47/*
48 * Error message prefixes
49 */
50static const char *link_co_err = "Link changeover error, ";
51static const char *link_rst_msg = "Resetting link ";
52static const char *link_unk_evt = "Unknown link event ";
b97bf3fd 53
a686e685
AS
54/*
55 * Out-of-range value for link session numbers
56 */
a686e685
AS
57#define INVALID_SESSION 0x10000
58
c4307285
YH
59/*
60 * Link state events:
b97bf3fd 61 */
b97bf3fd
PL
62#define STARTING_EVT 856384768 /* link processing trigger */
63#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
64#define TIMEOUT_EVT 560817u /* link timer expired */
65
c4307285
YH
66/*
67 * The following two 'message types' is really just implementation
68 * data conveniently stored in the message header.
b97bf3fd
PL
69 * They must not be considered part of the protocol
70 */
71#define OPEN_MSG 0
72#define CLOSED_MSG 1
73
c4307285 74/*
b97bf3fd
PL
75 * State value stored in 'exp_msg_count'
76 */
b97bf3fd
PL
77#define START_CHANGEOVER 100000u
78
a18c4bc3 79static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
b97bf3fd 80 struct sk_buff *buf);
247f0f3c 81static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf);
3bb53380 82static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
170b3927 83 struct sk_buff **buf);
a18c4bc3 84static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
a18c4bc3
PG
85static void link_state_event(struct tipc_link *l_ptr, u32 event);
86static void link_reset_statistics(struct tipc_link *l_ptr);
87static void link_print(struct tipc_link *l_ptr, const char *str);
247f0f3c
YX
88static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf);
89static void tipc_link_sync_xmit(struct tipc_link *l);
90static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
31e3c3f6 91
b97bf3fd 92/*
05790c64 93 * Simple link routines
b97bf3fd 94 */
05790c64 95static unsigned int align(unsigned int i)
b97bf3fd
PL
96{
97 return (i + 3) & ~3u;
98}
99
a18c4bc3 100static void link_init_max_pkt(struct tipc_link *l_ptr)
b97bf3fd 101{
7a2f7d18 102 struct tipc_bearer *b_ptr;
b97bf3fd 103 u32 max_pkt;
c4307285 104
7a2f7d18
YX
105 rcu_read_lock();
106 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
107 if (!b_ptr) {
108 rcu_read_unlock();
109 return;
110 }
111 max_pkt = (b_ptr->mtu & ~3);
112 rcu_read_unlock();
113
b97bf3fd
PL
114 if (max_pkt > MAX_MSG_SIZE)
115 max_pkt = MAX_MSG_SIZE;
116
c4307285 117 l_ptr->max_pkt_target = max_pkt;
b97bf3fd
PL
118 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
119 l_ptr->max_pkt = l_ptr->max_pkt_target;
c4307285 120 else
b97bf3fd
PL
121 l_ptr->max_pkt = MAX_PKT_DEFAULT;
122
c4307285 123 l_ptr->max_pkt_probes = 0;
b97bf3fd
PL
124}
125
a18c4bc3 126static u32 link_next_sent(struct tipc_link *l_ptr)
b97bf3fd
PL
127{
128 if (l_ptr->next_out)
f905730c 129 return buf_seqno(l_ptr->next_out);
b97bf3fd
PL
130 return mod(l_ptr->next_out_no);
131}
132
a18c4bc3 133static u32 link_last_sent(struct tipc_link *l_ptr)
b97bf3fd
PL
134{
135 return mod(link_next_sent(l_ptr) - 1);
136}
137
138/*
05790c64 139 * Simple non-static link routines (i.e. referenced outside this file)
b97bf3fd 140 */
a18c4bc3 141int tipc_link_is_up(struct tipc_link *l_ptr)
b97bf3fd
PL
142{
143 if (!l_ptr)
144 return 0;
a02cec21 145 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
b97bf3fd
PL
146}
147
a18c4bc3 148int tipc_link_is_active(struct tipc_link *l_ptr)
b97bf3fd 149{
a02cec21
ED
150 return (l_ptr->owner->active_links[0] == l_ptr) ||
151 (l_ptr->owner->active_links[1] == l_ptr);
b97bf3fd
PL
152}
153
b97bf3fd
PL
154/**
155 * link_timeout - handle expiration of link timer
156 * @l_ptr: pointer to link
b97bf3fd 157 */
a18c4bc3 158static void link_timeout(struct tipc_link *l_ptr)
b97bf3fd 159{
4323add6 160 tipc_node_lock(l_ptr->owner);
b97bf3fd
PL
161
162 /* update counters used in statistical profiling of send traffic */
b97bf3fd
PL
163 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
164 l_ptr->stats.queue_sz_counts++;
165
b97bf3fd
PL
166 if (l_ptr->first_out) {
167 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
168 u32 length = msg_size(msg);
169
f64f9e71
JP
170 if ((msg_user(msg) == MSG_FRAGMENTER) &&
171 (msg_type(msg) == FIRST_FRAGMENT)) {
b97bf3fd
PL
172 length = msg_size(msg_get_wrapped(msg));
173 }
174 if (length) {
175 l_ptr->stats.msg_lengths_total += length;
176 l_ptr->stats.msg_length_counts++;
177 if (length <= 64)
178 l_ptr->stats.msg_length_profile[0]++;
179 else if (length <= 256)
180 l_ptr->stats.msg_length_profile[1]++;
181 else if (length <= 1024)
182 l_ptr->stats.msg_length_profile[2]++;
183 else if (length <= 4096)
184 l_ptr->stats.msg_length_profile[3]++;
185 else if (length <= 16384)
186 l_ptr->stats.msg_length_profile[4]++;
187 else if (length <= 32768)
188 l_ptr->stats.msg_length_profile[5]++;
189 else
190 l_ptr->stats.msg_length_profile[6]++;
191 }
192 }
193
194 /* do all other link processing performed on a periodic basis */
b97bf3fd
PL
195
196 link_state_event(l_ptr, TIMEOUT_EVT);
197
198 if (l_ptr->next_out)
4323add6 199 tipc_link_push_queue(l_ptr);
b97bf3fd 200
4323add6 201 tipc_node_unlock(l_ptr->owner);
b97bf3fd
PL
202}
203
a18c4bc3 204static void link_set_timer(struct tipc_link *l_ptr, u32 time)
b97bf3fd
PL
205{
206 k_start_timer(&l_ptr->timer, time);
207}
208
209/**
4323add6 210 * tipc_link_create - create a new link
37b9c08a 211 * @n_ptr: pointer to associated node
b97bf3fd 212 * @b_ptr: pointer to associated bearer
b97bf3fd 213 * @media_addr: media address to use when sending messages over link
c4307285 214 *
b97bf3fd
PL
215 * Returns pointer to link.
216 */
a18c4bc3 217struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
c61dd61d
YX
218 struct tipc_bearer *b_ptr,
219 const struct tipc_media_addr *media_addr)
b97bf3fd 220{
a18c4bc3 221 struct tipc_link *l_ptr;
b97bf3fd
PL
222 struct tipc_msg *msg;
223 char *if_name;
37b9c08a
AS
224 char addr_string[16];
225 u32 peer = n_ptr->addr;
226
227 if (n_ptr->link_cnt >= 2) {
228 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19 229 pr_err("Attempt to establish third link to %s\n", addr_string);
37b9c08a
AS
230 return NULL;
231 }
232
233 if (n_ptr->links[b_ptr->identity]) {
234 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19
EH
235 pr_err("Attempt to establish second link on <%s> to %s\n",
236 b_ptr->name, addr_string);
37b9c08a
AS
237 return NULL;
238 }
b97bf3fd 239
0da974f4 240 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
b97bf3fd 241 if (!l_ptr) {
2cf8aa19 242 pr_warn("Link creation failed, no memory\n");
b97bf3fd
PL
243 return NULL;
244 }
b97bf3fd
PL
245
246 l_ptr->addr = peer;
2d627b92 247 if_name = strchr(b_ptr->name, ':') + 1;
062b4c99 248 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
b97bf3fd 249 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
c4307285 250 tipc_node(tipc_own_addr),
b97bf3fd
PL
251 if_name,
252 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
062b4c99 253 /* note: peer i/f name is updated by reset/activate message */
b97bf3fd 254 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
37b9c08a 255 l_ptr->owner = n_ptr;
b97bf3fd 256 l_ptr->checkpoint = 1;
f882cb76 257 l_ptr->peer_session = INVALID_SESSION;
7a2f7d18 258 l_ptr->bearer_id = b_ptr->identity;
5c216e1d 259 link_set_supervision_props(l_ptr, b_ptr->tolerance);
b97bf3fd
PL
260 l_ptr->state = RESET_UNKNOWN;
261
262 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
263 msg = l_ptr->pmsg;
c68ca7b7 264 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd 265 msg_set_size(msg, sizeof(l_ptr->proto_msg));
a686e685 266 msg_set_session(msg, (tipc_random & 0xffff));
b97bf3fd
PL
267 msg_set_bearer_id(msg, b_ptr->identity);
268 strcpy((char *)msg_data(msg), if_name);
269
270 l_ptr->priority = b_ptr->priority;
5c216e1d 271 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
b97bf3fd 272
7a2f7d18 273 l_ptr->net_plane = b_ptr->net_plane;
b97bf3fd
PL
274 link_init_max_pkt(l_ptr);
275
276 l_ptr->next_out_no = 1;
277 INIT_LIST_HEAD(&l_ptr->waiting_ports);
278
279 link_reset_statistics(l_ptr);
280
37b9c08a 281 tipc_node_attach_link(n_ptr, l_ptr);
b97bf3fd 282
170b3927
JPM
283 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
284 (unsigned long)l_ptr);
581465fa
JPM
285
286 link_state_event(l_ptr, STARTING_EVT);
b97bf3fd 287
b97bf3fd
PL
288 return l_ptr;
289}
290
7d33939f 291void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
8d8439b6
YX
292{
293 struct tipc_link *l_ptr;
c61dd61d 294 struct tipc_node *n_ptr;
8d8439b6 295
6c7a762e
YX
296 rcu_read_lock();
297 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
5356f3d7 298 tipc_node_lock(n_ptr);
c61dd61d
YX
299 l_ptr = n_ptr->links[bearer_id];
300 if (l_ptr) {
301 tipc_link_reset(l_ptr);
7d33939f
JPM
302 if (shutting_down || !tipc_node_is_up(n_ptr)) {
303 tipc_node_detach_link(l_ptr->owner, l_ptr);
304 tipc_link_reset_fragments(l_ptr);
5356f3d7 305 tipc_node_unlock(n_ptr);
7d33939f
JPM
306
307 /* Nobody else can access this link now: */
308 del_timer_sync(&l_ptr->timer);
309 kfree(l_ptr);
310 } else {
311 /* Detach/delete when failover is finished: */
312 l_ptr->flags |= LINK_STOPPED;
5356f3d7 313 tipc_node_unlock(n_ptr);
7d33939f
JPM
314 del_timer_sync(&l_ptr->timer);
315 }
c61dd61d
YX
316 continue;
317 }
5356f3d7 318 tipc_node_unlock(n_ptr);
8d8439b6 319 }
6c7a762e 320 rcu_read_unlock();
8d8439b6 321}
b97bf3fd
PL
322
323/**
c4307285 324 * link_schedule_port - schedule port for deferred sending
b97bf3fd
PL
325 * @l_ptr: pointer to link
326 * @origport: reference to sending port
327 * @sz: amount of data to be sent
c4307285
YH
328 *
329 * Schedules port for renewed sending of messages after link congestion
b97bf3fd
PL
330 * has abated.
331 */
a18c4bc3 332static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
b97bf3fd 333{
23dd4cce 334 struct tipc_port *p_ptr;
60120526 335 struct tipc_sock *tsk;
b97bf3fd 336
4323add6
PL
337 spin_lock_bh(&tipc_port_list_lock);
338 p_ptr = tipc_port_lock(origport);
b97bf3fd 339 if (p_ptr) {
b97bf3fd
PL
340 if (!list_empty(&p_ptr->wait_list))
341 goto exit;
60120526
JPM
342 tsk = tipc_port_to_sock(p_ptr);
343 tsk->link_cong = 1;
15e979da 344 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
b97bf3fd
PL
345 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
346 l_ptr->stats.link_congs++;
347exit:
4323add6 348 tipc_port_unlock(p_ptr);
b97bf3fd 349 }
4323add6 350 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
351 return -ELINKCONG;
352}
353
a18c4bc3 354void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
b97bf3fd 355{
23dd4cce 356 struct tipc_port *p_ptr;
60120526 357 struct tipc_sock *tsk;
23dd4cce 358 struct tipc_port *temp_p_ptr;
b97bf3fd
PL
359 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
360
361 if (all)
362 win = 100000;
363 if (win <= 0)
364 return;
4323add6 365 if (!spin_trylock_bh(&tipc_port_list_lock))
b97bf3fd
PL
366 return;
367 if (link_congested(l_ptr))
368 goto exit;
c4307285 369 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
b97bf3fd
PL
370 wait_list) {
371 if (win <= 0)
372 break;
60120526 373 tsk = tipc_port_to_sock(p_ptr);
b97bf3fd 374 list_del_init(&p_ptr->wait_list);
23dd4cce 375 spin_lock_bh(p_ptr->lock);
60120526
JPM
376 tsk->link_cong = 0;
377 tipc_sock_wakeup(tsk);
b97bf3fd 378 win -= p_ptr->waiting_pkts;
23dd4cce 379 spin_unlock_bh(p_ptr->lock);
b97bf3fd
PL
380 }
381
382exit:
4323add6 383 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd
PL
384}
385
c4307285 386/**
b97bf3fd
PL
387 * link_release_outqueue - purge link's outbound message queue
388 * @l_ptr: pointer to link
389 */
a18c4bc3 390static void link_release_outqueue(struct tipc_link *l_ptr)
b97bf3fd 391{
d77b3831 392 kfree_skb_list(l_ptr->first_out);
b97bf3fd
PL
393 l_ptr->first_out = NULL;
394 l_ptr->out_queue_size = 0;
395}
396
397/**
4323add6 398 * tipc_link_reset_fragments - purge link's inbound message fragments queue
b97bf3fd
PL
399 * @l_ptr: pointer to link
400 */
a18c4bc3 401void tipc_link_reset_fragments(struct tipc_link *l_ptr)
b97bf3fd 402{
37e22164
JPM
403 kfree_skb(l_ptr->reasm_buf);
404 l_ptr->reasm_buf = NULL;
b97bf3fd
PL
405}
406
c4307285 407/**
581465fa 408 * tipc_link_purge_queues - purge all pkt queues associated with link
b97bf3fd
PL
409 * @l_ptr: pointer to link
410 */
581465fa 411void tipc_link_purge_queues(struct tipc_link *l_ptr)
b97bf3fd 412{
d77b3831
YX
413 kfree_skb_list(l_ptr->oldest_deferred_in);
414 kfree_skb_list(l_ptr->first_out);
4323add6 415 tipc_link_reset_fragments(l_ptr);
5f6d9123 416 kfree_skb(l_ptr->proto_msg_queue);
b97bf3fd
PL
417 l_ptr->proto_msg_queue = NULL;
418}
419
a18c4bc3 420void tipc_link_reset(struct tipc_link *l_ptr)
b97bf3fd 421{
b97bf3fd
PL
422 u32 prev_state = l_ptr->state;
423 u32 checkpoint = l_ptr->next_in_no;
5392d646 424 int was_active_link = tipc_link_is_active(l_ptr);
c4307285 425
a686e685 426 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
b97bf3fd 427
a686e685
AS
428 /* Link is down, accept any session */
429 l_ptr->peer_session = INVALID_SESSION;
b97bf3fd 430
c4307285 431 /* Prepare for max packet size negotiation */
b97bf3fd 432 link_init_max_pkt(l_ptr);
c4307285 433
b97bf3fd 434 l_ptr->state = RESET_UNKNOWN;
b97bf3fd
PL
435
436 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
437 return;
438
4323add6 439 tipc_node_link_down(l_ptr->owner, l_ptr);
7a2f7d18 440 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
7368ddf1 441
b9d4c339 442 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
b97bf3fd
PL
443 l_ptr->reset_checkpoint = checkpoint;
444 l_ptr->exp_msg_count = START_CHANGEOVER;
445 }
446
447 /* Clean up all queues: */
b97bf3fd 448 link_release_outqueue(l_ptr);
5f6d9123 449 kfree_skb(l_ptr->proto_msg_queue);
b97bf3fd 450 l_ptr->proto_msg_queue = NULL;
d77b3831 451 kfree_skb_list(l_ptr->oldest_deferred_in);
b97bf3fd 452 if (!list_empty(&l_ptr->waiting_ports))
4323add6 453 tipc_link_wakeup_ports(l_ptr, 1);
b97bf3fd
PL
454
455 l_ptr->retransm_queue_head = 0;
456 l_ptr->retransm_queue_size = 0;
457 l_ptr->last_out = NULL;
458 l_ptr->first_out = NULL;
459 l_ptr->next_out = NULL;
460 l_ptr->unacked_window = 0;
461 l_ptr->checkpoint = 1;
462 l_ptr->next_out_no = 1;
463 l_ptr->deferred_inqueue_sz = 0;
464 l_ptr->oldest_deferred_in = NULL;
465 l_ptr->newest_deferred_in = NULL;
466 l_ptr->fsm_msg_cnt = 0;
467 l_ptr->stale_count = 0;
468 link_reset_statistics(l_ptr);
b97bf3fd
PL
469}
470
c61dd61d 471void tipc_link_reset_list(unsigned int bearer_id)
e0ca2c30
YX
472{
473 struct tipc_link *l_ptr;
c61dd61d 474 struct tipc_node *n_ptr;
e0ca2c30 475
6c7a762e
YX
476 rcu_read_lock();
477 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
5356f3d7 478 tipc_node_lock(n_ptr);
c61dd61d
YX
479 l_ptr = n_ptr->links[bearer_id];
480 if (l_ptr)
481 tipc_link_reset(l_ptr);
5356f3d7 482 tipc_node_unlock(n_ptr);
e0ca2c30 483 }
6c7a762e 484 rcu_read_unlock();
e0ca2c30 485}
b97bf3fd 486
a18c4bc3 487static void link_activate(struct tipc_link *l_ptr)
b97bf3fd 488{
5392d646 489 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
4323add6 490 tipc_node_link_up(l_ptr->owner, l_ptr);
7a2f7d18 491 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
b97bf3fd
PL
492}
493
494/**
495 * link_state_event - link finite state machine
496 * @l_ptr: pointer to link
497 * @event: state machine event to process
498 */
95c96174 499static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
b97bf3fd 500{
a18c4bc3 501 struct tipc_link *other;
b97bf3fd
PL
502 u32 cont_intv = l_ptr->continuity_interval;
503
7d33939f
JPM
504 if (l_ptr->flags & LINK_STOPPED)
505 return;
506
135daee6 507 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
b97bf3fd
PL
508 return; /* Not yet. */
509
77a7e07a
YX
510 /* Check whether changeover is going on */
511 if (l_ptr->exp_msg_count) {
a016892c 512 if (event == TIMEOUT_EVT)
b97bf3fd 513 link_set_timer(l_ptr, cont_intv);
77a7e07a 514 return;
b97bf3fd 515 }
b97bf3fd
PL
516
517 switch (l_ptr->state) {
518 case WORKING_WORKING:
b97bf3fd
PL
519 switch (event) {
520 case TRAFFIC_MSG_EVT:
b97bf3fd 521 case ACTIVATE_MSG:
b97bf3fd
PL
522 break;
523 case TIMEOUT_EVT:
b97bf3fd
PL
524 if (l_ptr->next_in_no != l_ptr->checkpoint) {
525 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 526 if (tipc_bclink_acks_missing(l_ptr->owner)) {
247f0f3c
YX
527 tipc_link_proto_xmit(l_ptr, STATE_MSG,
528 0, 0, 0, 0, 0);
b97bf3fd
PL
529 l_ptr->fsm_msg_cnt++;
530 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
247f0f3c
YX
531 tipc_link_proto_xmit(l_ptr, STATE_MSG,
532 1, 0, 0, 0, 0);
b97bf3fd
PL
533 l_ptr->fsm_msg_cnt++;
534 }
535 link_set_timer(l_ptr, cont_intv);
536 break;
537 }
b97bf3fd
PL
538 l_ptr->state = WORKING_UNKNOWN;
539 l_ptr->fsm_msg_cnt = 0;
247f0f3c 540 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd
PL
541 l_ptr->fsm_msg_cnt++;
542 link_set_timer(l_ptr, cont_intv / 4);
543 break;
544 case RESET_MSG:
2cf8aa19
EH
545 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
546 l_ptr->name);
4323add6 547 tipc_link_reset(l_ptr);
b97bf3fd
PL
548 l_ptr->state = RESET_RESET;
549 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
550 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
551 0, 0, 0, 0, 0);
b97bf3fd
PL
552 l_ptr->fsm_msg_cnt++;
553 link_set_timer(l_ptr, cont_intv);
554 break;
555 default:
2cf8aa19 556 pr_err("%s%u in WW state\n", link_unk_evt, event);
b97bf3fd
PL
557 }
558 break;
559 case WORKING_UNKNOWN:
b97bf3fd
PL
560 switch (event) {
561 case TRAFFIC_MSG_EVT:
b97bf3fd 562 case ACTIVATE_MSG:
b97bf3fd
PL
563 l_ptr->state = WORKING_WORKING;
564 l_ptr->fsm_msg_cnt = 0;
565 link_set_timer(l_ptr, cont_intv);
566 break;
567 case RESET_MSG:
2cf8aa19
EH
568 pr_info("%s<%s>, requested by peer while probing\n",
569 link_rst_msg, l_ptr->name);
4323add6 570 tipc_link_reset(l_ptr);
b97bf3fd
PL
571 l_ptr->state = RESET_RESET;
572 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
573 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
574 0, 0, 0, 0, 0);
b97bf3fd
PL
575 l_ptr->fsm_msg_cnt++;
576 link_set_timer(l_ptr, cont_intv);
577 break;
578 case TIMEOUT_EVT:
b97bf3fd 579 if (l_ptr->next_in_no != l_ptr->checkpoint) {
b97bf3fd
PL
580 l_ptr->state = WORKING_WORKING;
581 l_ptr->fsm_msg_cnt = 0;
582 l_ptr->checkpoint = l_ptr->next_in_no;
4323add6 583 if (tipc_bclink_acks_missing(l_ptr->owner)) {
247f0f3c
YX
584 tipc_link_proto_xmit(l_ptr, STATE_MSG,
585 0, 0, 0, 0, 0);
b97bf3fd
PL
586 l_ptr->fsm_msg_cnt++;
587 }
588 link_set_timer(l_ptr, cont_intv);
589 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
247f0f3c
YX
590 tipc_link_proto_xmit(l_ptr, STATE_MSG,
591 1, 0, 0, 0, 0);
b97bf3fd
PL
592 l_ptr->fsm_msg_cnt++;
593 link_set_timer(l_ptr, cont_intv / 4);
594 } else { /* Link has failed */
2cf8aa19
EH
595 pr_warn("%s<%s>, peer not responding\n",
596 link_rst_msg, l_ptr->name);
4323add6 597 tipc_link_reset(l_ptr);
b97bf3fd
PL
598 l_ptr->state = RESET_UNKNOWN;
599 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
600 tipc_link_proto_xmit(l_ptr, RESET_MSG,
601 0, 0, 0, 0, 0);
b97bf3fd
PL
602 l_ptr->fsm_msg_cnt++;
603 link_set_timer(l_ptr, cont_intv);
604 }
605 break;
606 default:
2cf8aa19 607 pr_err("%s%u in WU state\n", link_unk_evt, event);
b97bf3fd
PL
608 }
609 break;
610 case RESET_UNKNOWN:
b97bf3fd
PL
611 switch (event) {
612 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
613 break;
614 case ACTIVATE_MSG:
615 other = l_ptr->owner->active_links[0];
8d64a5ba 616 if (other && link_working_unknown(other))
b97bf3fd 617 break;
b97bf3fd
PL
618 l_ptr->state = WORKING_WORKING;
619 l_ptr->fsm_msg_cnt = 0;
620 link_activate(l_ptr);
247f0f3c 621 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 622 l_ptr->fsm_msg_cnt++;
c64f7a6a 623 if (l_ptr->owner->working_links == 1)
247f0f3c 624 tipc_link_sync_xmit(l_ptr);
b97bf3fd
PL
625 link_set_timer(l_ptr, cont_intv);
626 break;
627 case RESET_MSG:
b97bf3fd
PL
628 l_ptr->state = RESET_RESET;
629 l_ptr->fsm_msg_cnt = 0;
247f0f3c
YX
630 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
631 1, 0, 0, 0, 0);
b97bf3fd
PL
632 l_ptr->fsm_msg_cnt++;
633 link_set_timer(l_ptr, cont_intv);
634 break;
635 case STARTING_EVT:
135daee6 636 l_ptr->flags |= LINK_STARTED;
b97bf3fd
PL
637 /* fall through */
638 case TIMEOUT_EVT:
247f0f3c 639 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
640 l_ptr->fsm_msg_cnt++;
641 link_set_timer(l_ptr, cont_intv);
642 break;
643 default:
2cf8aa19 644 pr_err("%s%u in RU state\n", link_unk_evt, event);
b97bf3fd
PL
645 }
646 break;
647 case RESET_RESET:
b97bf3fd
PL
648 switch (event) {
649 case TRAFFIC_MSG_EVT:
b97bf3fd
PL
650 case ACTIVATE_MSG:
651 other = l_ptr->owner->active_links[0];
8d64a5ba 652 if (other && link_working_unknown(other))
b97bf3fd 653 break;
b97bf3fd
PL
654 l_ptr->state = WORKING_WORKING;
655 l_ptr->fsm_msg_cnt = 0;
656 link_activate(l_ptr);
247f0f3c 657 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
b97bf3fd 658 l_ptr->fsm_msg_cnt++;
c64f7a6a 659 if (l_ptr->owner->working_links == 1)
247f0f3c 660 tipc_link_sync_xmit(l_ptr);
b97bf3fd
PL
661 link_set_timer(l_ptr, cont_intv);
662 break;
663 case RESET_MSG:
b97bf3fd
PL
664 break;
665 case TIMEOUT_EVT:
247f0f3c
YX
666 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
667 0, 0, 0, 0, 0);
b97bf3fd
PL
668 l_ptr->fsm_msg_cnt++;
669 link_set_timer(l_ptr, cont_intv);
b97bf3fd
PL
670 break;
671 default:
2cf8aa19 672 pr_err("%s%u in RR state\n", link_unk_evt, event);
b97bf3fd
PL
673 }
674 break;
675 default:
2cf8aa19 676 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
b97bf3fd
PL
677 }
678}
679
680/*
681 * link_bundle_buf(): Append contents of a buffer to
c4307285 682 * the tail of an existing one.
b97bf3fd 683 */
ae8509c4 684static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
b97bf3fd
PL
685 struct sk_buff *buf)
686{
687 struct tipc_msg *bundler_msg = buf_msg(bundler);
688 struct tipc_msg *msg = buf_msg(buf);
689 u32 size = msg_size(msg);
e49060c7
AS
690 u32 bundle_size = msg_size(bundler_msg);
691 u32 to_pos = align(bundle_size);
692 u32 pad = to_pos - bundle_size;
b97bf3fd
PL
693
694 if (msg_user(bundler_msg) != MSG_BUNDLER)
695 return 0;
696 if (msg_type(bundler_msg) != OPEN_MSG)
697 return 0;
e49060c7 698 if (skb_tailroom(bundler) < (pad + size))
b97bf3fd 699 return 0;
15e979da 700 if (l_ptr->max_pkt < (to_pos + size))
863fae66 701 return 0;
b97bf3fd 702
e49060c7 703 skb_put(bundler, pad + size);
27d7ff46 704 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
b97bf3fd
PL
705 msg_set_size(bundler_msg, to_pos + size);
706 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
5f6d9123 707 kfree_skb(buf);
b97bf3fd
PL
708 l_ptr->stats.sent_bundled++;
709 return 1;
710}
711
a18c4bc3 712static void link_add_to_outqueue(struct tipc_link *l_ptr,
05790c64
SR
713 struct sk_buff *buf,
714 struct tipc_msg *msg)
b97bf3fd
PL
715{
716 u32 ack = mod(l_ptr->next_in_no - 1);
717 u32 seqno = mod(l_ptr->next_out_no++);
718
719 msg_set_word(msg, 2, ((ack << 16) | seqno));
720 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
721 buf->next = NULL;
722 if (l_ptr->first_out) {
723 l_ptr->last_out->next = buf;
724 l_ptr->last_out = buf;
725 } else
726 l_ptr->first_out = l_ptr->last_out = buf;
9bd80b60 727
b97bf3fd 728 l_ptr->out_queue_size++;
9bd80b60
AS
729 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
730 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
b97bf3fd
PL
731}
732
a18c4bc3 733static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
dc63d91e
AS
734 struct sk_buff *buf_chain,
735 u32 long_msgno)
736{
737 struct sk_buff *buf;
738 struct tipc_msg *msg;
739
740 if (!l_ptr->next_out)
741 l_ptr->next_out = buf_chain;
742 while (buf_chain) {
743 buf = buf_chain;
744 buf_chain = buf_chain->next;
745
746 msg = buf_msg(buf);
747 msg_set_long_msgno(msg, long_msgno);
748 link_add_to_outqueue(l_ptr, buf, msg);
749 }
750}
751
c4307285 752/*
247f0f3c
YX
753 * tipc_link_xmit() is the 'full path' for messages, called from
754 * inside TIPC when the 'fast path' in tipc_send_xmit
b97bf3fd
PL
755 * has failed, and from link_send()
756 */
247f0f3c 757int __tipc_link_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
b97bf3fd
PL
758{
759 struct tipc_msg *msg = buf_msg(buf);
760 u32 size = msg_size(msg);
761 u32 dsz = msg_data_sz(msg);
762 u32 queue_size = l_ptr->out_queue_size;
c68ca7b7 763 u32 imp = tipc_msg_tot_importance(msg);
b97bf3fd 764 u32 queue_limit = l_ptr->queue_limit[imp];
15e979da 765 u32 max_packet = l_ptr->max_pkt;
b97bf3fd 766
b97bf3fd 767 /* Match msg importance against queue limits: */
b97bf3fd
PL
768 if (unlikely(queue_size >= queue_limit)) {
769 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
bebc55ae 770 link_schedule_port(l_ptr, msg_origport(msg), size);
5f6d9123 771 kfree_skb(buf);
bebc55ae 772 return -ELINKCONG;
b97bf3fd 773 }
5f6d9123 774 kfree_skb(buf);
b97bf3fd 775 if (imp > CONN_MANAGER) {
2cf8aa19
EH
776 pr_warn("%s<%s>, send queue full", link_rst_msg,
777 l_ptr->name);
4323add6 778 tipc_link_reset(l_ptr);
b97bf3fd
PL
779 }
780 return dsz;
781 }
782
783 /* Fragmentation needed ? */
b97bf3fd 784 if (size > max_packet)
247f0f3c 785 return tipc_link_frag_xmit(l_ptr, buf);
b97bf3fd 786
617d3c7a 787 /* Packet can be queued or sent. */
512137ee 788 if (likely(!link_congested(l_ptr))) {
b97bf3fd
PL
789 link_add_to_outqueue(l_ptr, buf, msg);
790
7a2f7d18 791 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
3c294cb3 792 l_ptr->unacked_window = 0;
b97bf3fd
PL
793 return dsz;
794 }
617d3c7a 795 /* Congestion: can message be bundled ? */
b97bf3fd
PL
796 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
797 (msg_user(msg) != MSG_FRAGMENTER)) {
798
799 /* Try adding message to an existing bundle */
c4307285 800 if (l_ptr->next_out &&
3c294cb3 801 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
b97bf3fd 802 return dsz;
b97bf3fd
PL
803
804 /* Try creating a new bundle */
b97bf3fd 805 if (size <= max_packet * 2 / 3) {
31e3c3f6 806 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
b97bf3fd
PL
807 struct tipc_msg bundler_hdr;
808
809 if (bundler) {
c68ca7b7 810 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
75715217 811 INT_H_SIZE, l_ptr->addr);
27d7ff46
ACM
812 skb_copy_to_linear_data(bundler, &bundler_hdr,
813 INT_H_SIZE);
b97bf3fd
PL
814 skb_trim(bundler, INT_H_SIZE);
815 link_bundle_buf(l_ptr, bundler, buf);
816 buf = bundler;
817 msg = buf_msg(buf);
818 l_ptr->stats.sent_bundles++;
819 }
820 }
821 }
822 if (!l_ptr->next_out)
823 l_ptr->next_out = buf;
824 link_add_to_outqueue(l_ptr, buf, msg);
b97bf3fd
PL
825 return dsz;
826}
827
c4307285 828/*
247f0f3c
YX
829 * tipc_link_xmit(): same as __tipc_link_xmit(), but the link to use
830 * has not been selected yet, and the the owner node is not locked
b97bf3fd
PL
831 * Called by TIPC internal users, e.g. the name distributor
832 */
247f0f3c 833int tipc_link_xmit(struct sk_buff *buf, u32 dest, u32 selector)
b97bf3fd 834{
a18c4bc3 835 struct tipc_link *l_ptr;
6c00055a 836 struct tipc_node *n_ptr;
b97bf3fd
PL
837 int res = -ELINKCONG;
838
51a8e4de 839 n_ptr = tipc_node_find(dest);
b97bf3fd 840 if (n_ptr) {
4323add6 841 tipc_node_lock(n_ptr);
b97bf3fd 842 l_ptr = n_ptr->active_links[selector & 1];
a016892c 843 if (l_ptr)
247f0f3c 844 res = __tipc_link_xmit(l_ptr, buf);
a016892c 845 else
5f6d9123 846 kfree_skb(buf);
4323add6 847 tipc_node_unlock(n_ptr);
b97bf3fd 848 } else {
5f6d9123 849 kfree_skb(buf);
b97bf3fd 850 }
b97bf3fd
PL
851 return res;
852}
853
4f1688b2
JPM
854/* tipc_link_cong: determine return value and how to treat the
855 * sent buffer during link congestion.
856 * - For plain, errorless user data messages we keep the buffer and
857 * return -ELINKONG.
858 * - For all other messages we discard the buffer and return -EHOSTUNREACH
859 * - For TIPC internal messages we also reset the link
860 */
861static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
862{
863 struct tipc_msg *msg = buf_msg(buf);
864 uint psz = msg_size(msg);
865 uint imp = tipc_msg_tot_importance(msg);
866 u32 oport = msg_tot_origport(msg);
867
868 if (likely(imp <= TIPC_CRITICAL_IMPORTANCE)) {
869 if (!msg_errcode(msg) && !msg_reroute_cnt(msg)) {
870 link_schedule_port(link, oport, psz);
871 return -ELINKCONG;
872 }
873 } else {
874 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
875 tipc_link_reset(link);
876 }
877 kfree_skb_list(buf);
878 return -EHOSTUNREACH;
879}
880
881/**
882 * __tipc_link_xmit2(): same as tipc_link_xmit2, but destlink is known & locked
883 * @link: link to use
884 * @buf: chain of buffers containing message
885 * Consumes the buffer chain, except when returning -ELINKCONG
886 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
887 * user data messages) or -EHOSTUNREACH (all other messages/senders)
888 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
889 * to act on the return value, since they may need to do more send attempts.
890 */
891int __tipc_link_xmit2(struct tipc_link *link, struct sk_buff *buf)
892{
893 struct tipc_msg *msg = buf_msg(buf);
894 uint psz = msg_size(msg);
895 uint qsz = link->out_queue_size;
896 uint sndlim = link->queue_limit[0];
897 uint imp = tipc_msg_tot_importance(msg);
898 uint mtu = link->max_pkt;
899 uint ack = mod(link->next_in_no - 1);
900 uint seqno = link->next_out_no;
901 uint bc_last_in = link->owner->bclink.last_in;
902 struct tipc_media_addr *addr = &link->media_addr;
903 struct sk_buff *next = buf->next;
904
905 /* Match queue limits against msg importance: */
906 if (unlikely(qsz >= link->queue_limit[imp]))
907 return tipc_link_cong(link, buf);
908
909 /* Has valid packet limit been used ? */
910 if (unlikely(psz > mtu)) {
911 kfree_skb_list(buf);
912 return -EMSGSIZE;
913 }
914
915 /* Prepare each packet for sending, and add to outqueue: */
916 while (buf) {
917 next = buf->next;
918 msg = buf_msg(buf);
919 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
920 msg_set_bcast_ack(msg, bc_last_in);
921
922 if (!link->first_out) {
923 link->first_out = buf;
924 } else if (qsz < sndlim) {
925 link->last_out->next = buf;
926 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
927 link->stats.sent_bundled++;
928 buf = next;
929 next = buf->next;
930 continue;
931 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
932 link->stats.sent_bundled++;
933 link->stats.sent_bundles++;
934 link->last_out->next = buf;
935 if (!link->next_out)
936 link->next_out = buf;
937 } else {
938 link->last_out->next = buf;
939 if (!link->next_out)
940 link->next_out = buf;
941 }
942
943 /* Send packet if possible: */
944 if (likely(++qsz <= sndlim)) {
945 tipc_bearer_send(link->bearer_id, buf, addr);
946 link->next_out = next;
947 link->unacked_window = 0;
948 }
949 seqno++;
950 link->last_out = buf;
951 buf = next;
952 }
953 link->next_out_no = seqno;
954 link->out_queue_size = qsz;
955 return 0;
956}
957
958/**
959 * tipc_link_xmit2() is the general link level function for message sending
960 * @buf: chain of buffers containing message
961 * @dsz: amount of user data to be sent
962 * @dnode: address of destination node
963 * @selector: a number used for deterministic link selection
964 * Consumes the buffer chain, except when returning -ELINKCONG
965 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
966 */
967int tipc_link_xmit2(struct sk_buff *buf, u32 dnode, u32 selector)
968{
969 struct tipc_link *link = NULL;
970 struct tipc_node *node;
971 int rc = -EHOSTUNREACH;
972
973 node = tipc_node_find(dnode);
974 if (node) {
975 tipc_node_lock(node);
976 link = node->active_links[selector & 1];
977 if (link)
978 rc = __tipc_link_xmit2(link, buf);
979 tipc_node_unlock(node);
980 }
981
982 if (link)
983 return rc;
984
985 if (likely(in_own_node(dnode)))
986 return tipc_sk_rcv(buf);
987
988 kfree_skb_list(buf);
989 return rc;
990}
991
c64f7a6a 992/*
247f0f3c 993 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
c64f7a6a
JM
994 *
995 * Give a newly added peer node the sequence number where it should
996 * start receiving and acking broadcast packets.
997 *
998 * Called with node locked
999 */
247f0f3c 1000static void tipc_link_sync_xmit(struct tipc_link *l)
c64f7a6a
JM
1001{
1002 struct sk_buff *buf;
1003 struct tipc_msg *msg;
1004
1005 buf = tipc_buf_acquire(INT_H_SIZE);
1006 if (!buf)
1007 return;
1008
1009 msg = buf_msg(buf);
1010 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
1011 msg_set_last_bcast(msg, l->owner->bclink.acked);
1012 link_add_chain_to_outqueue(l, buf, 0);
1013 tipc_link_push_queue(l);
1014}
1015
1016/*
247f0f3c 1017 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
c64f7a6a
JM
1018 * Receive the sequence number where we should start receiving and
1019 * acking broadcast packets from a newly added peer node, and open
1020 * up for reception of such packets.
1021 *
1022 * Called with node locked
1023 */
247f0f3c 1024static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
c64f7a6a
JM
1025{
1026 struct tipc_msg *msg = buf_msg(buf);
1027
1028 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
1029 n->bclink.recv_permitted = true;
1030 kfree_skb(buf);
1031}
1032
1033/*
247f0f3c 1034 * tipc_link_names_xmit - send name table entries to new neighbor
9aa88c2a
AS
1035 *
1036 * Send routine for bulk delivery of name table messages when contact
1037 * with a new neighbor occurs. No link congestion checking is performed
1038 * because name table messages *must* be delivered. The messages must be
1039 * small enough not to require fragmentation.
1040 * Called without any locks held.
1041 */
247f0f3c 1042void tipc_link_names_xmit(struct list_head *message_list, u32 dest)
9aa88c2a
AS
1043{
1044 struct tipc_node *n_ptr;
a18c4bc3 1045 struct tipc_link *l_ptr;
9aa88c2a
AS
1046 struct sk_buff *buf;
1047 struct sk_buff *temp_buf;
1048
1049 if (list_empty(message_list))
1050 return;
1051
9aa88c2a
AS
1052 n_ptr = tipc_node_find(dest);
1053 if (n_ptr) {
1054 tipc_node_lock(n_ptr);
1055 l_ptr = n_ptr->active_links[0];
1056 if (l_ptr) {
1057 /* convert circular list to linear list */
1058 ((struct sk_buff *)message_list->prev)->next = NULL;
1059 link_add_chain_to_outqueue(l_ptr,
1060 (struct sk_buff *)message_list->next, 0);
1061 tipc_link_push_queue(l_ptr);
1062 INIT_LIST_HEAD(message_list);
1063 }
1064 tipc_node_unlock(n_ptr);
1065 }
9aa88c2a
AS
1066
1067 /* discard the messages if they couldn't be sent */
9aa88c2a
AS
1068 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
1069 list_del((struct list_head *)buf);
5f6d9123 1070 kfree_skb(buf);
9aa88c2a
AS
1071 }
1072}
1073
c4307285 1074/*
4323add6 1075 * tipc_link_push_packet: Push one unsent packet to the media
b97bf3fd 1076 */
98056963 1077static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
b97bf3fd
PL
1078{
1079 struct sk_buff *buf = l_ptr->first_out;
1080 u32 r_q_size = l_ptr->retransm_queue_size;
1081 u32 r_q_head = l_ptr->retransm_queue_head;
1082
1083 /* Step to position where retransmission failed, if any, */
1084 /* consider that buffers may have been released in meantime */
b97bf3fd 1085 if (r_q_size && buf) {
c4307285 1086 u32 last = lesser(mod(r_q_head + r_q_size),
b97bf3fd 1087 link_last_sent(l_ptr));
f905730c 1088 u32 first = buf_seqno(buf);
b97bf3fd
PL
1089
1090 while (buf && less(first, r_q_head)) {
1091 first = mod(first + 1);
1092 buf = buf->next;
1093 }
1094 l_ptr->retransm_queue_head = r_q_head = first;
1095 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1096 }
1097
1098 /* Continue retransmission now, if there is anything: */
ca509101 1099 if (r_q_size && buf) {
b97bf3fd 1100 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
c4307285 1101 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
7a2f7d18 1102 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
3c294cb3
YX
1103 l_ptr->retransm_queue_head = mod(++r_q_head);
1104 l_ptr->retransm_queue_size = --r_q_size;
1105 l_ptr->stats.retransmitted++;
1106 return 0;
b97bf3fd
PL
1107 }
1108
1109 /* Send deferred protocol message, if any: */
b97bf3fd
PL
1110 buf = l_ptr->proto_msg_queue;
1111 if (buf) {
1112 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
0e65967e 1113 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
7a2f7d18 1114 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
3c294cb3
YX
1115 l_ptr->unacked_window = 0;
1116 kfree_skb(buf);
1117 l_ptr->proto_msg_queue = NULL;
1118 return 0;
b97bf3fd
PL
1119 }
1120
1121 /* Send one deferred data message, if send window not full: */
b97bf3fd
PL
1122 buf = l_ptr->next_out;
1123 if (buf) {
1124 struct tipc_msg *msg = buf_msg(buf);
1125 u32 next = msg_seqno(msg);
f905730c 1126 u32 first = buf_seqno(l_ptr->first_out);
b97bf3fd
PL
1127
1128 if (mod(next - first) < l_ptr->queue_limit[0]) {
1129 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1130 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
7a2f7d18
YX
1131 tipc_bearer_send(l_ptr->bearer_id, buf,
1132 &l_ptr->media_addr);
3c294cb3 1133 if (msg_user(msg) == MSG_BUNDLER)
4f1688b2 1134 msg_set_type(msg, BUNDLE_CLOSED);
3c294cb3
YX
1135 l_ptr->next_out = buf->next;
1136 return 0;
b97bf3fd
PL
1137 }
1138 }
3c294cb3 1139 return 1;
b97bf3fd
PL
1140}
1141
1142/*
1143 * push_queue(): push out the unsent messages of a link where
1144 * congestion has abated. Node is locked
1145 */
a18c4bc3 1146void tipc_link_push_queue(struct tipc_link *l_ptr)
b97bf3fd
PL
1147{
1148 u32 res;
1149
b97bf3fd 1150 do {
4323add6 1151 res = tipc_link_push_packet(l_ptr);
0e35fd5e 1152 } while (!res);
b97bf3fd
PL
1153}
1154
3f5a12bd 1155void tipc_link_reset_all(struct tipc_node *node)
d356eeba 1156{
d356eeba
AS
1157 char addr_string[16];
1158 u32 i;
1159
3f5a12bd 1160 tipc_node_lock(node);
d356eeba 1161
2cf8aa19 1162 pr_warn("Resetting all links to %s\n",
3f5a12bd 1163 tipc_addr_string_fill(addr_string, node->addr));
d356eeba
AS
1164
1165 for (i = 0; i < MAX_BEARERS; i++) {
3f5a12bd
YX
1166 if (node->links[i]) {
1167 link_print(node->links[i], "Resetting link\n");
1168 tipc_link_reset(node->links[i]);
d356eeba
AS
1169 }
1170 }
1171
3f5a12bd 1172 tipc_node_unlock(node);
d356eeba
AS
1173}
1174
a18c4bc3 1175static void link_retransmit_failure(struct tipc_link *l_ptr,
ae8509c4 1176 struct sk_buff *buf)
d356eeba
AS
1177{
1178 struct tipc_msg *msg = buf_msg(buf);
1179
2cf8aa19 1180 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
d356eeba
AS
1181
1182 if (l_ptr->addr) {
d356eeba 1183 /* Handle failure on standard link */
8d64a5ba 1184 link_print(l_ptr, "Resetting link\n");
d356eeba
AS
1185 tipc_link_reset(l_ptr);
1186
1187 } else {
d356eeba 1188 /* Handle failure on broadcast link */
6c00055a 1189 struct tipc_node *n_ptr;
d356eeba
AS
1190 char addr_string[16];
1191
2cf8aa19
EH
1192 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1193 pr_cont("Outstanding acks: %lu\n",
1194 (unsigned long) TIPC_SKB_CB(buf)->handle);
617dbeaa 1195
01d83edd 1196 n_ptr = tipc_bclink_retransmit_to();
d356eeba
AS
1197 tipc_node_lock(n_ptr);
1198
c68ca7b7 1199 tipc_addr_string_fill(addr_string, n_ptr->addr);
2cf8aa19 1200 pr_info("Broadcast link info for %s\n", addr_string);
389dd9bc
YX
1201 pr_info("Reception permitted: %d, Acked: %u\n",
1202 n_ptr->bclink.recv_permitted,
2cf8aa19
EH
1203 n_ptr->bclink.acked);
1204 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1205 n_ptr->bclink.last_in,
1206 n_ptr->bclink.oos_state,
1207 n_ptr->bclink.last_sent);
d356eeba 1208
d356eeba
AS
1209 tipc_node_unlock(n_ptr);
1210
3f5a12bd 1211 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
d356eeba
AS
1212 l_ptr->stale_count = 0;
1213 }
1214}
1215
a18c4bc3 1216void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
4323add6 1217 u32 retransmits)
b97bf3fd
PL
1218{
1219 struct tipc_msg *msg;
1220
d356eeba
AS
1221 if (!buf)
1222 return;
1223
1224 msg = buf_msg(buf);
c4307285 1225
512137ee
EH
1226 /* Detect repeated retransmit failures */
1227 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1228 if (++l_ptr->stale_count > 100) {
1229 link_retransmit_failure(l_ptr, buf);
1230 return;
d356eeba
AS
1231 }
1232 } else {
512137ee
EH
1233 l_ptr->last_retransmitted = msg_seqno(msg);
1234 l_ptr->stale_count = 1;
b97bf3fd 1235 }
d356eeba 1236
ca509101 1237 while (retransmits && (buf != l_ptr->next_out) && buf) {
b97bf3fd
PL
1238 msg = buf_msg(buf);
1239 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
c4307285 1240 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
7a2f7d18 1241 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
3c294cb3
YX
1242 buf = buf->next;
1243 retransmits--;
1244 l_ptr->stats.retransmitted++;
b97bf3fd 1245 }
d356eeba 1246
b97bf3fd
PL
1247 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1248}
1249
c4307285 1250/**
b97bf3fd
PL
1251 * link_insert_deferred_queue - insert deferred messages back into receive chain
1252 */
a18c4bc3 1253static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
b97bf3fd
PL
1254 struct sk_buff *buf)
1255{
1256 u32 seq_no;
1257
1258 if (l_ptr->oldest_deferred_in == NULL)
1259 return buf;
1260
f905730c 1261 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
b97bf3fd
PL
1262 if (seq_no == mod(l_ptr->next_in_no)) {
1263 l_ptr->newest_deferred_in->next = buf;
1264 buf = l_ptr->oldest_deferred_in;
1265 l_ptr->oldest_deferred_in = NULL;
1266 l_ptr->deferred_inqueue_sz = 0;
1267 }
1268 return buf;
1269}
1270
85035568
AS
1271/**
1272 * link_recv_buf_validate - validate basic format of received message
1273 *
1274 * This routine ensures a TIPC message has an acceptable header, and at least
1275 * as much data as the header indicates it should. The routine also ensures
1276 * that the entire message header is stored in the main fragment of the message
1277 * buffer, to simplify future access to message header fields.
1278 *
1279 * Note: Having extra info present in the message header or data areas is OK.
1280 * TIPC will ignore the excess, under the assumption that it is optional info
1281 * introduced by a later release of the protocol.
1282 */
85035568
AS
1283static int link_recv_buf_validate(struct sk_buff *buf)
1284{
1285 static u32 min_data_hdr_size[8] = {
741d9eb7 1286 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
85035568
AS
1287 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1288 };
1289
1290 struct tipc_msg *msg;
1291 u32 tipc_hdr[2];
1292 u32 size;
1293 u32 hdr_size;
1294 u32 min_hdr_size;
1295
64380a04
EH
1296 /* If this packet comes from the defer queue, the skb has already
1297 * been validated
1298 */
1299 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1300 return 1;
1301
85035568
AS
1302 if (unlikely(buf->len < MIN_H_SIZE))
1303 return 0;
1304
1305 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1306 if (msg == NULL)
1307 return 0;
1308
1309 if (unlikely(msg_version(msg) != TIPC_VERSION))
1310 return 0;
1311
1312 size = msg_size(msg);
1313 hdr_size = msg_hdr_sz(msg);
1314 min_hdr_size = msg_isdata(msg) ?
1315 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1316
1317 if (unlikely((hdr_size < min_hdr_size) ||
1318 (size < hdr_size) ||
1319 (buf->len < size) ||
1320 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1321 return 0;
1322
1323 return pskb_may_pull(buf, hdr_size);
1324}
1325
b02b69c8 1326/**
170b3927 1327 * tipc_rcv - process TIPC packets/messages arriving from off-node
b02b69c8 1328 * @head: pointer to message buffer chain
7a2f7d18 1329 * @b_ptr: pointer to bearer message arrived on
b02b69c8
AS
1330 *
1331 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1332 * structure (i.e. cannot be NULL), but bearer can be inactive.
1333 */
170b3927 1334void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
b97bf3fd 1335{
b97bf3fd 1336 while (head) {
6c00055a 1337 struct tipc_node *n_ptr;
a18c4bc3 1338 struct tipc_link *l_ptr;
b97bf3fd
PL
1339 struct sk_buff *crs;
1340 struct sk_buff *buf = head;
85035568
AS
1341 struct tipc_msg *msg;
1342 u32 seq_no;
1343 u32 ackd;
b97bf3fd 1344 u32 released = 0;
b97bf3fd 1345
b97bf3fd 1346 head = head->next;
732256b9 1347 buf->next = NULL;
85035568
AS
1348
1349 /* Ensure message is well-formed */
85035568 1350 if (unlikely(!link_recv_buf_validate(buf)))
3af390e2 1351 goto discard;
b97bf3fd 1352
fe13dda2 1353 /* Ensure message data is a single contiguous unit */
5f6d9123 1354 if (unlikely(skb_linearize(buf)))
3af390e2 1355 goto discard;
fe13dda2 1356
85035568 1357 /* Handle arrival of a non-unicast link message */
85035568
AS
1358 msg = buf_msg(buf);
1359
b97bf3fd 1360 if (unlikely(msg_non_seq(msg))) {
1265a021 1361 if (msg_user(msg) == LINK_CONFIG)
247f0f3c 1362 tipc_disc_rcv(buf, b_ptr);
1265a021 1363 else
247f0f3c 1364 tipc_bclink_rcv(buf);
b97bf3fd
PL
1365 continue;
1366 }
c4307285 1367
ed33a9c4 1368 /* Discard unicast link messages destined for another node */
26008247
AS
1369 if (unlikely(!msg_short(msg) &&
1370 (msg_destnode(msg) != tipc_own_addr)))
3af390e2 1371 goto discard;
c4307285 1372
5a68d5ee 1373 /* Locate neighboring node that sent message */
4323add6 1374 n_ptr = tipc_node_find(msg_prevnode(msg));
b97bf3fd 1375 if (unlikely(!n_ptr))
3af390e2 1376 goto discard;
4323add6 1377 tipc_node_lock(n_ptr);
85035568 1378
b4b56102 1379 /* Locate unicast link endpoint that should handle message */
b4b56102 1380 l_ptr = n_ptr->links[b_ptr->identity];
3af390e2
YX
1381 if (unlikely(!l_ptr))
1382 goto unlock_discard;
5a68d5ee 1383
b4b56102 1384 /* Verify that communication with node is currently allowed */
aecb9bb8 1385 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
10f465c4
YX
1386 msg_user(msg) == LINK_PROTOCOL &&
1387 (msg_type(msg) == RESET_MSG ||
1388 msg_type(msg) == ACTIVATE_MSG) &&
1389 !msg_redundant_link(msg))
aecb9bb8 1390 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
10f465c4
YX
1391
1392 if (tipc_node_blocked(n_ptr))
3af390e2 1393 goto unlock_discard;
85035568
AS
1394
1395 /* Validate message sequence number info */
85035568
AS
1396 seq_no = msg_seqno(msg);
1397 ackd = msg_ack(msg);
1398
1399 /* Release acked messages */
389dd9bc 1400 if (n_ptr->bclink.recv_permitted)
36559591 1401 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
b97bf3fd
PL
1402
1403 crs = l_ptr->first_out;
c4307285 1404 while ((crs != l_ptr->next_out) &&
f905730c 1405 less_eq(buf_seqno(crs), ackd)) {
b97bf3fd 1406 struct sk_buff *next = crs->next;
5f6d9123 1407 kfree_skb(crs);
b97bf3fd
PL
1408 crs = next;
1409 released++;
1410 }
1411 if (released) {
1412 l_ptr->first_out = crs;
1413 l_ptr->out_queue_size -= released;
1414 }
85035568
AS
1415
1416 /* Try sending any messages link endpoint has pending */
b97bf3fd 1417 if (unlikely(l_ptr->next_out))
4323add6 1418 tipc_link_push_queue(l_ptr);
a5377831 1419
b97bf3fd 1420 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
4323add6 1421 tipc_link_wakeup_ports(l_ptr, 0);
a5377831 1422
b97bf3fd
PL
1423 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1424 l_ptr->stats.sent_acks++;
247f0f3c 1425 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
1426 }
1427
a5377831 1428 /* Process the incoming packet */
3af390e2
YX
1429 if (unlikely(!link_working_working(l_ptr))) {
1430 if (msg_user(msg) == LINK_PROTOCOL) {
247f0f3c 1431 tipc_link_proto_rcv(l_ptr, buf);
3af390e2 1432 head = link_insert_deferred_queue(l_ptr, head);
4323add6 1433 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1434 continue;
1435 }
3af390e2
YX
1436
1437 /* Traffic message. Conditionally activate link */
1438 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1439
1440 if (link_working_working(l_ptr)) {
1441 /* Re-insert buffer in front of queue */
1442 buf->next = head;
1443 head = buf;
1444 tipc_node_unlock(n_ptr);
1445 continue;
1446 }
1447 goto unlock_discard;
1448 }
1449
1450 /* Link is now in state WORKING_WORKING */
1451 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
b97bf3fd
PL
1452 link_handle_out_of_seq_msg(l_ptr, buf);
1453 head = link_insert_deferred_queue(l_ptr, head);
4323add6 1454 tipc_node_unlock(n_ptr);
b97bf3fd
PL
1455 continue;
1456 }
3af390e2
YX
1457 l_ptr->next_in_no++;
1458 if (unlikely(l_ptr->oldest_deferred_in))
b97bf3fd 1459 head = link_insert_deferred_queue(l_ptr, head);
a5377831
JPM
1460
1461 /* Deliver packet/message to correct user: */
1462 if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL)) {
1463 if (!tipc_link_tunnel_rcv(n_ptr, &buf)) {
1464 tipc_node_unlock(n_ptr);
1465 continue;
1466 }
1467 msg = buf_msg(buf);
1468 } else if (msg_user(msg) == MSG_FRAGMENTER) {
a5377831 1469 l_ptr->stats.recv_fragments++;
37e22164 1470 if (tipc_buf_append(&l_ptr->reasm_buf, &buf)) {
a5377831
JPM
1471 l_ptr->stats.recv_fragmented++;
1472 msg = buf_msg(buf);
1473 } else {
37e22164 1474 if (!l_ptr->reasm_buf)
a5377831
JPM
1475 tipc_link_reset(l_ptr);
1476 tipc_node_unlock(n_ptr);
1477 continue;
1478 }
1479 }
1480
1481 switch (msg_user(msg)) {
1482 case TIPC_LOW_IMPORTANCE:
1483 case TIPC_MEDIUM_IMPORTANCE:
1484 case TIPC_HIGH_IMPORTANCE:
1485 case TIPC_CRITICAL_IMPORTANCE:
ec8a2e56 1486 case CONN_MANAGER:
4323add6 1487 tipc_node_unlock(n_ptr);
9816f061 1488 tipc_sk_rcv(buf);
b97bf3fd 1489 continue;
3af390e2
YX
1490 case MSG_BUNDLER:
1491 l_ptr->stats.recv_bundles++;
1492 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1493 tipc_node_unlock(n_ptr);
247f0f3c 1494 tipc_link_bundle_rcv(buf);
3af390e2
YX
1495 continue;
1496 case NAME_DISTRIBUTOR:
1497 n_ptr->bclink.recv_permitted = true;
1498 tipc_node_unlock(n_ptr);
247f0f3c 1499 tipc_named_rcv(buf);
3af390e2 1500 continue;
a5377831 1501 case BCAST_PROTOCOL:
247f0f3c 1502 tipc_link_sync_rcv(n_ptr, buf);
a5377831 1503 break;
3af390e2
YX
1504 default:
1505 kfree_skb(buf);
3af390e2 1506 break;
b97bf3fd 1507 }
4323add6 1508 tipc_node_unlock(n_ptr);
3af390e2
YX
1509 continue;
1510unlock_discard:
3af390e2
YX
1511 tipc_node_unlock(n_ptr);
1512discard:
5f6d9123 1513 kfree_skb(buf);
b97bf3fd 1514 }
b97bf3fd
PL
1515}
1516
2c53040f 1517/**
8809b255
AS
1518 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1519 *
1520 * Returns increase in queue length (i.e. 0 or 1)
b97bf3fd 1521 */
8809b255 1522u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
4323add6 1523 struct sk_buff *buf)
b97bf3fd 1524{
8809b255
AS
1525 struct sk_buff *queue_buf;
1526 struct sk_buff **prev;
f905730c 1527 u32 seq_no = buf_seqno(buf);
b97bf3fd
PL
1528
1529 buf->next = NULL;
1530
1531 /* Empty queue ? */
1532 if (*head == NULL) {
1533 *head = *tail = buf;
1534 return 1;
1535 }
1536
1537 /* Last ? */
f905730c 1538 if (less(buf_seqno(*tail), seq_no)) {
b97bf3fd
PL
1539 (*tail)->next = buf;
1540 *tail = buf;
1541 return 1;
1542 }
1543
8809b255
AS
1544 /* Locate insertion point in queue, then insert; discard if duplicate */
1545 prev = head;
1546 queue_buf = *head;
1547 for (;;) {
1548 u32 curr_seqno = buf_seqno(queue_buf);
b97bf3fd 1549
8809b255 1550 if (seq_no == curr_seqno) {
5f6d9123 1551 kfree_skb(buf);
8809b255 1552 return 0;
b97bf3fd 1553 }
8809b255
AS
1554
1555 if (less(seq_no, curr_seqno))
b97bf3fd 1556 break;
b97bf3fd 1557
8809b255
AS
1558 prev = &queue_buf->next;
1559 queue_buf = queue_buf->next;
1560 }
b97bf3fd 1561
8809b255
AS
1562 buf->next = queue_buf;
1563 *prev = buf;
1564 return 1;
b97bf3fd
PL
1565}
1566
8809b255 1567/*
b97bf3fd
PL
1568 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1569 */
a18c4bc3 1570static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
b97bf3fd
PL
1571 struct sk_buff *buf)
1572{
f905730c 1573 u32 seq_no = buf_seqno(buf);
b97bf3fd
PL
1574
1575 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
247f0f3c 1576 tipc_link_proto_rcv(l_ptr, buf);
b97bf3fd
PL
1577 return;
1578 }
1579
b97bf3fd 1580 /* Record OOS packet arrival (force mismatch on next timeout) */
b97bf3fd
PL
1581 l_ptr->checkpoint--;
1582
c4307285 1583 /*
b97bf3fd
PL
1584 * Discard packet if a duplicate; otherwise add it to deferred queue
1585 * and notify peer of gap as per protocol specification
1586 */
b97bf3fd
PL
1587 if (less(seq_no, mod(l_ptr->next_in_no))) {
1588 l_ptr->stats.duplicates++;
5f6d9123 1589 kfree_skb(buf);
b97bf3fd
PL
1590 return;
1591 }
1592
4323add6
PL
1593 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1594 &l_ptr->newest_deferred_in, buf)) {
b97bf3fd
PL
1595 l_ptr->deferred_inqueue_sz++;
1596 l_ptr->stats.deferred_recv++;
64380a04 1597 TIPC_SKB_CB(buf)->deferred = true;
b97bf3fd 1598 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
247f0f3c 1599 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
b97bf3fd
PL
1600 } else
1601 l_ptr->stats.duplicates++;
1602}
1603
1604/*
1605 * Send protocol message to the other endpoint.
1606 */
247f0f3c
YX
1607void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1608 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
b97bf3fd 1609{
1fc54d8f 1610 struct sk_buff *buf = NULL;
b97bf3fd 1611 struct tipc_msg *msg = l_ptr->pmsg;
c4307285 1612 u32 msg_size = sizeof(l_ptr->proto_msg);
75f0aa49 1613 int r_flag;
b97bf3fd 1614
92d2c905 1615 /* Discard any previous message that was deferred due to congestion */
92d2c905 1616 if (l_ptr->proto_msg_queue) {
5f6d9123 1617 kfree_skb(l_ptr->proto_msg_queue);
92d2c905
AS
1618 l_ptr->proto_msg_queue = NULL;
1619 }
1620
77a7e07a
YX
1621 /* Don't send protocol message during link changeover */
1622 if (l_ptr->exp_msg_count)
b97bf3fd 1623 return;
b4b56102
AS
1624
1625 /* Abort non-RESET send if communication with node is prohibited */
10f465c4 1626 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
b4b56102
AS
1627 return;
1628
92d2c905 1629 /* Create protocol message with "out-of-sequence" sequence number */
b97bf3fd 1630 msg_set_type(msg, msg_typ);
7a2f7d18 1631 msg_set_net_plane(msg, l_ptr->net_plane);
7a54d4a9 1632 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
4323add6 1633 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
b97bf3fd
PL
1634
1635 if (msg_typ == STATE_MSG) {
1636 u32 next_sent = mod(l_ptr->next_out_no);
1637
4323add6 1638 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
1639 return;
1640 if (l_ptr->next_out)
f905730c 1641 next_sent = buf_seqno(l_ptr->next_out);
b97bf3fd
PL
1642 msg_set_next_sent(msg, next_sent);
1643 if (l_ptr->oldest_deferred_in) {
f905730c 1644 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
b97bf3fd
PL
1645 gap = mod(rec - mod(l_ptr->next_in_no));
1646 }
1647 msg_set_seq_gap(msg, gap);
1648 if (gap)
1649 l_ptr->stats.sent_nacks++;
1650 msg_set_link_tolerance(msg, tolerance);
1651 msg_set_linkprio(msg, priority);
1652 msg_set_max_pkt(msg, ack_mtu);
1653 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1654 msg_set_probe(msg, probe_msg != 0);
c4307285 1655 if (probe_msg) {
b97bf3fd
PL
1656 u32 mtu = l_ptr->max_pkt;
1657
c4307285 1658 if ((mtu < l_ptr->max_pkt_target) &&
b97bf3fd
PL
1659 link_working_working(l_ptr) &&
1660 l_ptr->fsm_msg_cnt) {
1661 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285
YH
1662 if (l_ptr->max_pkt_probes == 10) {
1663 l_ptr->max_pkt_target = (msg_size - 4);
1664 l_ptr->max_pkt_probes = 0;
b97bf3fd 1665 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
c4307285 1666 }
b97bf3fd 1667 l_ptr->max_pkt_probes++;
c4307285 1668 }
b97bf3fd
PL
1669
1670 l_ptr->stats.sent_probes++;
c4307285 1671 }
b97bf3fd
PL
1672 l_ptr->stats.sent_states++;
1673 } else { /* RESET_MSG or ACTIVATE_MSG */
1674 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1675 msg_set_seq_gap(msg, 0);
1676 msg_set_next_sent(msg, 1);
f23d9bf2 1677 msg_set_probe(msg, 0);
b97bf3fd
PL
1678 msg_set_link_tolerance(msg, l_ptr->tolerance);
1679 msg_set_linkprio(msg, l_ptr->priority);
1680 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1681 }
1682
75f0aa49
AS
1683 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1684 msg_set_redundant_link(msg, r_flag);
b97bf3fd 1685 msg_set_linkprio(msg, l_ptr->priority);
92d2c905 1686 msg_set_size(msg, msg_size);
b97bf3fd
PL
1687
1688 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1689
31e3c3f6 1690 buf = tipc_buf_acquire(msg_size);
b97bf3fd
PL
1691 if (!buf)
1692 return;
1693
27d7ff46 1694 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
796c75d0 1695 buf->priority = TC_PRIO_CONTROL;
b97bf3fd 1696
7a2f7d18 1697 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
92d2c905 1698 l_ptr->unacked_window = 0;
5f6d9123 1699 kfree_skb(buf);
b97bf3fd
PL
1700}
1701
1702/*
1703 * Receive protocol message :
c4307285
YH
1704 * Note that network plane id propagates through the network, and may
1705 * change at any time. The node with lowest address rules
b97bf3fd 1706 */
247f0f3c 1707static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
b97bf3fd
PL
1708{
1709 u32 rec_gap = 0;
1710 u32 max_pkt_info;
c4307285 1711 u32 max_pkt_ack;
b97bf3fd
PL
1712 u32 msg_tol;
1713 struct tipc_msg *msg = buf_msg(buf);
1714
77a7e07a
YX
1715 /* Discard protocol message during link changeover */
1716 if (l_ptr->exp_msg_count)
b97bf3fd
PL
1717 goto exit;
1718
7a2f7d18 1719 if (l_ptr->net_plane != msg_net_plane(msg))
b97bf3fd 1720 if (tipc_own_addr > msg_prevnode(msg))
7a2f7d18 1721 l_ptr->net_plane = msg_net_plane(msg);
b97bf3fd 1722
b97bf3fd 1723 switch (msg_type(msg)) {
c4307285 1724
b97bf3fd 1725 case RESET_MSG:
a686e685
AS
1726 if (!link_working_unknown(l_ptr) &&
1727 (l_ptr->peer_session != INVALID_SESSION)) {
641c218d
AS
1728 if (less_eq(msg_session(msg), l_ptr->peer_session))
1729 break; /* duplicate or old reset: ignore */
b97bf3fd 1730 }
b4b56102
AS
1731
1732 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1733 link_working_unknown(l_ptr))) {
1734 /*
1735 * peer has lost contact -- don't allow peer's links
1736 * to reactivate before we recognize loss & clean up
1737 */
ca9cf06a 1738 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
b4b56102
AS
1739 }
1740
47361c87
AS
1741 link_state_event(l_ptr, RESET_MSG);
1742
b97bf3fd
PL
1743 /* fall thru' */
1744 case ACTIVATE_MSG:
1745 /* Update link settings according other endpoint's values */
b97bf3fd
PL
1746 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1747
2db9983a
AS
1748 msg_tol = msg_link_tolerance(msg);
1749 if (msg_tol > l_ptr->tolerance)
b97bf3fd
PL
1750 link_set_supervision_props(l_ptr, msg_tol);
1751
1752 if (msg_linkprio(msg) > l_ptr->priority)
1753 l_ptr->priority = msg_linkprio(msg);
1754
1755 max_pkt_info = msg_max_pkt(msg);
c4307285 1756 if (max_pkt_info) {
b97bf3fd
PL
1757 if (max_pkt_info < l_ptr->max_pkt_target)
1758 l_ptr->max_pkt_target = max_pkt_info;
1759 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1760 l_ptr->max_pkt = l_ptr->max_pkt_target;
1761 } else {
c4307285 1762 l_ptr->max_pkt = l_ptr->max_pkt_target;
b97bf3fd 1763 }
b97bf3fd 1764
4d75313c 1765 /* Synchronize broadcast link info, if not done previously */
7a54d4a9
AS
1766 if (!tipc_node_is_up(l_ptr->owner)) {
1767 l_ptr->owner->bclink.last_sent =
1768 l_ptr->owner->bclink.last_in =
1769 msg_last_bcast(msg);
1770 l_ptr->owner->bclink.oos_state = 0;
1771 }
4d75313c 1772
b97bf3fd
PL
1773 l_ptr->peer_session = msg_session(msg);
1774 l_ptr->peer_bearer_id = msg_bearer_id(msg);
47361c87
AS
1775
1776 if (msg_type(msg) == ACTIVATE_MSG)
1777 link_state_event(l_ptr, ACTIVATE_MSG);
b97bf3fd
PL
1778 break;
1779 case STATE_MSG:
1780
2db9983a
AS
1781 msg_tol = msg_link_tolerance(msg);
1782 if (msg_tol)
b97bf3fd 1783 link_set_supervision_props(l_ptr, msg_tol);
c4307285
YH
1784
1785 if (msg_linkprio(msg) &&
b97bf3fd 1786 (msg_linkprio(msg) != l_ptr->priority)) {
2cf8aa19
EH
1787 pr_warn("%s<%s>, priority change %u->%u\n",
1788 link_rst_msg, l_ptr->name, l_ptr->priority,
1789 msg_linkprio(msg));
b97bf3fd 1790 l_ptr->priority = msg_linkprio(msg);
4323add6 1791 tipc_link_reset(l_ptr); /* Enforce change to take effect */
b97bf3fd
PL
1792 break;
1793 }
ec37dcd3
JPM
1794
1795 /* Record reception; force mismatch at next timeout: */
1796 l_ptr->checkpoint--;
1797
b97bf3fd
PL
1798 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1799 l_ptr->stats.recv_states++;
1800 if (link_reset_unknown(l_ptr))
1801 break;
1802
1803 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
c4307285 1804 rec_gap = mod(msg_next_sent(msg) -
b97bf3fd
PL
1805 mod(l_ptr->next_in_no));
1806 }
1807
1808 max_pkt_ack = msg_max_pkt(msg);
c4307285 1809 if (max_pkt_ack > l_ptr->max_pkt) {
c4307285
YH
1810 l_ptr->max_pkt = max_pkt_ack;
1811 l_ptr->max_pkt_probes = 0;
1812 }
b97bf3fd
PL
1813
1814 max_pkt_ack = 0;
c4307285 1815 if (msg_probe(msg)) {
b97bf3fd 1816 l_ptr->stats.recv_probes++;
a016892c 1817 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
c4307285 1818 max_pkt_ack = msg_size(msg);
c4307285 1819 }
b97bf3fd
PL
1820
1821 /* Protocol message before retransmits, reduce loss risk */
389dd9bc 1822 if (l_ptr->owner->bclink.recv_permitted)
7a54d4a9
AS
1823 tipc_bclink_update_link_state(l_ptr->owner,
1824 msg_last_bcast(msg));
b97bf3fd
PL
1825
1826 if (rec_gap || (msg_probe(msg))) {
247f0f3c
YX
1827 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1828 0, max_pkt_ack);
b97bf3fd
PL
1829 }
1830 if (msg_seq_gap(msg)) {
b97bf3fd 1831 l_ptr->stats.recv_nacks++;
4323add6
PL
1832 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1833 msg_seq_gap(msg));
b97bf3fd
PL
1834 }
1835 break;
b97bf3fd
PL
1836 }
1837exit:
5f6d9123 1838 kfree_skb(buf);
b97bf3fd
PL
1839}
1840
1841
170b3927
JPM
1842/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1843 * a different bearer. Owner node is locked.
b97bf3fd 1844 */
170b3927
JPM
1845static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1846 struct tipc_msg *tunnel_hdr,
1847 struct tipc_msg *msg,
1848 u32 selector)
b97bf3fd 1849{
a18c4bc3 1850 struct tipc_link *tunnel;
b97bf3fd
PL
1851 struct sk_buff *buf;
1852 u32 length = msg_size(msg);
1853
1854 tunnel = l_ptr->owner->active_links[selector & 1];
5392d646 1855 if (!tipc_link_is_up(tunnel)) {
2cf8aa19 1856 pr_warn("%stunnel link no longer available\n", link_co_err);
b97bf3fd 1857 return;
5392d646 1858 }
b97bf3fd 1859 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
31e3c3f6 1860 buf = tipc_buf_acquire(length + INT_H_SIZE);
5392d646 1861 if (!buf) {
2cf8aa19 1862 pr_warn("%sunable to send tunnel msg\n", link_co_err);
b97bf3fd 1863 return;
5392d646 1864 }
27d7ff46
ACM
1865 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1866 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
247f0f3c 1867 __tipc_link_xmit(tunnel, buf);
b97bf3fd
PL
1868}
1869
1870
170b3927
JPM
1871/* tipc_link_failover_send_queue(): A link has gone down, but a second
1872 * link is still active. We can do failover. Tunnel the failing link's
1873 * whole send queue via the remaining link. This way, we don't lose
1874 * any packets, and sequence order is preserved for subsequent traffic
1875 * sent over the remaining link. Owner node is locked.
b97bf3fd 1876 */
170b3927 1877void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
b97bf3fd
PL
1878{
1879 u32 msgcount = l_ptr->out_queue_size;
1880 struct sk_buff *crs = l_ptr->first_out;
a18c4bc3 1881 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
b97bf3fd 1882 struct tipc_msg tunnel_hdr;
5392d646 1883 int split_bundles;
b97bf3fd
PL
1884
1885 if (!tunnel)
1886 return;
1887
c68ca7b7 1888 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
75715217 1889 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd
PL
1890 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1891 msg_set_msgcnt(&tunnel_hdr, msgcount);
f131072c 1892
b97bf3fd
PL
1893 if (!l_ptr->first_out) {
1894 struct sk_buff *buf;
1895
31e3c3f6 1896 buf = tipc_buf_acquire(INT_H_SIZE);
b97bf3fd 1897 if (buf) {
27d7ff46 1898 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
b97bf3fd 1899 msg_set_size(&tunnel_hdr, INT_H_SIZE);
247f0f3c 1900 __tipc_link_xmit(tunnel, buf);
b97bf3fd 1901 } else {
2cf8aa19
EH
1902 pr_warn("%sunable to send changeover msg\n",
1903 link_co_err);
b97bf3fd
PL
1904 }
1905 return;
1906 }
f131072c 1907
c4307285 1908 split_bundles = (l_ptr->owner->active_links[0] !=
5392d646
AS
1909 l_ptr->owner->active_links[1]);
1910
b97bf3fd
PL
1911 while (crs) {
1912 struct tipc_msg *msg = buf_msg(crs);
1913
1914 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
b97bf3fd 1915 struct tipc_msg *m = msg_get_wrapped(msg);
0e65967e 1916 unchar *pos = (unchar *)m;
b97bf3fd 1917
d788d805 1918 msgcount = msg_msgcnt(msg);
b97bf3fd 1919 while (msgcount--) {
0e65967e 1920 msg_set_seqno(m, msg_seqno(msg));
170b3927
JPM
1921 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1922 msg_link_selector(m));
b97bf3fd
PL
1923 pos += align(msg_size(m));
1924 m = (struct tipc_msg *)pos;
1925 }
1926 } else {
170b3927
JPM
1927 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1928 msg_link_selector(msg));
b97bf3fd
PL
1929 }
1930 crs = crs->next;
1931 }
1932}
1933
247f0f3c 1934/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
170b3927
JPM
1935 * duplicate of the first link's send queue via the new link. This way, we
1936 * are guaranteed that currently queued packets from a socket are delivered
1937 * before future traffic from the same socket, even if this is using the
1938 * new link. The last arriving copy of each duplicate packet is dropped at
1939 * the receiving end by the regular protocol check, so packet cardinality
1940 * and sequence order is preserved per sender/receiver socket pair.
1941 * Owner node is locked.
1942 */
247f0f3c 1943void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
170b3927 1944 struct tipc_link *tunnel)
b97bf3fd
PL
1945{
1946 struct sk_buff *iter;
1947 struct tipc_msg tunnel_hdr;
1948
c68ca7b7 1949 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
75715217 1950 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
b97bf3fd
PL
1951 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1952 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1953 iter = l_ptr->first_out;
1954 while (iter) {
1955 struct sk_buff *outbuf;
1956 struct tipc_msg *msg = buf_msg(iter);
1957 u32 length = msg_size(msg);
1958
1959 if (msg_user(msg) == MSG_BUNDLER)
1960 msg_set_type(msg, CLOSED_MSG);
1961 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
c4307285 1962 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
b97bf3fd 1963 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
31e3c3f6 1964 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
b97bf3fd 1965 if (outbuf == NULL) {
2cf8aa19
EH
1966 pr_warn("%sunable to send duplicate msg\n",
1967 link_co_err);
b97bf3fd
PL
1968 return;
1969 }
27d7ff46
ACM
1970 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1971 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1972 length);
247f0f3c 1973 __tipc_link_xmit(tunnel, outbuf);
4323add6 1974 if (!tipc_link_is_up(l_ptr))
b97bf3fd
PL
1975 return;
1976 iter = iter->next;
1977 }
1978}
1979
b97bf3fd
PL
1980/**
1981 * buf_extract - extracts embedded TIPC message from another message
1982 * @skb: encapsulating message buffer
1983 * @from_pos: offset to extract from
1984 *
c4307285 1985 * Returns a new message buffer containing an embedded message. The
b97bf3fd
PL
1986 * encapsulating message itself is left unchanged.
1987 */
b97bf3fd
PL
1988static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1989{
1990 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1991 u32 size = msg_size(msg);
1992 struct sk_buff *eb;
1993
31e3c3f6 1994 eb = tipc_buf_acquire(size);
b97bf3fd 1995 if (eb)
27d7ff46 1996 skb_copy_to_linear_data(eb, msg, size);
b97bf3fd
PL
1997 return eb;
1998}
1999
1dab3d5a
JPM
2000
2001
2002/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
2003 * Owner node is locked.
2004 */
2005static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
2006 struct sk_buff *t_buf)
2007{
2008 struct sk_buff *buf;
2009
2010 if (!tipc_link_is_up(l_ptr))
2011 return;
2012
2013 buf = buf_extract(t_buf, INT_H_SIZE);
2014 if (buf == NULL) {
2015 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
2016 return;
2017 }
2018
2019 /* Add buffer to deferred queue, if applicable: */
2020 link_handle_out_of_seq_msg(l_ptr, buf);
2021}
2022
f006c9c7
JPM
2023/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
2024 * Owner node is locked.
2025 */
2026static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
2027 struct sk_buff *t_buf)
2028{
2029 struct tipc_msg *t_msg = buf_msg(t_buf);
2030 struct sk_buff *buf = NULL;
2031 struct tipc_msg *msg;
2032
2033 if (tipc_link_is_up(l_ptr))
2034 tipc_link_reset(l_ptr);
2035
2036 /* First failover packet? */
2037 if (l_ptr->exp_msg_count == START_CHANGEOVER)
2038 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
2039
2040 /* Should there be an inner packet? */
2041 if (l_ptr->exp_msg_count) {
2042 l_ptr->exp_msg_count--;
2043 buf = buf_extract(t_buf, INT_H_SIZE);
2044 if (buf == NULL) {
2045 pr_warn("%sno inner failover pkt\n", link_co_err);
2046 goto exit;
2047 }
2048 msg = buf_msg(buf);
2049
2050 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
2051 kfree_skb(buf);
2052 buf = NULL;
2053 goto exit;
2054 }
2055 if (msg_user(msg) == MSG_FRAGMENTER) {
2056 l_ptr->stats.recv_fragments++;
37e22164 2057 tipc_buf_append(&l_ptr->reasm_buf, &buf);
f006c9c7
JPM
2058 }
2059 }
f006c9c7 2060exit:
7d33939f
JPM
2061 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
2062 tipc_node_detach_link(l_ptr->owner, l_ptr);
2063 kfree(l_ptr);
2064 }
f006c9c7
JPM
2065 return buf;
2066}
2067
1dab3d5a 2068/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
170b3927
JPM
2069 * via other link as result of a failover (ORIGINAL_MSG) or
2070 * a new active link (DUPLICATE_MSG). Failover packets are
2071 * returned to the active link for delivery upwards.
2072 * Owner node is locked.
b97bf3fd 2073 */
3bb53380 2074static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
170b3927 2075 struct sk_buff **buf)
b97bf3fd 2076{
02842f71
JPM
2077 struct sk_buff *t_buf = *buf;
2078 struct tipc_link *l_ptr;
2079 struct tipc_msg *t_msg = buf_msg(t_buf);
2080 u32 bearer_id = msg_bearer_id(t_msg);
b97bf3fd 2081
1e9d47a9
JPM
2082 *buf = NULL;
2083
cb4b102f
DC
2084 if (bearer_id >= MAX_BEARERS)
2085 goto exit;
1dab3d5a 2086
02842f71
JPM
2087 l_ptr = n_ptr->links[bearer_id];
2088 if (!l_ptr)
b97bf3fd 2089 goto exit;
b97bf3fd 2090
02842f71
JPM
2091 if (msg_type(t_msg) == DUPLICATE_MSG)
2092 tipc_link_dup_rcv(l_ptr, t_buf);
2093 else if (msg_type(t_msg) == ORIGINAL_MSG)
2094 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
1e9d47a9
JPM
2095 else
2096 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
b97bf3fd 2097exit:
02842f71 2098 kfree_skb(t_buf);
1e9d47a9 2099 return *buf != NULL;
b97bf3fd
PL
2100}
2101
2102/*
2103 * Bundler functionality:
2104 */
247f0f3c 2105void tipc_link_bundle_rcv(struct sk_buff *buf)
b97bf3fd
PL
2106{
2107 u32 msgcount = msg_msgcnt(buf_msg(buf));
2108 u32 pos = INT_H_SIZE;
2109 struct sk_buff *obuf;
ec8a2e56 2110 struct tipc_msg *omsg;
b97bf3fd 2111
b97bf3fd
PL
2112 while (msgcount--) {
2113 obuf = buf_extract(buf, pos);
2114 if (obuf == NULL) {
2cf8aa19 2115 pr_warn("Link unable to unbundle message(s)\n");
a10bd924 2116 break;
3ff50b79 2117 }
ec8a2e56
JPM
2118 omsg = buf_msg(obuf);
2119 pos += align(msg_size(omsg));
2120 if (msg_isdata(omsg) || (msg_user(omsg) == CONN_MANAGER)) {
2121 tipc_sk_rcv(obuf);
2122 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
2123 tipc_named_rcv(obuf);
2124 } else {
2125 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
2126 kfree_skb(obuf);
2127 }
b97bf3fd 2128 }
5f6d9123 2129 kfree_skb(buf);
b97bf3fd
PL
2130}
2131
2132/*
2133 * Fragmentation/defragmentation:
2134 */
2135
c4307285 2136/*
247f0f3c 2137 * tipc_link_frag_xmit: Entry for buffers needing fragmentation.
c4307285 2138 * The buffer is complete, inclusive total message length.
b97bf3fd
PL
2139 * Returns user data length.
2140 */
247f0f3c 2141static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
b97bf3fd 2142{
77561557
AS
2143 struct sk_buff *buf_chain = NULL;
2144 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
b97bf3fd
PL
2145 struct tipc_msg *inmsg = buf_msg(buf);
2146 struct tipc_msg fragm_hdr;
2147 u32 insize = msg_size(inmsg);
2148 u32 dsz = msg_data_sz(inmsg);
2149 unchar *crs = buf->data;
2150 u32 rest = insize;
15e979da 2151 u32 pack_sz = l_ptr->max_pkt;
b97bf3fd 2152 u32 fragm_sz = pack_sz - INT_H_SIZE;
77561557 2153 u32 fragm_no = 0;
9c396a7b 2154 u32 destaddr;
b97bf3fd
PL
2155
2156 if (msg_short(inmsg))
2157 destaddr = l_ptr->addr;
9c396a7b
AS
2158 else
2159 destaddr = msg_destnode(inmsg);
b97bf3fd 2160
b97bf3fd 2161 /* Prepare reusable fragment header: */
c68ca7b7 2162 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
75715217 2163 INT_H_SIZE, destaddr);
b97bf3fd
PL
2164
2165 /* Chop up message: */
b97bf3fd
PL
2166 while (rest > 0) {
2167 struct sk_buff *fragm;
2168
2169 if (rest <= fragm_sz) {
2170 fragm_sz = rest;
2171 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2172 }
31e3c3f6 2173 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
b97bf3fd 2174 if (fragm == NULL) {
5f6d9123 2175 kfree_skb(buf);
d77b3831 2176 kfree_skb_list(buf_chain);
77561557 2177 return -ENOMEM;
b97bf3fd
PL
2178 }
2179 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
77561557
AS
2180 fragm_no++;
2181 msg_set_fragm_no(&fragm_hdr, fragm_no);
27d7ff46
ACM
2182 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2183 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2184 fragm_sz);
77561557
AS
2185 buf_chain_tail->next = fragm;
2186 buf_chain_tail = fragm;
b97bf3fd 2187
b97bf3fd
PL
2188 rest -= fragm_sz;
2189 crs += fragm_sz;
2190 msg_set_type(&fragm_hdr, FRAGMENT);
2191 }
5f6d9123 2192 kfree_skb(buf);
77561557
AS
2193
2194 /* Append chain of fragments to send queue & send them */
77561557
AS
2195 l_ptr->long_msg_seq_no++;
2196 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2197 l_ptr->stats.sent_fragments += fragm_no;
2198 l_ptr->stats.sent_fragmented++;
2199 tipc_link_push_queue(l_ptr);
2200
b97bf3fd
PL
2201 return dsz;
2202}
2203
a18c4bc3 2204static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
b97bf3fd 2205{
5413b4c6
AS
2206 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2207 return;
2208
b97bf3fd
PL
2209 l_ptr->tolerance = tolerance;
2210 l_ptr->continuity_interval =
2211 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2212 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2213}
2214
a18c4bc3 2215void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
b97bf3fd
PL
2216{
2217 /* Data messages from this node, inclusive FIRST_FRAGM */
06d82c91
AS
2218 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2219 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2220 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2221 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
b97bf3fd 2222 /* Transiting data messages,inclusive FIRST_FRAGM */
06d82c91
AS
2223 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2224 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2225 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2226 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
b97bf3fd 2227 l_ptr->queue_limit[CONN_MANAGER] = 1200;
b97bf3fd
PL
2228 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2229 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2230 /* FRAGMENT and LAST_FRAGMENT packets */
2231 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2232}
2233
e099e86c
JPM
2234/* tipc_link_find_owner - locate owner node of link by link's name
2235 * @name: pointer to link name string
2236 * @bearer_id: pointer to index in 'node->links' array where the link was found.
c4307285 2237 *
e099e86c 2238 * Returns pointer to node owning the link, or 0 if no matching link is found.
b97bf3fd 2239 */
e099e86c
JPM
2240static struct tipc_node *tipc_link_find_owner(const char *link_name,
2241 unsigned int *bearer_id)
b97bf3fd 2242{
a18c4bc3 2243 struct tipc_link *l_ptr;
bbfbe47c 2244 struct tipc_node *n_ptr;
e099e86c 2245 struct tipc_node *found_node = 0;
bbfbe47c 2246 int i;
b97bf3fd 2247
e099e86c 2248 *bearer_id = 0;
6c7a762e
YX
2249 rcu_read_lock();
2250 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
a11607f5 2251 tipc_node_lock(n_ptr);
bbfbe47c
EH
2252 for (i = 0; i < MAX_BEARERS; i++) {
2253 l_ptr = n_ptr->links[i];
e099e86c
JPM
2254 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
2255 *bearer_id = i;
2256 found_node = n_ptr;
2257 break;
2258 }
bbfbe47c 2259 }
a11607f5 2260 tipc_node_unlock(n_ptr);
e099e86c
JPM
2261 if (found_node)
2262 break;
bbfbe47c 2263 }
6c7a762e
YX
2264 rcu_read_unlock();
2265
e099e86c 2266 return found_node;
b97bf3fd
PL
2267}
2268
5c216e1d
AS
2269/**
2270 * link_value_is_valid -- validate proposed link tolerance/priority/window
2271 *
2c53040f
BH
2272 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2273 * @new_value: the new value
5c216e1d
AS
2274 *
2275 * Returns 1 if value is within range, 0 if not.
2276 */
5c216e1d
AS
2277static int link_value_is_valid(u16 cmd, u32 new_value)
2278{
2279 switch (cmd) {
2280 case TIPC_CMD_SET_LINK_TOL:
2281 return (new_value >= TIPC_MIN_LINK_TOL) &&
2282 (new_value <= TIPC_MAX_LINK_TOL);
2283 case TIPC_CMD_SET_LINK_PRI:
2284 return (new_value <= TIPC_MAX_LINK_PRI);
2285 case TIPC_CMD_SET_LINK_WINDOW:
2286 return (new_value >= TIPC_MIN_LINK_WIN) &&
2287 (new_value <= TIPC_MAX_LINK_WIN);
2288 }
2289 return 0;
2290}
2291
5c216e1d
AS
2292/**
2293 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
2c53040f
BH
2294 * @name: ptr to link, bearer, or media name
2295 * @new_value: new value of link, bearer, or media setting
2296 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
5c216e1d 2297 *
7216cd94 2298 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
5c216e1d
AS
2299 *
2300 * Returns 0 if value updated and negative value on error.
2301 */
5c216e1d
AS
2302static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2303{
2304 struct tipc_node *node;
a18c4bc3 2305 struct tipc_link *l_ptr;
5c216e1d 2306 struct tipc_bearer *b_ptr;
358a0d1c 2307 struct tipc_media *m_ptr;
e099e86c 2308 int bearer_id;
636c0371 2309 int res = 0;
5c216e1d 2310
e099e86c
JPM
2311 node = tipc_link_find_owner(name, &bearer_id);
2312 if (node) {
5c216e1d 2313 tipc_node_lock(node);
e099e86c
JPM
2314 l_ptr = node->links[bearer_id];
2315
2316 if (l_ptr) {
2317 switch (cmd) {
2318 case TIPC_CMD_SET_LINK_TOL:
2319 link_set_supervision_props(l_ptr, new_value);
247f0f3c
YX
2320 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2321 new_value, 0, 0);
e099e86c
JPM
2322 break;
2323 case TIPC_CMD_SET_LINK_PRI:
2324 l_ptr->priority = new_value;
247f0f3c
YX
2325 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2326 0, new_value, 0);
e099e86c
JPM
2327 break;
2328 case TIPC_CMD_SET_LINK_WINDOW:
2329 tipc_link_set_queue_limits(l_ptr, new_value);
2330 break;
2331 default:
2332 res = -EINVAL;
2333 break;
2334 }
5c216e1d
AS
2335 }
2336 tipc_node_unlock(node);
636c0371 2337 return res;
5c216e1d
AS
2338 }
2339
2340 b_ptr = tipc_bearer_find(name);
2341 if (b_ptr) {
2342 switch (cmd) {
2343 case TIPC_CMD_SET_LINK_TOL:
2344 b_ptr->tolerance = new_value;
636c0371 2345 break;
5c216e1d
AS
2346 case TIPC_CMD_SET_LINK_PRI:
2347 b_ptr->priority = new_value;
636c0371 2348 break;
5c216e1d
AS
2349 case TIPC_CMD_SET_LINK_WINDOW:
2350 b_ptr->window = new_value;
636c0371
YX
2351 break;
2352 default:
2353 res = -EINVAL;
2354 break;
5c216e1d 2355 }
636c0371 2356 return res;
5c216e1d
AS
2357 }
2358
2359 m_ptr = tipc_media_find(name);
2360 if (!m_ptr)
2361 return -ENODEV;
2362 switch (cmd) {
2363 case TIPC_CMD_SET_LINK_TOL:
2364 m_ptr->tolerance = new_value;
636c0371 2365 break;
5c216e1d
AS
2366 case TIPC_CMD_SET_LINK_PRI:
2367 m_ptr->priority = new_value;
636c0371 2368 break;
5c216e1d
AS
2369 case TIPC_CMD_SET_LINK_WINDOW:
2370 m_ptr->window = new_value;
636c0371
YX
2371 break;
2372 default:
2373 res = -EINVAL;
2374 break;
5c216e1d 2375 }
636c0371 2376 return res;
5c216e1d
AS
2377}
2378
c4307285 2379struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
4323add6 2380 u16 cmd)
b97bf3fd
PL
2381{
2382 struct tipc_link_config *args;
c4307285 2383 u32 new_value;
c4307285 2384 int res;
b97bf3fd
PL
2385
2386 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
4323add6 2387 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
2388
2389 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2390 new_value = ntohl(args->value);
2391
5c216e1d
AS
2392 if (!link_value_is_valid(cmd, new_value))
2393 return tipc_cfg_reply_error_string(
2394 "cannot change, value invalid");
2395
4323add6 2396 if (!strcmp(args->name, tipc_bclink_name)) {
b97bf3fd 2397 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
4323add6
PL
2398 (tipc_bclink_set_queue_limits(new_value) == 0))
2399 return tipc_cfg_reply_none();
c4307285 2400 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
4323add6 2401 " (cannot change setting on broadcast link)");
b97bf3fd
PL
2402 }
2403
5c216e1d 2404 res = link_cmd_set_value(args->name, new_value, cmd);
b97bf3fd 2405 if (res)
c4307285 2406 return tipc_cfg_reply_error_string("cannot change link setting");
b97bf3fd 2407
4323add6 2408 return tipc_cfg_reply_none();
b97bf3fd
PL
2409}
2410
2411/**
2412 * link_reset_statistics - reset link statistics
2413 * @l_ptr: pointer to link
2414 */
a18c4bc3 2415static void link_reset_statistics(struct tipc_link *l_ptr)
b97bf3fd
PL
2416{
2417 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2418 l_ptr->stats.sent_info = l_ptr->next_out_no;
2419 l_ptr->stats.recv_info = l_ptr->next_in_no;
2420}
2421
4323add6 2422struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
2423{
2424 char *link_name;
a18c4bc3 2425 struct tipc_link *l_ptr;
6c00055a 2426 struct tipc_node *node;
e099e86c 2427 unsigned int bearer_id;
b97bf3fd
PL
2428
2429 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 2430 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd
PL
2431
2432 link_name = (char *)TLV_DATA(req_tlv_area);
4323add6
PL
2433 if (!strcmp(link_name, tipc_bclink_name)) {
2434 if (tipc_bclink_reset_stats())
2435 return tipc_cfg_reply_error_string("link not found");
2436 return tipc_cfg_reply_none();
b97bf3fd 2437 }
e099e86c 2438 node = tipc_link_find_owner(link_name, &bearer_id);
7216cd94 2439 if (!node)
e099e86c 2440 return tipc_cfg_reply_error_string("link not found");
7216cd94 2441
a11607f5 2442 tipc_node_lock(node);
e099e86c 2443 l_ptr = node->links[bearer_id];
b97bf3fd 2444 if (!l_ptr) {
e099e86c 2445 tipc_node_unlock(node);
4323add6 2446 return tipc_cfg_reply_error_string("link not found");
b97bf3fd 2447 }
b97bf3fd 2448 link_reset_statistics(l_ptr);
4323add6 2449 tipc_node_unlock(node);
4323add6 2450 return tipc_cfg_reply_none();
b97bf3fd
PL
2451}
2452
2453/**
2454 * percent - convert count to a percentage of total (rounding up or down)
2455 */
b97bf3fd
PL
2456static u32 percent(u32 count, u32 total)
2457{
2458 return (count * 100 + (total / 2)) / total;
2459}
2460
2461/**
4323add6 2462 * tipc_link_stats - print link statistics
b97bf3fd
PL
2463 * @name: link name
2464 * @buf: print buffer area
2465 * @buf_size: size of print buffer area
c4307285 2466 *
b97bf3fd
PL
2467 * Returns length of print buffer data string (or 0 if error)
2468 */
4323add6 2469static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
b97bf3fd 2470{
dc1aed37
EH
2471 struct tipc_link *l;
2472 struct tipc_stats *s;
6c00055a 2473 struct tipc_node *node;
b97bf3fd
PL
2474 char *status;
2475 u32 profile_total = 0;
e099e86c 2476 unsigned int bearer_id;
dc1aed37 2477 int ret;
b97bf3fd 2478
4323add6
PL
2479 if (!strcmp(name, tipc_bclink_name))
2480 return tipc_bclink_stats(buf, buf_size);
b97bf3fd 2481
e099e86c 2482 node = tipc_link_find_owner(name, &bearer_id);
7216cd94 2483 if (!node)
b97bf3fd 2484 return 0;
7216cd94 2485
4323add6 2486 tipc_node_lock(node);
e099e86c
JPM
2487
2488 l = node->links[bearer_id];
2489 if (!l) {
2490 tipc_node_unlock(node);
e099e86c
JPM
2491 return 0;
2492 }
2493
dc1aed37 2494 s = &l->stats;
b97bf3fd 2495
dc1aed37 2496 if (tipc_link_is_active(l))
b97bf3fd 2497 status = "ACTIVE";
dc1aed37 2498 else if (tipc_link_is_up(l))
b97bf3fd
PL
2499 status = "STANDBY";
2500 else
2501 status = "DEFUNCT";
dc1aed37
EH
2502
2503 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2504 " %s MTU:%u Priority:%u Tolerance:%u ms"
2505 " Window:%u packets\n",
2506 l->name, status, l->max_pkt, l->priority,
2507 l->tolerance, l->queue_limit[0]);
2508
2509 ret += tipc_snprintf(buf + ret, buf_size - ret,
2510 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2511 l->next_in_no - s->recv_info, s->recv_fragments,
2512 s->recv_fragmented, s->recv_bundles,
2513 s->recv_bundled);
2514
2515 ret += tipc_snprintf(buf + ret, buf_size - ret,
2516 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2517 l->next_out_no - s->sent_info, s->sent_fragments,
2518 s->sent_fragmented, s->sent_bundles,
2519 s->sent_bundled);
2520
2521 profile_total = s->msg_length_counts;
b97bf3fd
PL
2522 if (!profile_total)
2523 profile_total = 1;
dc1aed37
EH
2524
2525 ret += tipc_snprintf(buf + ret, buf_size - ret,
2526 " TX profile sample:%u packets average:%u octets\n"
2527 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2528 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2529 s->msg_length_counts,
2530 s->msg_lengths_total / profile_total,
2531 percent(s->msg_length_profile[0], profile_total),
2532 percent(s->msg_length_profile[1], profile_total),
2533 percent(s->msg_length_profile[2], profile_total),
2534 percent(s->msg_length_profile[3], profile_total),
2535 percent(s->msg_length_profile[4], profile_total),
2536 percent(s->msg_length_profile[5], profile_total),
2537 percent(s->msg_length_profile[6], profile_total));
2538
2539 ret += tipc_snprintf(buf + ret, buf_size - ret,
2540 " RX states:%u probes:%u naks:%u defs:%u"
2541 " dups:%u\n", s->recv_states, s->recv_probes,
2542 s->recv_nacks, s->deferred_recv, s->duplicates);
2543
2544 ret += tipc_snprintf(buf + ret, buf_size - ret,
2545 " TX states:%u probes:%u naks:%u acks:%u"
2546 " dups:%u\n", s->sent_states, s->sent_probes,
2547 s->sent_nacks, s->sent_acks, s->retransmitted);
2548
2549 ret += tipc_snprintf(buf + ret, buf_size - ret,
3c294cb3
YX
2550 " Congestion link:%u Send queue"
2551 " max:%u avg:%u\n", s->link_congs,
dc1aed37
EH
2552 s->max_queue_sz, s->queue_sz_counts ?
2553 (s->accu_queue_sz / s->queue_sz_counts) : 0);
b97bf3fd 2554
4323add6 2555 tipc_node_unlock(node);
dc1aed37 2556 return ret;
b97bf3fd
PL
2557}
2558
4323add6 2559struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
2560{
2561 struct sk_buff *buf;
2562 struct tlv_desc *rep_tlv;
2563 int str_len;
dc1aed37
EH
2564 int pb_len;
2565 char *pb;
b97bf3fd
PL
2566
2567 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
4323add6 2568 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 2569
dc1aed37 2570 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
b97bf3fd
PL
2571 if (!buf)
2572 return NULL;
2573
2574 rep_tlv = (struct tlv_desc *)buf->data;
dc1aed37
EH
2575 pb = TLV_DATA(rep_tlv);
2576 pb_len = ULTRA_STRING_MAX_LEN;
4323add6 2577 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
dc1aed37 2578 pb, pb_len);
b97bf3fd 2579 if (!str_len) {
5f6d9123 2580 kfree_skb(buf);
c4307285 2581 return tipc_cfg_reply_error_string("link not found");
b97bf3fd 2582 }
dc1aed37 2583 str_len += 1; /* for "\0" */
b97bf3fd
PL
2584 skb_put(buf, TLV_SPACE(str_len));
2585 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2586
2587 return buf;
2588}
2589
b97bf3fd 2590/**
4323add6 2591 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
b97bf3fd
PL
2592 * @dest: network address of destination node
2593 * @selector: used to select from set of active links
c4307285 2594 *
b97bf3fd
PL
2595 * If no active link can be found, uses default maximum packet size.
2596 */
4323add6 2597u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
b97bf3fd 2598{
6c00055a 2599 struct tipc_node *n_ptr;
a18c4bc3 2600 struct tipc_link *l_ptr;
b97bf3fd 2601 u32 res = MAX_PKT_DEFAULT;
c4307285 2602
b97bf3fd
PL
2603 if (dest == tipc_own_addr)
2604 return MAX_MSG_SIZE;
2605
51a8e4de 2606 n_ptr = tipc_node_find(dest);
b97bf3fd 2607 if (n_ptr) {
4323add6 2608 tipc_node_lock(n_ptr);
b97bf3fd
PL
2609 l_ptr = n_ptr->active_links[selector & 1];
2610 if (l_ptr)
15e979da 2611 res = l_ptr->max_pkt;
4323add6 2612 tipc_node_unlock(n_ptr);
b97bf3fd 2613 }
b97bf3fd
PL
2614 return res;
2615}
2616
a18c4bc3 2617static void link_print(struct tipc_link *l_ptr, const char *str)
b97bf3fd 2618{
7a2f7d18
YX
2619 struct tipc_bearer *b_ptr;
2620
2621 rcu_read_lock();
2622 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2623 if (b_ptr)
2624 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2625 rcu_read_unlock();
8d64a5ba 2626
b97bf3fd 2627 if (link_working_unknown(l_ptr))
5deedde9 2628 pr_cont(":WU\n");
8d64a5ba 2629 else if (link_reset_reset(l_ptr))
5deedde9 2630 pr_cont(":RR\n");
8d64a5ba 2631 else if (link_reset_unknown(l_ptr))
5deedde9 2632 pr_cont(":RU\n");
8d64a5ba 2633 else if (link_working_working(l_ptr))
5deedde9
PG
2634 pr_cont(":WW\n");
2635 else
2636 pr_cont("\n");
b97bf3fd 2637}