blob: 96a8072f73cccb078c200fd01990ed8bfcbaf843 [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 Maloy170b3922014-01-07 17:02:41 -05004 * Copyright (c) 1996-2007, 2012-2014, 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040040#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "discover.h"
43#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010044
Ying Xue796c75d2013-06-17 10:54:48 -040045#include <linux/pkt_sched.h>
46
Erik Hugne2cf8aa12012-06-29 00:16:37 -040047/*
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 ";
Per Lidenb97bf3f2006-01-02 19:04:38 +010053
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090054/*
Allan Stephensa686e682008-06-04 17:29:39 -070055 * Out-of-range value for link session numbers
56 */
Allan Stephensa686e682008-06-04 17:29:39 -070057#define INVALID_SESSION 0x10000
58
59/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090060 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010061 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010062#define STARTING_EVT 856384768 /* link processing trigger */
63#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
64#define TIMEOUT_EVT 560817u /* link timer expired */
65
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090066/*
67 * The following two 'message types' is really just implementation
68 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010069 * They must not be considered part of the protocol
70 */
71#define OPEN_MSG 0
72#define CLOSED_MSG 1
73
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090074/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010075 * State value stored in 'exp_msg_count'
76 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010077#define START_CHANGEOVER 100000u
78
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050079static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +010080 struct sk_buff *buf);
Ying Xue247f0f32014-02-18 16:06:46 +080081static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Paul Maloy3bb53382014-02-13 17:29:12 -050082static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -050083 struct sk_buff **buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050084static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050085static 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);
Ying Xue247f0f32014-02-18 16:06:46 +080088static 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);
stephen hemminger31e3c3f2010-10-13 13:20:35 +000091
Per Lidenb97bf3f2006-01-02 19:04:38 +010092/*
Sam Ravnborg05790c62006-03-20 22:37:04 -080093 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080095static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +010096{
97 return (i + 3) & ~3u;
98}
99
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500100static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800102 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900104
Ying Xue7a2f7d12014-04-21 10:55:46 +0800105 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
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 if (max_pkt > MAX_MSG_SIZE)
115 max_pkt = MAX_MSG_SIZE;
116
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900117 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
119 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900120 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 l_ptr->max_pkt = MAX_PKT_DEFAULT;
122
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124}
125
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500126static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
128 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400129 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130 return mod(l_ptr->next_out_no);
131}
132
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500133static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134{
135 return mod(link_next_sent(l_ptr) - 1);
136}
137
138/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800139 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500141int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142{
143 if (!l_ptr)
144 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000145 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146}
147
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500148int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000150 return (l_ptr->owner->active_links[0] == l_ptr) ||
151 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152}
153
154/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 * link_timeout - handle expiration of link timer
156 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500158static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159{
Per Liden4323add2006-01-18 00:38:21 +0100160 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161
162 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
164 l_ptr->stats.queue_sz_counts++;
165
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 if (l_ptr->first_out) {
167 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
168 u32 length = msg_size(msg);
169
Joe Perchesf64f9e72009-11-29 16:55:45 -0800170 if ((msg_user(msg) == MSG_FRAGMENTER) &&
171 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 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 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195
196 link_state_event(l_ptr, TIMEOUT_EVT);
197
198 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100199 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200
Per Liden4323add2006-01-18 00:38:21 +0100201 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202}
203
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500204static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205{
206 k_start_timer(&l_ptr->timer, time);
207}
208
209/**
Per Liden4323add2006-01-18 00:38:21 +0100210 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500211 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900214 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 * Returns pointer to link.
216 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500217struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500218 struct tipc_bearer *b_ptr,
219 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500221 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 struct tipc_msg *msg;
223 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500224 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);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400229 pr_err("Attempt to establish third link to %s\n", addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500230 return NULL;
231 }
232
233 if (n_ptr->links[b_ptr->identity]) {
234 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400235 pr_err("Attempt to establish second link on <%s> to %s\n",
236 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500237 return NULL;
238 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700240 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400242 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 return NULL;
244 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245
246 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500247 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400248 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900250 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 if_name,
252 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400253 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500255 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400257 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800258 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400259 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 l_ptr->state = RESET_UNKNOWN;
261
262 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
263 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000264 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700266 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 msg_set_bearer_id(msg, b_ptr->identity);
268 strcpy((char *)msg_data(msg), if_name);
269
270 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400271 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272
Ying Xue7a2f7d12014-04-21 10:55:46 +0800273 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274 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
Allan Stephens37b9c082011-02-28 11:32:27 -0500281 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500283 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
284 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500285
286 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 return l_ptr;
289}
290
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500291void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500292{
293 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500294 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500295
Ying Xue6c7a7622014-03-27 12:54:37 +0800296 rcu_read_lock();
297 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800298 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500299 l_ptr = n_ptr->links[bearer_id];
300 if (l_ptr) {
301 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500302 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);
Ying Xue5356f3d2014-05-05 08:56:09 +0800305 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500306
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500307 /* 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;
Ying Xue5356f3d2014-05-05 08:56:09 +0800313 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500314 del_timer_sync(&l_ptr->timer);
315 }
Ying Xuec61dd612014-02-13 17:29:09 -0500316 continue;
317 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800318 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500319 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800320 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500321}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
323/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900324 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 * @l_ptr: pointer to link
326 * @origport: reference to sending port
327 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900328 *
329 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 * has abated.
331 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500332static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500334 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335
Per Liden4323add2006-01-18 00:38:21 +0100336 spin_lock_bh(&tipc_port_list_lock);
337 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 if (p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 if (!list_empty(&p_ptr->wait_list))
340 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500341 p_ptr->congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000342 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
344 l_ptr->stats.link_congs++;
345exit:
Per Liden4323add2006-01-18 00:38:21 +0100346 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 }
Per Liden4323add2006-01-18 00:38:21 +0100348 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 return -ELINKCONG;
350}
351
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500352void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500354 struct tipc_port *p_ptr;
355 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
357
358 if (all)
359 win = 100000;
360 if (win <= 0)
361 return;
Per Liden4323add2006-01-18 00:38:21 +0100362 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 return;
364 if (link_congested(l_ptr))
365 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900366 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 wait_list) {
368 if (win <= 0)
369 break;
370 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500371 spin_lock_bh(p_ptr->lock);
372 p_ptr->congested = 0;
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400373 tipc_port_wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500375 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 }
377
378exit:
Per Liden4323add2006-01-18 00:38:21 +0100379 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380}
381
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900382/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 * link_release_outqueue - purge link's outbound message queue
384 * @l_ptr: pointer to link
385 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500386static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387{
Ying Xued77b3832013-12-10 20:45:38 -0800388 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 l_ptr->first_out = NULL;
390 l_ptr->out_queue_size = 0;
391}
392
393/**
Per Liden4323add2006-01-18 00:38:21 +0100394 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 * @l_ptr: pointer to link
396 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500397void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400399 kfree_skb(l_ptr->reasm_buf);
400 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401}
402
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900403/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500404 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 * @l_ptr: pointer to link
406 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500407void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408{
Ying Xued77b3832013-12-10 20:45:38 -0800409 kfree_skb_list(l_ptr->oldest_deferred_in);
410 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100411 tipc_link_reset_fragments(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400412 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 l_ptr->proto_msg_queue = NULL;
414}
415
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500416void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 u32 prev_state = l_ptr->state;
419 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700420 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421
Allan Stephensa686e682008-06-04 17:29:39 -0700422 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423
Allan Stephensa686e682008-06-04 17:29:39 -0700424 /* Link is down, accept any session */
425 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900427 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900429
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431
432 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
433 return;
434
Per Liden4323add2006-01-18 00:38:21 +0100435 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800436 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000437
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500438 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 l_ptr->reset_checkpoint = checkpoint;
440 l_ptr->exp_msg_count = START_CHANGEOVER;
441 }
442
443 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400445 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 l_ptr->proto_msg_queue = NULL;
Ying Xued77b3832013-12-10 20:45:38 -0800447 kfree_skb_list(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100449 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450
451 l_ptr->retransm_queue_head = 0;
452 l_ptr->retransm_queue_size = 0;
453 l_ptr->last_out = NULL;
454 l_ptr->first_out = NULL;
455 l_ptr->next_out = NULL;
456 l_ptr->unacked_window = 0;
457 l_ptr->checkpoint = 1;
458 l_ptr->next_out_no = 1;
459 l_ptr->deferred_inqueue_sz = 0;
460 l_ptr->oldest_deferred_in = NULL;
461 l_ptr->newest_deferred_in = NULL;
462 l_ptr->fsm_msg_cnt = 0;
463 l_ptr->stale_count = 0;
464 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465}
466
Ying Xuec61dd612014-02-13 17:29:09 -0500467void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500468{
469 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500470 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500471
Ying Xue6c7a7622014-03-27 12:54:37 +0800472 rcu_read_lock();
473 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800474 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500475 l_ptr = n_ptr->links[bearer_id];
476 if (l_ptr)
477 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800478 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500479 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800480 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500481}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500483static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484{
Allan Stephens5392d642006-06-25 23:52:50 -0700485 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100486 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800487 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488}
489
490/**
491 * link_state_event - link finite state machine
492 * @l_ptr: pointer to link
493 * @event: state machine event to process
494 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000495static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500497 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 u32 cont_intv = l_ptr->continuity_interval;
499
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500500 if (l_ptr->flags & LINK_STOPPED)
501 return;
502
Ying Xue135daee2014-02-13 17:29:08 -0500503 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 return; /* Not yet. */
505
Ying Xue77a7e072013-12-10 20:45:44 -0800506 /* Check whether changeover is going on */
507 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000508 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800510 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512
513 switch (l_ptr->state) {
514 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 switch (event) {
516 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 break;
519 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 if (l_ptr->next_in_no != l_ptr->checkpoint) {
521 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100522 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800523 tipc_link_proto_xmit(l_ptr, STATE_MSG,
524 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 l_ptr->fsm_msg_cnt++;
526 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800527 tipc_link_proto_xmit(l_ptr, STATE_MSG,
528 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 l_ptr->fsm_msg_cnt++;
530 }
531 link_set_timer(l_ptr, cont_intv);
532 break;
533 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 l_ptr->state = WORKING_UNKNOWN;
535 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800536 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 l_ptr->fsm_msg_cnt++;
538 link_set_timer(l_ptr, cont_intv / 4);
539 break;
540 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400541 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
542 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100543 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 l_ptr->state = RESET_RESET;
545 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800546 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
547 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 l_ptr->fsm_msg_cnt++;
549 link_set_timer(l_ptr, cont_intv);
550 break;
551 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400552 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 }
554 break;
555 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 switch (event) {
557 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 l_ptr->state = WORKING_WORKING;
560 l_ptr->fsm_msg_cnt = 0;
561 link_set_timer(l_ptr, cont_intv);
562 break;
563 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400564 pr_info("%s<%s>, requested by peer while probing\n",
565 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100566 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 l_ptr->state = RESET_RESET;
568 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800569 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
570 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 l_ptr->fsm_msg_cnt++;
572 link_set_timer(l_ptr, cont_intv);
573 break;
574 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 l_ptr->state = WORKING_WORKING;
577 l_ptr->fsm_msg_cnt = 0;
578 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100579 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800580 tipc_link_proto_xmit(l_ptr, STATE_MSG,
581 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 l_ptr->fsm_msg_cnt++;
583 }
584 link_set_timer(l_ptr, cont_intv);
585 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800586 tipc_link_proto_xmit(l_ptr, STATE_MSG,
587 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 l_ptr->fsm_msg_cnt++;
589 link_set_timer(l_ptr, cont_intv / 4);
590 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400591 pr_warn("%s<%s>, peer not responding\n",
592 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100593 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 l_ptr->state = RESET_UNKNOWN;
595 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800596 tipc_link_proto_xmit(l_ptr, RESET_MSG,
597 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 l_ptr->fsm_msg_cnt++;
599 link_set_timer(l_ptr, cont_intv);
600 }
601 break;
602 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400603 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 }
605 break;
606 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 switch (event) {
608 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 break;
610 case ACTIVATE_MSG:
611 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000612 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 l_ptr->state = WORKING_WORKING;
615 l_ptr->fsm_msg_cnt = 0;
616 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800617 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800619 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800620 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 link_set_timer(l_ptr, cont_intv);
622 break;
623 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->state = RESET_RESET;
625 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800626 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
627 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 l_ptr->fsm_msg_cnt++;
629 link_set_timer(l_ptr, cont_intv);
630 break;
631 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500632 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 /* fall through */
634 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800635 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 l_ptr->fsm_msg_cnt++;
637 link_set_timer(l_ptr, cont_intv);
638 break;
639 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400640 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 }
642 break;
643 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 switch (event) {
645 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 case ACTIVATE_MSG:
647 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000648 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 l_ptr->state = WORKING_WORKING;
651 l_ptr->fsm_msg_cnt = 0;
652 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800653 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800655 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800656 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 link_set_timer(l_ptr, cont_intv);
658 break;
659 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 break;
661 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800662 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
663 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 l_ptr->fsm_msg_cnt++;
665 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 break;
667 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400668 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 }
670 break;
671 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400672 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 }
674}
675
676/*
677 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900678 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400680static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 struct sk_buff *buf)
682{
683 struct tipc_msg *bundler_msg = buf_msg(bundler);
684 struct tipc_msg *msg = buf_msg(buf);
685 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700686 u32 bundle_size = msg_size(bundler_msg);
687 u32 to_pos = align(bundle_size);
688 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689
690 if (msg_user(bundler_msg) != MSG_BUNDLER)
691 return 0;
692 if (msg_type(bundler_msg) != OPEN_MSG)
693 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700694 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000696 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700697 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698
Allan Stephense49060c2006-06-29 12:32:46 -0700699 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300700 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701 msg_set_size(bundler_msg, to_pos + size);
702 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400703 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704 l_ptr->stats.sent_bundled++;
705 return 1;
706}
707
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500708static void link_add_to_outqueue(struct tipc_link *l_ptr,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800709 struct sk_buff *buf,
710 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711{
712 u32 ack = mod(l_ptr->next_in_no - 1);
713 u32 seqno = mod(l_ptr->next_out_no++);
714
715 msg_set_word(msg, 2, ((ack << 16) | seqno));
716 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
717 buf->next = NULL;
718 if (l_ptr->first_out) {
719 l_ptr->last_out->next = buf;
720 l_ptr->last_out = buf;
721 } else
722 l_ptr->first_out = l_ptr->last_out = buf;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500723
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 l_ptr->out_queue_size++;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500725 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
726 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727}
728
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500729static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
Allan Stephensdc63d912011-04-21 11:50:42 -0400730 struct sk_buff *buf_chain,
731 u32 long_msgno)
732{
733 struct sk_buff *buf;
734 struct tipc_msg *msg;
735
736 if (!l_ptr->next_out)
737 l_ptr->next_out = buf_chain;
738 while (buf_chain) {
739 buf = buf_chain;
740 buf_chain = buf_chain->next;
741
742 msg = buf_msg(buf);
743 msg_set_long_msgno(msg, long_msgno);
744 link_add_to_outqueue(l_ptr, buf, msg);
745 }
746}
747
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900748/*
Ying Xue247f0f32014-02-18 16:06:46 +0800749 * tipc_link_xmit() is the 'full path' for messages, called from
750 * inside TIPC when the 'fast path' in tipc_send_xmit
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 * has failed, and from link_send()
752 */
Ying Xue247f0f32014-02-18 16:06:46 +0800753int __tipc_link_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754{
755 struct tipc_msg *msg = buf_msg(buf);
756 u32 size = msg_size(msg);
757 u32 dsz = msg_data_sz(msg);
758 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000759 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000761 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 /* Match msg importance against queue limits: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 if (unlikely(queue_size >= queue_limit)) {
765 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
Allan Stephensbebc55a2011-04-19 10:17:58 -0400766 link_schedule_port(l_ptr, msg_origport(msg), size);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400767 kfree_skb(buf);
Allan Stephensbebc55a2011-04-19 10:17:58 -0400768 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 }
Allan Stephens5f6d9122011-11-04 13:24:29 -0400770 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 if (imp > CONN_MANAGER) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400772 pr_warn("%s<%s>, send queue full", link_rst_msg,
773 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100774 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 }
776 return dsz;
777 }
778
779 /* Fragmentation needed ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 if (size > max_packet)
Ying Xue247f0f32014-02-18 16:06:46 +0800781 return tipc_link_frag_xmit(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400783 /* Packet can be queued or sent. */
Erik Hugne512137e2013-12-06 10:08:00 -0500784 if (likely(!link_congested(l_ptr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 link_add_to_outqueue(l_ptr, buf, msg);
786
Ying Xue7a2f7d12014-04-21 10:55:46 +0800787 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800788 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 return dsz;
790 }
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400791 /* Congestion: can message be bundled ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
793 (msg_user(msg) != MSG_FRAGMENTER)) {
794
795 /* Try adding message to an existing bundle */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900796 if (l_ptr->next_out &&
Ying Xue3c294cb2012-11-15 11:34:45 +0800797 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 return dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799
800 /* Try creating a new bundle */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000802 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 struct tipc_msg bundler_hdr;
804
805 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000806 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700807 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300808 skb_copy_to_linear_data(bundler, &bundler_hdr,
809 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 skb_trim(bundler, INT_H_SIZE);
811 link_bundle_buf(l_ptr, bundler, buf);
812 buf = bundler;
813 msg = buf_msg(buf);
814 l_ptr->stats.sent_bundles++;
815 }
816 }
817 }
818 if (!l_ptr->next_out)
819 l_ptr->next_out = buf;
820 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 return dsz;
822}
823
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900824/*
Ying Xue247f0f32014-02-18 16:06:46 +0800825 * tipc_link_xmit(): same as __tipc_link_xmit(), but the link to use
826 * has not been selected yet, and the the owner node is not locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 * Called by TIPC internal users, e.g. the name distributor
828 */
Ying Xue247f0f32014-02-18 16:06:46 +0800829int tipc_link_xmit(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500831 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700832 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 int res = -ELINKCONG;
834
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000835 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100837 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000839 if (l_ptr)
Ying Xue247f0f32014-02-18 16:06:46 +0800840 res = __tipc_link_xmit(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000841 else
Allan Stephens5f6d9122011-11-04 13:24:29 -0400842 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100843 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 } else {
Allan Stephens5f6d9122011-11-04 13:24:29 -0400845 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 return res;
848}
849
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500850/* tipc_link_cong: determine return value and how to treat the
851 * sent buffer during link congestion.
852 * - For plain, errorless user data messages we keep the buffer and
853 * return -ELINKONG.
854 * - For all other messages we discard the buffer and return -EHOSTUNREACH
855 * - For TIPC internal messages we also reset the link
856 */
857static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
858{
859 struct tipc_msg *msg = buf_msg(buf);
860 uint psz = msg_size(msg);
861 uint imp = tipc_msg_tot_importance(msg);
862 u32 oport = msg_tot_origport(msg);
863
864 if (likely(imp <= TIPC_CRITICAL_IMPORTANCE)) {
865 if (!msg_errcode(msg) && !msg_reroute_cnt(msg)) {
866 link_schedule_port(link, oport, psz);
867 return -ELINKCONG;
868 }
869 } else {
870 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
871 tipc_link_reset(link);
872 }
873 kfree_skb_list(buf);
874 return -EHOSTUNREACH;
875}
876
877/**
878 * __tipc_link_xmit2(): same as tipc_link_xmit2, but destlink is known & locked
879 * @link: link to use
880 * @buf: chain of buffers containing message
881 * Consumes the buffer chain, except when returning -ELINKCONG
882 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
883 * user data messages) or -EHOSTUNREACH (all other messages/senders)
884 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
885 * to act on the return value, since they may need to do more send attempts.
886 */
887int __tipc_link_xmit2(struct tipc_link *link, struct sk_buff *buf)
888{
889 struct tipc_msg *msg = buf_msg(buf);
890 uint psz = msg_size(msg);
891 uint qsz = link->out_queue_size;
892 uint sndlim = link->queue_limit[0];
893 uint imp = tipc_msg_tot_importance(msg);
894 uint mtu = link->max_pkt;
895 uint ack = mod(link->next_in_no - 1);
896 uint seqno = link->next_out_no;
897 uint bc_last_in = link->owner->bclink.last_in;
898 struct tipc_media_addr *addr = &link->media_addr;
899 struct sk_buff *next = buf->next;
900
901 /* Match queue limits against msg importance: */
902 if (unlikely(qsz >= link->queue_limit[imp]))
903 return tipc_link_cong(link, buf);
904
905 /* Has valid packet limit been used ? */
906 if (unlikely(psz > mtu)) {
907 kfree_skb_list(buf);
908 return -EMSGSIZE;
909 }
910
911 /* Prepare each packet for sending, and add to outqueue: */
912 while (buf) {
913 next = buf->next;
914 msg = buf_msg(buf);
915 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
916 msg_set_bcast_ack(msg, bc_last_in);
917
918 if (!link->first_out) {
919 link->first_out = buf;
920 } else if (qsz < sndlim) {
921 link->last_out->next = buf;
922 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
923 link->stats.sent_bundled++;
924 buf = next;
925 next = buf->next;
926 continue;
927 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
928 link->stats.sent_bundled++;
929 link->stats.sent_bundles++;
930 link->last_out->next = buf;
931 if (!link->next_out)
932 link->next_out = buf;
933 } else {
934 link->last_out->next = buf;
935 if (!link->next_out)
936 link->next_out = buf;
937 }
938
939 /* Send packet if possible: */
940 if (likely(++qsz <= sndlim)) {
941 tipc_bearer_send(link->bearer_id, buf, addr);
942 link->next_out = next;
943 link->unacked_window = 0;
944 }
945 seqno++;
946 link->last_out = buf;
947 buf = next;
948 }
949 link->next_out_no = seqno;
950 link->out_queue_size = qsz;
951 return 0;
952}
953
954/**
955 * tipc_link_xmit2() is the general link level function for message sending
956 * @buf: chain of buffers containing message
957 * @dsz: amount of user data to be sent
958 * @dnode: address of destination node
959 * @selector: a number used for deterministic link selection
960 * Consumes the buffer chain, except when returning -ELINKCONG
961 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
962 */
963int tipc_link_xmit2(struct sk_buff *buf, u32 dnode, u32 selector)
964{
965 struct tipc_link *link = NULL;
966 struct tipc_node *node;
967 int rc = -EHOSTUNREACH;
968
969 node = tipc_node_find(dnode);
970 if (node) {
971 tipc_node_lock(node);
972 link = node->active_links[selector & 1];
973 if (link)
974 rc = __tipc_link_xmit2(link, buf);
975 tipc_node_unlock(node);
976 }
977
978 if (link)
979 return rc;
980
981 if (likely(in_own_node(dnode)))
982 return tipc_sk_rcv(buf);
983
984 kfree_skb_list(buf);
985 return rc;
986}
987
Jon Maloyc64f7a62012-11-16 13:51:31 +0800988/*
Ying Xue247f0f32014-02-18 16:06:46 +0800989 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800990 *
991 * Give a newly added peer node the sequence number where it should
992 * start receiving and acking broadcast packets.
993 *
994 * Called with node locked
995 */
Ying Xue247f0f32014-02-18 16:06:46 +0800996static void tipc_link_sync_xmit(struct tipc_link *l)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800997{
998 struct sk_buff *buf;
999 struct tipc_msg *msg;
1000
1001 buf = tipc_buf_acquire(INT_H_SIZE);
1002 if (!buf)
1003 return;
1004
1005 msg = buf_msg(buf);
1006 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
1007 msg_set_last_bcast(msg, l->owner->bclink.acked);
1008 link_add_chain_to_outqueue(l, buf, 0);
1009 tipc_link_push_queue(l);
1010}
1011
1012/*
Ying Xue247f0f32014-02-18 16:06:46 +08001013 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +08001014 * Receive the sequence number where we should start receiving and
1015 * acking broadcast packets from a newly added peer node, and open
1016 * up for reception of such packets.
1017 *
1018 * Called with node locked
1019 */
Ying Xue247f0f32014-02-18 16:06:46 +08001020static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +08001021{
1022 struct tipc_msg *msg = buf_msg(buf);
1023
1024 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
1025 n->bclink.recv_permitted = true;
1026 kfree_skb(buf);
1027}
1028
1029/*
Ying Xue247f0f32014-02-18 16:06:46 +08001030 * tipc_link_names_xmit - send name table entries to new neighbor
Allan Stephens9aa88c22011-05-31 13:38:02 -04001031 *
1032 * Send routine for bulk delivery of name table messages when contact
1033 * with a new neighbor occurs. No link congestion checking is performed
1034 * because name table messages *must* be delivered. The messages must be
1035 * small enough not to require fragmentation.
1036 * Called without any locks held.
1037 */
Ying Xue247f0f32014-02-18 16:06:46 +08001038void tipc_link_names_xmit(struct list_head *message_list, u32 dest)
Allan Stephens9aa88c22011-05-31 13:38:02 -04001039{
1040 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001041 struct tipc_link *l_ptr;
Allan Stephens9aa88c22011-05-31 13:38:02 -04001042 struct sk_buff *buf;
1043 struct sk_buff *temp_buf;
1044
1045 if (list_empty(message_list))
1046 return;
1047
Allan Stephens9aa88c22011-05-31 13:38:02 -04001048 n_ptr = tipc_node_find(dest);
1049 if (n_ptr) {
1050 tipc_node_lock(n_ptr);
1051 l_ptr = n_ptr->active_links[0];
1052 if (l_ptr) {
1053 /* convert circular list to linear list */
1054 ((struct sk_buff *)message_list->prev)->next = NULL;
1055 link_add_chain_to_outqueue(l_ptr,
1056 (struct sk_buff *)message_list->next, 0);
1057 tipc_link_push_queue(l_ptr);
1058 INIT_LIST_HEAD(message_list);
1059 }
1060 tipc_node_unlock(n_ptr);
1061 }
Allan Stephens9aa88c22011-05-31 13:38:02 -04001062
1063 /* discard the messages if they couldn't be sent */
Allan Stephens9aa88c22011-05-31 13:38:02 -04001064 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
1065 list_del((struct list_head *)buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -04001066 kfree_skb(buf);
Allan Stephens9aa88c22011-05-31 13:38:02 -04001067 }
1068}
1069
1070/*
Per Liden4323add2006-01-18 00:38:21 +01001071 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 */
stephen hemminger98056962014-01-04 13:47:48 -08001073static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074{
1075 struct sk_buff *buf = l_ptr->first_out;
1076 u32 r_q_size = l_ptr->retransm_queue_size;
1077 u32 r_q_head = l_ptr->retransm_queue_head;
1078
1079 /* Step to position where retransmission failed, if any, */
1080 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001082 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -04001084 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085
1086 while (buf && less(first, r_q_head)) {
1087 first = mod(first + 1);
1088 buf = buf->next;
1089 }
1090 l_ptr->retransm_queue_head = r_q_head = first;
1091 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1092 }
1093
1094 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +00001095 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001097 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001098 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001099 l_ptr->retransm_queue_head = mod(++r_q_head);
1100 l_ptr->retransm_queue_size = --r_q_size;
1101 l_ptr->stats.retransmitted++;
1102 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 }
1104
1105 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 buf = l_ptr->proto_msg_queue;
1107 if (buf) {
1108 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001109 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001110 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001111 l_ptr->unacked_window = 0;
1112 kfree_skb(buf);
1113 l_ptr->proto_msg_queue = NULL;
1114 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 }
1116
1117 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 buf = l_ptr->next_out;
1119 if (buf) {
1120 struct tipc_msg *msg = buf_msg(buf);
1121 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -04001122 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123
1124 if (mod(next - first) < l_ptr->queue_limit[0]) {
1125 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001126 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001127 tipc_bearer_send(l_ptr->bearer_id, buf,
1128 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001129 if (msg_user(msg) == MSG_BUNDLER)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -05001130 msg_set_type(msg, BUNDLE_CLOSED);
Ying Xue3c294cb2012-11-15 11:34:45 +08001131 l_ptr->next_out = buf->next;
1132 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 }
1134 }
Ying Xue3c294cb2012-11-15 11:34:45 +08001135 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136}
1137
1138/*
1139 * push_queue(): push out the unsent messages of a link where
1140 * congestion has abated. Node is locked
1141 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001142void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143{
1144 u32 res;
1145
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 do {
Per Liden4323add2006-01-18 00:38:21 +01001147 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001148 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149}
1150
Ying Xue3f5a12b2014-05-05 08:56:17 +08001151void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001152{
Allan Stephensd356eeb2006-06-25 23:40:01 -07001153 char addr_string[16];
1154 u32 i;
1155
Ying Xue3f5a12b2014-05-05 08:56:17 +08001156 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001157
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001158 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +08001159 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001160
1161 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +08001162 if (node->links[i]) {
1163 link_print(node->links[i], "Resetting link\n");
1164 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001165 }
1166 }
1167
Ying Xue3f5a12b2014-05-05 08:56:17 +08001168 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001169}
1170
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001171static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001172 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001173{
1174 struct tipc_msg *msg = buf_msg(buf);
1175
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001176 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001177
1178 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001179 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001180 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001181 tipc_link_reset(l_ptr);
1182
1183 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001184 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -07001185 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001186 char addr_string[16];
1187
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001188 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1189 pr_cont("Outstanding acks: %lu\n",
1190 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001191
Allan Stephens01d83ed2011-01-18 13:53:16 -05001192 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -07001193 tipc_node_lock(n_ptr);
1194
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001195 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001196 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +08001197 pr_info("Reception permitted: %d, Acked: %u\n",
1198 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001199 n_ptr->bclink.acked);
1200 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1201 n_ptr->bclink.last_in,
1202 n_ptr->bclink.oos_state,
1203 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001204
Allan Stephensd356eeb2006-06-25 23:40:01 -07001205 tipc_node_unlock(n_ptr);
1206
Ying Xue3f5a12b2014-05-05 08:56:17 +08001207 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001208 l_ptr->stale_count = 0;
1209 }
1210}
1211
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001212void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001213 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214{
1215 struct tipc_msg *msg;
1216
Allan Stephensd356eeb2006-06-25 23:40:01 -07001217 if (!buf)
1218 return;
1219
1220 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001221
Erik Hugne512137e2013-12-06 10:08:00 -05001222 /* Detect repeated retransmit failures */
1223 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1224 if (++l_ptr->stale_count > 100) {
1225 link_retransmit_failure(l_ptr, buf);
1226 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001227 }
1228 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001229 l_ptr->last_retransmitted = msg_seqno(msg);
1230 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001232
Neil Hormanca509102010-03-15 07:58:45 +00001233 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 msg = buf_msg(buf);
1235 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001236 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001237 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001238 buf = buf->next;
1239 retransmits--;
1240 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001242
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1244}
1245
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001246/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 * link_insert_deferred_queue - insert deferred messages back into receive chain
1248 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001249static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 struct sk_buff *buf)
1251{
1252 u32 seq_no;
1253
1254 if (l_ptr->oldest_deferred_in == NULL)
1255 return buf;
1256
Allan Stephensf9057302011-10-24 16:03:12 -04001257 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 if (seq_no == mod(l_ptr->next_in_no)) {
1259 l_ptr->newest_deferred_in->next = buf;
1260 buf = l_ptr->oldest_deferred_in;
1261 l_ptr->oldest_deferred_in = NULL;
1262 l_ptr->deferred_inqueue_sz = 0;
1263 }
1264 return buf;
1265}
1266
Allan Stephens85035562008-04-15 19:04:54 -07001267/**
1268 * link_recv_buf_validate - validate basic format of received message
1269 *
1270 * This routine ensures a TIPC message has an acceptable header, and at least
1271 * as much data as the header indicates it should. The routine also ensures
1272 * that the entire message header is stored in the main fragment of the message
1273 * buffer, to simplify future access to message header fields.
1274 *
1275 * Note: Having extra info present in the message header or data areas is OK.
1276 * TIPC will ignore the excess, under the assumption that it is optional info
1277 * introduced by a later release of the protocol.
1278 */
Allan Stephens85035562008-04-15 19:04:54 -07001279static int link_recv_buf_validate(struct sk_buff *buf)
1280{
1281 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001282 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001283 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1284 };
1285
1286 struct tipc_msg *msg;
1287 u32 tipc_hdr[2];
1288 u32 size;
1289 u32 hdr_size;
1290 u32 min_hdr_size;
1291
Erik Hugne64380a02014-02-11 11:38:26 +01001292 /* If this packet comes from the defer queue, the skb has already
1293 * been validated
1294 */
1295 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1296 return 1;
1297
Allan Stephens85035562008-04-15 19:04:54 -07001298 if (unlikely(buf->len < MIN_H_SIZE))
1299 return 0;
1300
1301 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1302 if (msg == NULL)
1303 return 0;
1304
1305 if (unlikely(msg_version(msg) != TIPC_VERSION))
1306 return 0;
1307
1308 size = msg_size(msg);
1309 hdr_size = msg_hdr_sz(msg);
1310 min_hdr_size = msg_isdata(msg) ?
1311 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1312
1313 if (unlikely((hdr_size < min_hdr_size) ||
1314 (size < hdr_size) ||
1315 (buf->len < size) ||
1316 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1317 return 0;
1318
1319 return pskb_may_pull(buf, hdr_size);
1320}
1321
Allan Stephensb02b69c2010-08-17 11:00:07 +00001322/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001323 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001324 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001325 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001326 *
1327 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1328 * structure (i.e. cannot be NULL), but bearer can be inactive.
1329 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001330void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001333 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001334 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 struct sk_buff *crs;
1336 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001337 struct tipc_msg *msg;
1338 u32 seq_no;
1339 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 u32 released = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001343 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001344
1345 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001346 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001347 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348
Allan Stephensfe13dda2008-04-15 19:03:23 -07001349 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001350 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001351 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001352
Allan Stephens85035562008-04-15 19:04:54 -07001353 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001354 msg = buf_msg(buf);
1355
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001357 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001358 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001359 else
Ying Xue247f0f32014-02-18 16:06:46 +08001360 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 continue;
1362 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001363
Allan Stephensed33a9c2011-04-05 15:15:04 -04001364 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001365 if (unlikely(!msg_short(msg) &&
1366 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001367 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001368
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001369 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001370 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001372 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001373 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001374
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001375 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001377 if (unlikely(!l_ptr))
1378 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001379
Allan Stephensb4b56102011-05-27 11:00:51 -04001380 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001381 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001382 msg_user(msg) == LINK_PROTOCOL &&
1383 (msg_type(msg) == RESET_MSG ||
1384 msg_type(msg) == ACTIVATE_MSG) &&
1385 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001386 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001387
Ying Xue10f465c2014-05-05 08:56:11 +08001388 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001389 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001390
Allan Stephens85035562008-04-15 19:04:54 -07001391 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001392 seq_no = msg_seqno(msg);
1393 ackd = msg_ack(msg);
1394
1395 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001396 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001397 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398
1399 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001400 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001401 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 struct sk_buff *next = crs->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001403 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 crs = next;
1405 released++;
1406 }
1407 if (released) {
1408 l_ptr->first_out = crs;
1409 l_ptr->out_queue_size -= released;
1410 }
Allan Stephens85035562008-04-15 19:04:54 -07001411
1412 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001414 tipc_link_push_queue(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001415
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001417 tipc_link_wakeup_ports(l_ptr, 0);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001418
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1420 l_ptr->stats.sent_acks++;
Ying Xue247f0f32014-02-18 16:06:46 +08001421 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 }
1423
Jon Paul Maloya5377832014-02-13 17:29:15 -05001424 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001425 if (unlikely(!link_working_working(l_ptr))) {
1426 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001427 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001428 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001429 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 continue;
1431 }
Ying Xue3af390e2013-10-30 11:26:57 +08001432
1433 /* Traffic message. Conditionally activate link */
1434 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1435
1436 if (link_working_working(l_ptr)) {
1437 /* Re-insert buffer in front of queue */
1438 buf->next = head;
1439 head = buf;
1440 tipc_node_unlock(n_ptr);
1441 continue;
1442 }
1443 goto unlock_discard;
1444 }
1445
1446 /* Link is now in state WORKING_WORKING */
1447 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 link_handle_out_of_seq_msg(l_ptr, buf);
1449 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001450 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 continue;
1452 }
Ying Xue3af390e2013-10-30 11:26:57 +08001453 l_ptr->next_in_no++;
1454 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001456
1457 /* Deliver packet/message to correct user: */
1458 if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL)) {
1459 if (!tipc_link_tunnel_rcv(n_ptr, &buf)) {
1460 tipc_node_unlock(n_ptr);
1461 continue;
1462 }
1463 msg = buf_msg(buf);
1464 } else if (msg_user(msg) == MSG_FRAGMENTER) {
Jon Paul Maloya5377832014-02-13 17:29:15 -05001465 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001466 if (tipc_buf_append(&l_ptr->reasm_buf, &buf)) {
Jon Paul Maloya5377832014-02-13 17:29:15 -05001467 l_ptr->stats.recv_fragmented++;
1468 msg = buf_msg(buf);
1469 } else {
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001470 if (!l_ptr->reasm_buf)
Jon Paul Maloya5377832014-02-13 17:29:15 -05001471 tipc_link_reset(l_ptr);
1472 tipc_node_unlock(n_ptr);
1473 continue;
1474 }
1475 }
1476
1477 switch (msg_user(msg)) {
1478 case TIPC_LOW_IMPORTANCE:
1479 case TIPC_MEDIUM_IMPORTANCE:
1480 case TIPC_HIGH_IMPORTANCE:
1481 case TIPC_CRITICAL_IMPORTANCE:
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001482 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001483 tipc_node_unlock(n_ptr);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001484 tipc_sk_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001486 case MSG_BUNDLER:
1487 l_ptr->stats.recv_bundles++;
1488 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1489 tipc_node_unlock(n_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +08001490 tipc_link_bundle_rcv(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001491 continue;
1492 case NAME_DISTRIBUTOR:
1493 n_ptr->bclink.recv_permitted = true;
1494 tipc_node_unlock(n_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +08001495 tipc_named_rcv(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001496 continue;
Jon Paul Maloya5377832014-02-13 17:29:15 -05001497 case BCAST_PROTOCOL:
Ying Xue247f0f32014-02-18 16:06:46 +08001498 tipc_link_sync_rcv(n_ptr, buf);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001499 break;
Ying Xue3af390e2013-10-30 11:26:57 +08001500 default:
1501 kfree_skb(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001502 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 }
Per Liden4323add2006-01-18 00:38:21 +01001504 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001505 continue;
1506unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001507 tipc_node_unlock(n_ptr);
1508discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001509 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511}
1512
Ben Hutchings2c530402012-07-10 10:55:09 +00001513/**
Allan Stephens8809b252011-10-25 10:44:35 -04001514 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1515 *
1516 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517 */
Allan Stephens8809b252011-10-25 10:44:35 -04001518u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001519 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520{
Allan Stephens8809b252011-10-25 10:44:35 -04001521 struct sk_buff *queue_buf;
1522 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001523 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524
1525 buf->next = NULL;
1526
1527 /* Empty queue ? */
1528 if (*head == NULL) {
1529 *head = *tail = buf;
1530 return 1;
1531 }
1532
1533 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001534 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 (*tail)->next = buf;
1536 *tail = buf;
1537 return 1;
1538 }
1539
Allan Stephens8809b252011-10-25 10:44:35 -04001540 /* Locate insertion point in queue, then insert; discard if duplicate */
1541 prev = head;
1542 queue_buf = *head;
1543 for (;;) {
1544 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545
Allan Stephens8809b252011-10-25 10:44:35 -04001546 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001547 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001548 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549 }
Allan Stephens8809b252011-10-25 10:44:35 -04001550
1551 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553
Allan Stephens8809b252011-10-25 10:44:35 -04001554 prev = &queue_buf->next;
1555 queue_buf = queue_buf->next;
1556 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557
Allan Stephens8809b252011-10-25 10:44:35 -04001558 buf->next = queue_buf;
1559 *prev = buf;
1560 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561}
1562
Allan Stephens8809b252011-10-25 10:44:35 -04001563/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001564 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1565 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001566static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 struct sk_buff *buf)
1568{
Allan Stephensf9057302011-10-24 16:03:12 -04001569 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570
1571 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001572 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573 return;
1574 }
1575
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 l_ptr->checkpoint--;
1578
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001579 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 * Discard packet if a duplicate; otherwise add it to deferred queue
1581 * and notify peer of gap as per protocol specification
1582 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 if (less(seq_no, mod(l_ptr->next_in_no))) {
1584 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001585 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 return;
1587 }
1588
Per Liden4323add2006-01-18 00:38:21 +01001589 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1590 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591 l_ptr->deferred_inqueue_sz++;
1592 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001593 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001595 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596 } else
1597 l_ptr->stats.duplicates++;
1598}
1599
1600/*
1601 * Send protocol message to the other endpoint.
1602 */
Ying Xue247f0f32014-02-18 16:06:46 +08001603void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1604 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001606 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001608 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001609 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610
Allan Stephens92d2c902011-10-25 11:20:26 -04001611 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001612 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001613 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001614 l_ptr->proto_msg_queue = NULL;
1615 }
1616
Ying Xue77a7e072013-12-10 20:45:44 -08001617 /* Don't send protocol message during link changeover */
1618 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001620
1621 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001622 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001623 return;
1624
Allan Stephens92d2c902011-10-25 11:20:26 -04001625 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001626 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001627 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001628 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001629 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630
1631 if (msg_typ == STATE_MSG) {
1632 u32 next_sent = mod(l_ptr->next_out_no);
1633
Per Liden4323add2006-01-18 00:38:21 +01001634 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 return;
1636 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001637 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 msg_set_next_sent(msg, next_sent);
1639 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001640 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641 gap = mod(rec - mod(l_ptr->next_in_no));
1642 }
1643 msg_set_seq_gap(msg, gap);
1644 if (gap)
1645 l_ptr->stats.sent_nacks++;
1646 msg_set_link_tolerance(msg, tolerance);
1647 msg_set_linkprio(msg, priority);
1648 msg_set_max_pkt(msg, ack_mtu);
1649 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1650 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001651 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 u32 mtu = l_ptr->max_pkt;
1653
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 link_working_working(l_ptr) &&
1656 l_ptr->fsm_msg_cnt) {
1657 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001658 if (l_ptr->max_pkt_probes == 10) {
1659 l_ptr->max_pkt_target = (msg_size - 4);
1660 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001662 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001664 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665
1666 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001667 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 l_ptr->stats.sent_states++;
1669 } else { /* RESET_MSG or ACTIVATE_MSG */
1670 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1671 msg_set_seq_gap(msg, 0);
1672 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001673 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 msg_set_link_tolerance(msg, l_ptr->tolerance);
1675 msg_set_linkprio(msg, l_ptr->priority);
1676 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1677 }
1678
Allan Stephens75f0aa42011-02-28 15:30:20 -05001679 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1680 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001682 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683
1684 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1685
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001686 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 if (!buf)
1688 return;
1689
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001690 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001691 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692
Ying Xue7a2f7d12014-04-21 10:55:46 +08001693 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001694 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001695 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696}
1697
1698/*
1699 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001700 * Note that network plane id propagates through the network, and may
1701 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 */
Ying Xue247f0f32014-02-18 16:06:46 +08001703static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704{
1705 u32 rec_gap = 0;
1706 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001707 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 u32 msg_tol;
1709 struct tipc_msg *msg = buf_msg(buf);
1710
Ying Xue77a7e072013-12-10 20:45:44 -08001711 /* Discard protocol message during link changeover */
1712 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 goto exit;
1714
Ying Xue7a2f7d12014-04-21 10:55:46 +08001715 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001717 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001720
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001722 if (!link_working_unknown(l_ptr) &&
1723 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001724 if (less_eq(msg_session(msg), l_ptr->peer_session))
1725 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001727
1728 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1729 link_working_unknown(l_ptr))) {
1730 /*
1731 * peer has lost contact -- don't allow peer's links
1732 * to reactivate before we recognize loss & clean up
1733 */
Ying Xueca9cf062014-05-08 08:54:40 +08001734 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001735 }
1736
Allan Stephens47361c82011-10-26 10:55:16 -04001737 link_state_event(l_ptr, RESET_MSG);
1738
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 /* fall thru' */
1740 case ACTIVATE_MSG:
1741 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1743
Allan Stephens2db99832010-12-31 18:59:33 +00001744 msg_tol = msg_link_tolerance(msg);
1745 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 link_set_supervision_props(l_ptr, msg_tol);
1747
1748 if (msg_linkprio(msg) > l_ptr->priority)
1749 l_ptr->priority = msg_linkprio(msg);
1750
1751 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001752 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753 if (max_pkt_info < l_ptr->max_pkt_target)
1754 l_ptr->max_pkt_target = max_pkt_info;
1755 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1756 l_ptr->max_pkt = l_ptr->max_pkt_target;
1757 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001758 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760
Allan Stephens4d753132011-10-25 12:19:05 -04001761 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001762 if (!tipc_node_is_up(l_ptr->owner)) {
1763 l_ptr->owner->bclink.last_sent =
1764 l_ptr->owner->bclink.last_in =
1765 msg_last_bcast(msg);
1766 l_ptr->owner->bclink.oos_state = 0;
1767 }
Allan Stephens4d753132011-10-25 12:19:05 -04001768
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769 l_ptr->peer_session = msg_session(msg);
1770 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001771
1772 if (msg_type(msg) == ACTIVATE_MSG)
1773 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 break;
1775 case STATE_MSG:
1776
Allan Stephens2db99832010-12-31 18:59:33 +00001777 msg_tol = msg_link_tolerance(msg);
1778 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001780
1781 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001783 pr_warn("%s<%s>, priority change %u->%u\n",
1784 link_rst_msg, l_ptr->name, l_ptr->priority,
1785 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001787 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 break;
1789 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001790
1791 /* Record reception; force mismatch at next timeout: */
1792 l_ptr->checkpoint--;
1793
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1795 l_ptr->stats.recv_states++;
1796 if (link_reset_unknown(l_ptr))
1797 break;
1798
1799 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001800 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 mod(l_ptr->next_in_no));
1802 }
1803
1804 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001805 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001806 l_ptr->max_pkt = max_pkt_ack;
1807 l_ptr->max_pkt_probes = 0;
1808 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809
1810 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001811 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001813 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001814 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001815 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816
1817 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001818 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001819 tipc_bclink_update_link_state(l_ptr->owner,
1820 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821
1822 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001823 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1824 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 }
1826 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001828 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1829 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 }
1831 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 }
1833exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001834 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835}
1836
1837
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001838/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1839 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001841static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1842 struct tipc_msg *tunnel_hdr,
1843 struct tipc_msg *msg,
1844 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001846 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 struct sk_buff *buf;
1848 u32 length = msg_size(msg);
1849
1850 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001851 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001852 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001854 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001856 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001857 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001858 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001860 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001861 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1862 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Ying Xue247f0f32014-02-18 16:06:46 +08001863 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864}
1865
1866
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001867/* tipc_link_failover_send_queue(): A link has gone down, but a second
1868 * link is still active. We can do failover. Tunnel the failing link's
1869 * whole send queue via the remaining link. This way, we don't lose
1870 * any packets, and sequence order is preserved for subsequent traffic
1871 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001873void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874{
1875 u32 msgcount = l_ptr->out_queue_size;
1876 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001877 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001879 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880
1881 if (!tunnel)
1882 return;
1883
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001884 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001885 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1887 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001888
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 if (!l_ptr->first_out) {
1890 struct sk_buff *buf;
1891
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001892 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001894 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xue247f0f32014-02-18 16:06:46 +08001896 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001898 pr_warn("%sunable to send changeover msg\n",
1899 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 }
1901 return;
1902 }
Allan Stephensf1310722006-06-25 23:51:37 -07001903
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001904 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001905 l_ptr->owner->active_links[1]);
1906
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 while (crs) {
1908 struct tipc_msg *msg = buf_msg(crs);
1909
1910 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001912 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913
Florian Westphald788d802007-08-02 19:28:06 -07001914 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001916 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001917 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1918 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919 pos += align(msg_size(m));
1920 m = (struct tipc_msg *)pos;
1921 }
1922 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001923 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1924 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 }
1926 crs = crs->next;
1927 }
1928}
1929
Ying Xue247f0f32014-02-18 16:06:46 +08001930/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001931 * duplicate of the first link's send queue via the new link. This way, we
1932 * are guaranteed that currently queued packets from a socket are delivered
1933 * before future traffic from the same socket, even if this is using the
1934 * new link. The last arriving copy of each duplicate packet is dropped at
1935 * the receiving end by the regular protocol check, so packet cardinality
1936 * and sequence order is preserved per sender/receiver socket pair.
1937 * Owner node is locked.
1938 */
Ying Xue247f0f32014-02-18 16:06:46 +08001939void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001940 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941{
1942 struct sk_buff *iter;
1943 struct tipc_msg tunnel_hdr;
1944
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001945 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001946 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1948 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1949 iter = l_ptr->first_out;
1950 while (iter) {
1951 struct sk_buff *outbuf;
1952 struct tipc_msg *msg = buf_msg(iter);
1953 u32 length = msg_size(msg);
1954
1955 if (msg_user(msg) == MSG_BUNDLER)
1956 msg_set_type(msg, CLOSED_MSG);
1957 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001958 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001960 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001962 pr_warn("%sunable to send duplicate msg\n",
1963 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964 return;
1965 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001966 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1967 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1968 length);
Ying Xue247f0f32014-02-18 16:06:46 +08001969 __tipc_link_xmit(tunnel, outbuf);
Per Liden4323add2006-01-18 00:38:21 +01001970 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 return;
1972 iter = iter->next;
1973 }
1974}
1975
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976/**
1977 * buf_extract - extracts embedded TIPC message from another message
1978 * @skb: encapsulating message buffer
1979 * @from_pos: offset to extract from
1980 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001981 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982 * encapsulating message itself is left unchanged.
1983 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1985{
1986 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1987 u32 size = msg_size(msg);
1988 struct sk_buff *eb;
1989
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001990 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001991 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001992 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001993 return eb;
1994}
1995
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001996
1997
1998/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1999 * Owner node is locked.
2000 */
2001static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
2002 struct sk_buff *t_buf)
2003{
2004 struct sk_buff *buf;
2005
2006 if (!tipc_link_is_up(l_ptr))
2007 return;
2008
2009 buf = buf_extract(t_buf, INT_H_SIZE);
2010 if (buf == NULL) {
2011 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
2012 return;
2013 }
2014
2015 /* Add buffer to deferred queue, if applicable: */
2016 link_handle_out_of_seq_msg(l_ptr, buf);
2017}
2018
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002019/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
2020 * Owner node is locked.
2021 */
2022static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
2023 struct sk_buff *t_buf)
2024{
2025 struct tipc_msg *t_msg = buf_msg(t_buf);
2026 struct sk_buff *buf = NULL;
2027 struct tipc_msg *msg;
2028
2029 if (tipc_link_is_up(l_ptr))
2030 tipc_link_reset(l_ptr);
2031
2032 /* First failover packet? */
2033 if (l_ptr->exp_msg_count == START_CHANGEOVER)
2034 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
2035
2036 /* Should there be an inner packet? */
2037 if (l_ptr->exp_msg_count) {
2038 l_ptr->exp_msg_count--;
2039 buf = buf_extract(t_buf, INT_H_SIZE);
2040 if (buf == NULL) {
2041 pr_warn("%sno inner failover pkt\n", link_co_err);
2042 goto exit;
2043 }
2044 msg = buf_msg(buf);
2045
2046 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
2047 kfree_skb(buf);
2048 buf = NULL;
2049 goto exit;
2050 }
2051 if (msg_user(msg) == MSG_FRAGMENTER) {
2052 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04002053 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002054 }
2055 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002056exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05002057 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
2058 tipc_node_detach_link(l_ptr->owner, l_ptr);
2059 kfree(l_ptr);
2060 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002061 return buf;
2062}
2063
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05002064/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002065 * via other link as result of a failover (ORIGINAL_MSG) or
2066 * a new active link (DUPLICATE_MSG). Failover packets are
2067 * returned to the active link for delivery upwards.
2068 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05002070static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002071 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002073 struct sk_buff *t_buf = *buf;
2074 struct tipc_link *l_ptr;
2075 struct tipc_msg *t_msg = buf_msg(t_buf);
2076 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05002078 *buf = NULL;
2079
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002080 if (bearer_id >= MAX_BEARERS)
2081 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05002082
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002083 l_ptr = n_ptr->links[bearer_id];
2084 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002085 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002087 if (msg_type(t_msg) == DUPLICATE_MSG)
2088 tipc_link_dup_rcv(l_ptr, t_buf);
2089 else if (msg_type(t_msg) == ORIGINAL_MSG)
2090 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05002091 else
2092 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002093exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002094 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05002095 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096}
2097
2098/*
2099 * Bundler functionality:
2100 */
Ying Xue247f0f32014-02-18 16:06:46 +08002101void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002102{
2103 u32 msgcount = msg_msgcnt(buf_msg(buf));
2104 u32 pos = INT_H_SIZE;
2105 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05002106 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 while (msgcount--) {
2109 obuf = buf_extract(buf, pos);
2110 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002111 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07002112 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002113 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05002114 omsg = buf_msg(obuf);
2115 pos += align(msg_size(omsg));
2116 if (msg_isdata(omsg) || (msg_user(omsg) == CONN_MANAGER)) {
2117 tipc_sk_rcv(obuf);
2118 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
2119 tipc_named_rcv(obuf);
2120 } else {
2121 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
2122 kfree_skb(obuf);
2123 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002125 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126}
2127
2128/*
2129 * Fragmentation/defragmentation:
2130 */
2131
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002132/*
Ying Xue247f0f32014-02-18 16:06:46 +08002133 * tipc_link_frag_xmit: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002134 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135 * Returns user data length.
2136 */
Ying Xue247f0f32014-02-18 16:06:46 +08002137static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138{
Allan Stephens77561552011-04-17 13:06:23 -04002139 struct sk_buff *buf_chain = NULL;
2140 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141 struct tipc_msg *inmsg = buf_msg(buf);
2142 struct tipc_msg fragm_hdr;
2143 u32 insize = msg_size(inmsg);
2144 u32 dsz = msg_data_sz(inmsg);
2145 unchar *crs = buf->data;
2146 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002147 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148 u32 fragm_sz = pack_sz - INT_H_SIZE;
Allan Stephens77561552011-04-17 13:06:23 -04002149 u32 fragm_no = 0;
Allan Stephens9c396a72008-06-04 17:36:58 -07002150 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002151
2152 if (msg_short(inmsg))
2153 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002154 else
2155 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002156
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157 /* Prepare reusable fragment header: */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002158 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002159 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002160
2161 /* Chop up message: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002162 while (rest > 0) {
2163 struct sk_buff *fragm;
2164
2165 if (rest <= fragm_sz) {
2166 fragm_sz = rest;
2167 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2168 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002169 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002170 if (fragm == NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002171 kfree_skb(buf);
Ying Xued77b3832013-12-10 20:45:38 -08002172 kfree_skb_list(buf_chain);
Allan Stephens77561552011-04-17 13:06:23 -04002173 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002174 }
2175 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Allan Stephens77561552011-04-17 13:06:23 -04002176 fragm_no++;
2177 msg_set_fragm_no(&fragm_hdr, fragm_no);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002178 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2179 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2180 fragm_sz);
Allan Stephens77561552011-04-17 13:06:23 -04002181 buf_chain_tail->next = fragm;
2182 buf_chain_tail = fragm;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002183
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 rest -= fragm_sz;
2185 crs += fragm_sz;
2186 msg_set_type(&fragm_hdr, FRAGMENT);
2187 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002188 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002189
2190 /* Append chain of fragments to send queue & send them */
Allan Stephens77561552011-04-17 13:06:23 -04002191 l_ptr->long_msg_seq_no++;
2192 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2193 l_ptr->stats.sent_fragments += fragm_no;
2194 l_ptr->stats.sent_fragmented++;
2195 tipc_link_push_queue(l_ptr);
2196
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 return dsz;
2198}
2199
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002200static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002201{
Allan Stephens5413b4c2011-01-18 13:24:55 -05002202 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2203 return;
2204
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205 l_ptr->tolerance = tolerance;
2206 l_ptr->continuity_interval =
2207 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2208 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2209}
2210
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002211void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002212{
2213 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002214 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2215 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2216 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2217 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002218 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002219 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2220 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2221 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2222 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002223 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002224 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2225 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2226 /* FRAGMENT and LAST_FRAGMENT packets */
2227 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2228}
2229
Jon Paul Maloye099e862014-02-13 17:29:18 -05002230/* tipc_link_find_owner - locate owner node of link by link's name
2231 * @name: pointer to link name string
2232 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002233 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05002234 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002235 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05002236static struct tipc_node *tipc_link_find_owner(const char *link_name,
2237 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002238{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002239 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002240 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002241 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002242 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002243
Jon Paul Maloye099e862014-02-13 17:29:18 -05002244 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08002245 rcu_read_lock();
2246 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002247 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02002248 for (i = 0; i < MAX_BEARERS; i++) {
2249 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05002250 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
2251 *bearer_id = i;
2252 found_node = n_ptr;
2253 break;
2254 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02002255 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002256 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002257 if (found_node)
2258 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002259 }
Ying Xue6c7a7622014-03-27 12:54:37 +08002260 rcu_read_unlock();
2261
Jon Paul Maloye099e862014-02-13 17:29:18 -05002262 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002263}
2264
Allan Stephens5c216e12011-10-18 11:34:29 -04002265/**
2266 * link_value_is_valid -- validate proposed link tolerance/priority/window
2267 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002268 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2269 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002270 *
2271 * Returns 1 if value is within range, 0 if not.
2272 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002273static int link_value_is_valid(u16 cmd, u32 new_value)
2274{
2275 switch (cmd) {
2276 case TIPC_CMD_SET_LINK_TOL:
2277 return (new_value >= TIPC_MIN_LINK_TOL) &&
2278 (new_value <= TIPC_MAX_LINK_TOL);
2279 case TIPC_CMD_SET_LINK_PRI:
2280 return (new_value <= TIPC_MAX_LINK_PRI);
2281 case TIPC_CMD_SET_LINK_WINDOW:
2282 return (new_value >= TIPC_MIN_LINK_WIN) &&
2283 (new_value <= TIPC_MAX_LINK_WIN);
2284 }
2285 return 0;
2286}
2287
Allan Stephens5c216e12011-10-18 11:34:29 -04002288/**
2289 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002290 * @name: ptr to link, bearer, or media name
2291 * @new_value: new value of link, bearer, or media setting
2292 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002293 *
Ying Xue7216cd92014-04-21 10:55:48 +08002294 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002295 *
2296 * Returns 0 if value updated and negative value on error.
2297 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002298static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2299{
2300 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002301 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002302 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002303 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002304 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002305 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002306
Jon Paul Maloye099e862014-02-13 17:29:18 -05002307 node = tipc_link_find_owner(name, &bearer_id);
2308 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002309 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002310 l_ptr = node->links[bearer_id];
2311
2312 if (l_ptr) {
2313 switch (cmd) {
2314 case TIPC_CMD_SET_LINK_TOL:
2315 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002316 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2317 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002318 break;
2319 case TIPC_CMD_SET_LINK_PRI:
2320 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002321 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2322 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002323 break;
2324 case TIPC_CMD_SET_LINK_WINDOW:
2325 tipc_link_set_queue_limits(l_ptr, new_value);
2326 break;
2327 default:
2328 res = -EINVAL;
2329 break;
2330 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002331 }
2332 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002333 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002334 }
2335
2336 b_ptr = tipc_bearer_find(name);
2337 if (b_ptr) {
2338 switch (cmd) {
2339 case TIPC_CMD_SET_LINK_TOL:
2340 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002341 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002342 case TIPC_CMD_SET_LINK_PRI:
2343 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002344 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002345 case TIPC_CMD_SET_LINK_WINDOW:
2346 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002347 break;
2348 default:
2349 res = -EINVAL;
2350 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002351 }
Ying Xue636c0372013-10-18 07:23:20 +02002352 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002353 }
2354
2355 m_ptr = tipc_media_find(name);
2356 if (!m_ptr)
2357 return -ENODEV;
2358 switch (cmd) {
2359 case TIPC_CMD_SET_LINK_TOL:
2360 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002361 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002362 case TIPC_CMD_SET_LINK_PRI:
2363 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002364 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002365 case TIPC_CMD_SET_LINK_WINDOW:
2366 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002367 break;
2368 default:
2369 res = -EINVAL;
2370 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002371 }
Ying Xue636c0372013-10-18 07:23:20 +02002372 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002373}
2374
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002375struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002376 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002377{
2378 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002379 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002380 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381
2382 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002383 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002384
2385 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2386 new_value = ntohl(args->value);
2387
Allan Stephens5c216e12011-10-18 11:34:29 -04002388 if (!link_value_is_valid(cmd, new_value))
2389 return tipc_cfg_reply_error_string(
2390 "cannot change, value invalid");
2391
Per Liden4323add2006-01-18 00:38:21 +01002392 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002393 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002394 (tipc_bclink_set_queue_limits(new_value) == 0))
2395 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002396 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002397 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398 }
2399
Allan Stephens5c216e12011-10-18 11:34:29 -04002400 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002401 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002402 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002403
Per Liden4323add2006-01-18 00:38:21 +01002404 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405}
2406
2407/**
2408 * link_reset_statistics - reset link statistics
2409 * @l_ptr: pointer to link
2410 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002411static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002412{
2413 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2414 l_ptr->stats.sent_info = l_ptr->next_out_no;
2415 l_ptr->stats.recv_info = l_ptr->next_in_no;
2416}
2417
Per Liden4323add2006-01-18 00:38:21 +01002418struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002419{
2420 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002421 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002422 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002423 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002424
2425 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002426 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427
2428 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002429 if (!strcmp(link_name, tipc_bclink_name)) {
2430 if (tipc_bclink_reset_stats())
2431 return tipc_cfg_reply_error_string("link not found");
2432 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002433 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002434 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002435 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002436 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002437
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002438 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002439 l_ptr = node->links[bearer_id];
2440 if (!l_ptr) {
2441 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002442 return tipc_cfg_reply_error_string("link not found");
2443 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002444 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002445 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002446 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002447}
2448
2449/**
2450 * percent - convert count to a percentage of total (rounding up or down)
2451 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002452static u32 percent(u32 count, u32 total)
2453{
2454 return (count * 100 + (total / 2)) / total;
2455}
2456
2457/**
Per Liden4323add2006-01-18 00:38:21 +01002458 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002459 * @name: link name
2460 * @buf: print buffer area
2461 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002462 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002463 * Returns length of print buffer data string (or 0 if error)
2464 */
Per Liden4323add2006-01-18 00:38:21 +01002465static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002467 struct tipc_link *l;
2468 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002469 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470 char *status;
2471 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002472 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002473 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002474
Per Liden4323add2006-01-18 00:38:21 +01002475 if (!strcmp(name, tipc_bclink_name))
2476 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477
Jon Paul Maloye099e862014-02-13 17:29:18 -05002478 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002479 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002481
Per Liden4323add2006-01-18 00:38:21 +01002482 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002483
2484 l = node->links[bearer_id];
2485 if (!l) {
2486 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002487 return 0;
2488 }
2489
Erik Hugnedc1aed32012-06-29 00:50:23 -04002490 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002491
Erik Hugnedc1aed32012-06-29 00:50:23 -04002492 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002493 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002494 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495 status = "STANDBY";
2496 else
2497 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002498
2499 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2500 " %s MTU:%u Priority:%u Tolerance:%u ms"
2501 " Window:%u packets\n",
2502 l->name, status, l->max_pkt, l->priority,
2503 l->tolerance, l->queue_limit[0]);
2504
2505 ret += tipc_snprintf(buf + ret, buf_size - ret,
2506 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2507 l->next_in_no - s->recv_info, s->recv_fragments,
2508 s->recv_fragmented, s->recv_bundles,
2509 s->recv_bundled);
2510
2511 ret += tipc_snprintf(buf + ret, buf_size - ret,
2512 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2513 l->next_out_no - s->sent_info, s->sent_fragments,
2514 s->sent_fragmented, s->sent_bundles,
2515 s->sent_bundled);
2516
2517 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002518 if (!profile_total)
2519 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002520
2521 ret += tipc_snprintf(buf + ret, buf_size - ret,
2522 " TX profile sample:%u packets average:%u octets\n"
2523 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2524 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2525 s->msg_length_counts,
2526 s->msg_lengths_total / profile_total,
2527 percent(s->msg_length_profile[0], profile_total),
2528 percent(s->msg_length_profile[1], profile_total),
2529 percent(s->msg_length_profile[2], profile_total),
2530 percent(s->msg_length_profile[3], profile_total),
2531 percent(s->msg_length_profile[4], profile_total),
2532 percent(s->msg_length_profile[5], profile_total),
2533 percent(s->msg_length_profile[6], profile_total));
2534
2535 ret += tipc_snprintf(buf + ret, buf_size - ret,
2536 " RX states:%u probes:%u naks:%u defs:%u"
2537 " dups:%u\n", s->recv_states, s->recv_probes,
2538 s->recv_nacks, s->deferred_recv, s->duplicates);
2539
2540 ret += tipc_snprintf(buf + ret, buf_size - ret,
2541 " TX states:%u probes:%u naks:%u acks:%u"
2542 " dups:%u\n", s->sent_states, s->sent_probes,
2543 s->sent_nacks, s->sent_acks, s->retransmitted);
2544
2545 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002546 " Congestion link:%u Send queue"
2547 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002548 s->max_queue_sz, s->queue_sz_counts ?
2549 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550
Per Liden4323add2006-01-18 00:38:21 +01002551 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002552 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002553}
2554
Per Liden4323add2006-01-18 00:38:21 +01002555struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002556{
2557 struct sk_buff *buf;
2558 struct tlv_desc *rep_tlv;
2559 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002560 int pb_len;
2561 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562
2563 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002564 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002565
Erik Hugnedc1aed32012-06-29 00:50:23 -04002566 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567 if (!buf)
2568 return NULL;
2569
2570 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002571 pb = TLV_DATA(rep_tlv);
2572 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002573 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002574 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002575 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002576 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002577 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002579 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580 skb_put(buf, TLV_SPACE(str_len));
2581 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2582
2583 return buf;
2584}
2585
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586/**
Per Liden4323add2006-01-18 00:38:21 +01002587 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002588 * @dest: network address of destination node
2589 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002590 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002591 * If no active link can be found, uses default maximum packet size.
2592 */
Per Liden4323add2006-01-18 00:38:21 +01002593u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594{
David S. Miller6c000552008-09-02 23:38:32 -07002595 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002596 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002597 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002598
Per Lidenb97bf3f2006-01-02 19:04:38 +01002599 if (dest == tipc_own_addr)
2600 return MAX_MSG_SIZE;
2601
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002602 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002603 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002604 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 l_ptr = n_ptr->active_links[selector & 1];
2606 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002607 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002608 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610 return res;
2611}
2612
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002613static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002614{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002615 struct tipc_bearer *b_ptr;
2616
2617 rcu_read_lock();
2618 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2619 if (b_ptr)
2620 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2621 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002622
Per Lidenb97bf3f2006-01-02 19:04:38 +01002623 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002624 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002625 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002626 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002627 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002628 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002629 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002630 pr_cont(":WW\n");
2631 else
2632 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002633}