blob: a081e7d08d22aefec08f2e661cbdee29c03e1502 [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 Stephensf882cb72011-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;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500335 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336
Per Liden4323add2006-01-18 00:38:21 +0100337 spin_lock_bh(&tipc_port_list_lock);
338 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 if (p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 if (!list_empty(&p_ptr->wait_list))
341 goto exit;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500342 tsk = tipc_port_to_sock(p_ptr);
343 tsk->link_cong = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000344 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
346 l_ptr->stats.link_congs++;
347exit:
Per Liden4323add2006-01-18 00:38:21 +0100348 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 }
Per Liden4323add2006-01-18 00:38:21 +0100350 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 return -ELINKCONG;
352}
353
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500354void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500356 struct tipc_port *p_ptr;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500357 struct tipc_sock *tsk;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500358 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
360
361 if (all)
362 win = 100000;
363 if (win <= 0)
364 return;
Per Liden4323add2006-01-18 00:38:21 +0100365 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 return;
367 if (link_congested(l_ptr))
368 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900369 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 wait_list) {
371 if (win <= 0)
372 break;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500373 tsk = tipc_port_to_sock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500375 spin_lock_bh(p_ptr->lock);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500376 tsk->link_cong = 0;
377 tipc_sock_wakeup(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500379 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 }
381
382exit:
Per Liden4323add2006-01-18 00:38:21 +0100383 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384}
385
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900386/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 * link_release_outqueue - purge link's outbound message queue
388 * @l_ptr: pointer to link
389 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500390static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391{
Ying Xued77b3832013-12-10 20:45:38 -0800392 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 l_ptr->first_out = NULL;
394 l_ptr->out_queue_size = 0;
395}
396
397/**
Per Liden4323add2006-01-18 00:38:21 +0100398 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 * @l_ptr: pointer to link
400 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500401void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400403 kfree_skb(l_ptr->reasm_buf);
404 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405}
406
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900407/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500408 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 * @l_ptr: pointer to link
410 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500411void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412{
Ying Xued77b3832013-12-10 20:45:38 -0800413 kfree_skb_list(l_ptr->oldest_deferred_in);
414 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100415 tipc_link_reset_fragments(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400416 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 l_ptr->proto_msg_queue = NULL;
418}
419
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500420void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 u32 prev_state = l_ptr->state;
423 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700424 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425
Allan Stephensa686e682008-06-04 17:29:39 -0700426 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427
Allan Stephensa686e682008-06-04 17:29:39 -0700428 /* Link is down, accept any session */
429 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900431 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900433
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435
436 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
437 return;
438
Per Liden4323add2006-01-18 00:38:21 +0100439 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800440 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000441
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500442 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 l_ptr->reset_checkpoint = checkpoint;
444 l_ptr->exp_msg_count = START_CHANGEOVER;
445 }
446
447 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400449 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 l_ptr->proto_msg_queue = NULL;
Ying Xued77b3832013-12-10 20:45:38 -0800451 kfree_skb_list(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100453 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454
455 l_ptr->retransm_queue_head = 0;
456 l_ptr->retransm_queue_size = 0;
457 l_ptr->last_out = NULL;
458 l_ptr->first_out = NULL;
459 l_ptr->next_out = NULL;
460 l_ptr->unacked_window = 0;
461 l_ptr->checkpoint = 1;
462 l_ptr->next_out_no = 1;
463 l_ptr->deferred_inqueue_sz = 0;
464 l_ptr->oldest_deferred_in = NULL;
465 l_ptr->newest_deferred_in = NULL;
466 l_ptr->fsm_msg_cnt = 0;
467 l_ptr->stale_count = 0;
468 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469}
470
Ying Xuec61dd612014-02-13 17:29:09 -0500471void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500472{
473 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500474 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500475
Ying Xue6c7a7622014-03-27 12:54:37 +0800476 rcu_read_lock();
477 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800478 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500479 l_ptr = n_ptr->links[bearer_id];
480 if (l_ptr)
481 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800482 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500483 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800484 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500485}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500487static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488{
Allan Stephens5392d642006-06-25 23:52:50 -0700489 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100490 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800491 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492}
493
494/**
495 * link_state_event - link finite state machine
496 * @l_ptr: pointer to link
497 * @event: state machine event to process
498 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000499static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500501 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 u32 cont_intv = l_ptr->continuity_interval;
503
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500504 if (l_ptr->flags & LINK_STOPPED)
505 return;
506
Ying Xue135daee2014-02-13 17:29:08 -0500507 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 return; /* Not yet. */
509
Ying Xue77a7e072013-12-10 20:45:44 -0800510 /* Check whether changeover is going on */
511 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000512 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800514 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516
517 switch (l_ptr->state) {
518 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 switch (event) {
520 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 break;
523 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 if (l_ptr->next_in_no != l_ptr->checkpoint) {
525 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100526 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800527 tipc_link_proto_xmit(l_ptr, STATE_MSG,
528 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 l_ptr->fsm_msg_cnt++;
530 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800531 tipc_link_proto_xmit(l_ptr, STATE_MSG,
532 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 l_ptr->fsm_msg_cnt++;
534 }
535 link_set_timer(l_ptr, cont_intv);
536 break;
537 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 l_ptr->state = WORKING_UNKNOWN;
539 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800540 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 l_ptr->fsm_msg_cnt++;
542 link_set_timer(l_ptr, cont_intv / 4);
543 break;
544 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400545 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
546 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100547 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 l_ptr->state = RESET_RESET;
549 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800550 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
551 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 l_ptr->fsm_msg_cnt++;
553 link_set_timer(l_ptr, cont_intv);
554 break;
555 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400556 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 }
558 break;
559 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 switch (event) {
561 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 l_ptr->state = WORKING_WORKING;
564 l_ptr->fsm_msg_cnt = 0;
565 link_set_timer(l_ptr, cont_intv);
566 break;
567 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400568 pr_info("%s<%s>, requested by peer while probing\n",
569 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100570 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 l_ptr->state = RESET_RESET;
572 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800573 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
574 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 l_ptr->fsm_msg_cnt++;
576 link_set_timer(l_ptr, cont_intv);
577 break;
578 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 l_ptr->state = WORKING_WORKING;
581 l_ptr->fsm_msg_cnt = 0;
582 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100583 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800584 tipc_link_proto_xmit(l_ptr, STATE_MSG,
585 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 l_ptr->fsm_msg_cnt++;
587 }
588 link_set_timer(l_ptr, cont_intv);
589 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800590 tipc_link_proto_xmit(l_ptr, STATE_MSG,
591 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 l_ptr->fsm_msg_cnt++;
593 link_set_timer(l_ptr, cont_intv / 4);
594 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400595 pr_warn("%s<%s>, peer not responding\n",
596 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100597 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 l_ptr->state = RESET_UNKNOWN;
599 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800600 tipc_link_proto_xmit(l_ptr, RESET_MSG,
601 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 l_ptr->fsm_msg_cnt++;
603 link_set_timer(l_ptr, cont_intv);
604 }
605 break;
606 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400607 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 }
609 break;
610 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 switch (event) {
612 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 break;
614 case ACTIVATE_MSG:
615 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000616 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 l_ptr->state = WORKING_WORKING;
619 l_ptr->fsm_msg_cnt = 0;
620 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800621 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800623 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800624 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 link_set_timer(l_ptr, cont_intv);
626 break;
627 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 l_ptr->state = RESET_RESET;
629 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800630 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
631 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 l_ptr->fsm_msg_cnt++;
633 link_set_timer(l_ptr, cont_intv);
634 break;
635 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500636 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 /* fall through */
638 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800639 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 l_ptr->fsm_msg_cnt++;
641 link_set_timer(l_ptr, cont_intv);
642 break;
643 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400644 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 }
646 break;
647 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 switch (event) {
649 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 case ACTIVATE_MSG:
651 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000652 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 l_ptr->state = WORKING_WORKING;
655 l_ptr->fsm_msg_cnt = 0;
656 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800657 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800659 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800660 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 link_set_timer(l_ptr, cont_intv);
662 break;
663 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 break;
665 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800666 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
667 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 l_ptr->fsm_msg_cnt++;
669 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 break;
671 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400672 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 }
674 break;
675 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400676 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677 }
678}
679
680/*
681 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900682 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400684static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 struct sk_buff *buf)
686{
687 struct tipc_msg *bundler_msg = buf_msg(bundler);
688 struct tipc_msg *msg = buf_msg(buf);
689 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700690 u32 bundle_size = msg_size(bundler_msg);
691 u32 to_pos = align(bundle_size);
692 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693
694 if (msg_user(bundler_msg) != MSG_BUNDLER)
695 return 0;
696 if (msg_type(bundler_msg) != OPEN_MSG)
697 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700698 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000700 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700701 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702
Allan Stephense49060c2006-06-29 12:32:46 -0700703 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300704 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705 msg_set_size(bundler_msg, to_pos + size);
706 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400707 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 l_ptr->stats.sent_bundled++;
709 return 1;
710}
711
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500712static void link_add_to_outqueue(struct tipc_link *l_ptr,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800713 struct sk_buff *buf,
714 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715{
716 u32 ack = mod(l_ptr->next_in_no - 1);
717 u32 seqno = mod(l_ptr->next_out_no++);
718
719 msg_set_word(msg, 2, ((ack << 16) | seqno));
720 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
721 buf->next = NULL;
722 if (l_ptr->first_out) {
723 l_ptr->last_out->next = buf;
724 l_ptr->last_out = buf;
725 } else
726 l_ptr->first_out = l_ptr->last_out = buf;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500727
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 l_ptr->out_queue_size++;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500729 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
730 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731}
732
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500733static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
Allan Stephensdc63d912011-04-21 11:50:42 -0400734 struct sk_buff *buf_chain,
735 u32 long_msgno)
736{
737 struct sk_buff *buf;
738 struct tipc_msg *msg;
739
740 if (!l_ptr->next_out)
741 l_ptr->next_out = buf_chain;
742 while (buf_chain) {
743 buf = buf_chain;
744 buf_chain = buf_chain->next;
745
746 msg = buf_msg(buf);
747 msg_set_long_msgno(msg, long_msgno);
748 link_add_to_outqueue(l_ptr, buf, msg);
749 }
750}
751
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900752/*
Ying Xue247f0f32014-02-18 16:06:46 +0800753 * tipc_link_xmit() is the 'full path' for messages, called from
754 * inside TIPC when the 'fast path' in tipc_send_xmit
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 * has failed, and from link_send()
756 */
Ying Xue247f0f32014-02-18 16:06:46 +0800757int __tipc_link_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758{
759 struct tipc_msg *msg = buf_msg(buf);
760 u32 size = msg_size(msg);
761 u32 dsz = msg_data_sz(msg);
762 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000763 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000765 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 /* Match msg importance against queue limits: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 if (unlikely(queue_size >= queue_limit)) {
769 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
Allan Stephensbebc55a2011-04-19 10:17:58 -0400770 link_schedule_port(l_ptr, msg_origport(msg), size);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400771 kfree_skb(buf);
Allan Stephensbebc55a2011-04-19 10:17:58 -0400772 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 }
Allan Stephens5f6d9122011-11-04 13:24:29 -0400774 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 if (imp > CONN_MANAGER) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400776 pr_warn("%s<%s>, send queue full", link_rst_msg,
777 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100778 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 }
780 return dsz;
781 }
782
783 /* Fragmentation needed ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 if (size > max_packet)
Ying Xue247f0f32014-02-18 16:06:46 +0800785 return tipc_link_frag_xmit(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400787 /* Packet can be queued or sent. */
Erik Hugne512137e2013-12-06 10:08:00 -0500788 if (likely(!link_congested(l_ptr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 link_add_to_outqueue(l_ptr, buf, msg);
790
Ying Xue7a2f7d12014-04-21 10:55:46 +0800791 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800792 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 return dsz;
794 }
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400795 /* Congestion: can message be bundled ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
797 (msg_user(msg) != MSG_FRAGMENTER)) {
798
799 /* Try adding message to an existing bundle */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900800 if (l_ptr->next_out &&
Ying Xue3c294cb2012-11-15 11:34:45 +0800801 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 return dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803
804 /* Try creating a new bundle */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000806 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 struct tipc_msg bundler_hdr;
808
809 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000810 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700811 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300812 skb_copy_to_linear_data(bundler, &bundler_hdr,
813 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 skb_trim(bundler, INT_H_SIZE);
815 link_bundle_buf(l_ptr, bundler, buf);
816 buf = bundler;
817 msg = buf_msg(buf);
818 l_ptr->stats.sent_bundles++;
819 }
820 }
821 }
822 if (!l_ptr->next_out)
823 l_ptr->next_out = buf;
824 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 return dsz;
826}
827
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828/*
Ying Xue247f0f32014-02-18 16:06:46 +0800829 * tipc_link_xmit(): same as __tipc_link_xmit(), but the link to use
830 * has not been selected yet, and the the owner node is not locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 * Called by TIPC internal users, e.g. the name distributor
832 */
Ying Xue247f0f32014-02-18 16:06:46 +0800833int tipc_link_xmit(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500835 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700836 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 int res = -ELINKCONG;
838
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000839 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100841 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000843 if (l_ptr)
Ying Xue247f0f32014-02-18 16:06:46 +0800844 res = __tipc_link_xmit(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000845 else
Allan Stephens5f6d9122011-11-04 13:24:29 -0400846 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100847 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 } else {
Allan Stephens5f6d9122011-11-04 13:24:29 -0400849 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 return res;
852}
853
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500854/* tipc_link_cong: determine return value and how to treat the
855 * sent buffer during link congestion.
856 * - For plain, errorless user data messages we keep the buffer and
857 * return -ELINKONG.
858 * - For all other messages we discard the buffer and return -EHOSTUNREACH
859 * - For TIPC internal messages we also reset the link
860 */
861static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
862{
863 struct tipc_msg *msg = buf_msg(buf);
864 uint psz = msg_size(msg);
865 uint imp = tipc_msg_tot_importance(msg);
866 u32 oport = msg_tot_origport(msg);
867
868 if (likely(imp <= TIPC_CRITICAL_IMPORTANCE)) {
869 if (!msg_errcode(msg) && !msg_reroute_cnt(msg)) {
870 link_schedule_port(link, oport, psz);
871 return -ELINKCONG;
872 }
873 } else {
874 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
875 tipc_link_reset(link);
876 }
877 kfree_skb_list(buf);
878 return -EHOSTUNREACH;
879}
880
881/**
882 * __tipc_link_xmit2(): same as tipc_link_xmit2, but destlink is known & locked
883 * @link: link to use
884 * @buf: chain of buffers containing message
885 * Consumes the buffer chain, except when returning -ELINKCONG
886 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
887 * user data messages) or -EHOSTUNREACH (all other messages/senders)
888 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
889 * to act on the return value, since they may need to do more send attempts.
890 */
891int __tipc_link_xmit2(struct tipc_link *link, struct sk_buff *buf)
892{
893 struct tipc_msg *msg = buf_msg(buf);
894 uint psz = msg_size(msg);
895 uint qsz = link->out_queue_size;
896 uint sndlim = link->queue_limit[0];
897 uint imp = tipc_msg_tot_importance(msg);
898 uint mtu = link->max_pkt;
899 uint ack = mod(link->next_in_no - 1);
900 uint seqno = link->next_out_no;
901 uint bc_last_in = link->owner->bclink.last_in;
902 struct tipc_media_addr *addr = &link->media_addr;
903 struct sk_buff *next = buf->next;
904
905 /* Match queue limits against msg importance: */
906 if (unlikely(qsz >= link->queue_limit[imp]))
907 return tipc_link_cong(link, buf);
908
909 /* Has valid packet limit been used ? */
910 if (unlikely(psz > mtu)) {
911 kfree_skb_list(buf);
912 return -EMSGSIZE;
913 }
914
915 /* Prepare each packet for sending, and add to outqueue: */
916 while (buf) {
917 next = buf->next;
918 msg = buf_msg(buf);
919 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
920 msg_set_bcast_ack(msg, bc_last_in);
921
922 if (!link->first_out) {
923 link->first_out = buf;
924 } else if (qsz < sndlim) {
925 link->last_out->next = buf;
926 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
927 link->stats.sent_bundled++;
928 buf = next;
929 next = buf->next;
930 continue;
931 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
932 link->stats.sent_bundled++;
933 link->stats.sent_bundles++;
934 link->last_out->next = buf;
935 if (!link->next_out)
936 link->next_out = buf;
937 } else {
938 link->last_out->next = buf;
939 if (!link->next_out)
940 link->next_out = buf;
941 }
942
943 /* Send packet if possible: */
944 if (likely(++qsz <= sndlim)) {
945 tipc_bearer_send(link->bearer_id, buf, addr);
946 link->next_out = next;
947 link->unacked_window = 0;
948 }
949 seqno++;
950 link->last_out = buf;
951 buf = next;
952 }
953 link->next_out_no = seqno;
954 link->out_queue_size = qsz;
955 return 0;
956}
957
958/**
959 * tipc_link_xmit2() is the general link level function for message sending
960 * @buf: chain of buffers containing message
961 * @dsz: amount of user data to be sent
962 * @dnode: address of destination node
963 * @selector: a number used for deterministic link selection
964 * Consumes the buffer chain, except when returning -ELINKCONG
965 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
966 */
967int tipc_link_xmit2(struct sk_buff *buf, u32 dnode, u32 selector)
968{
969 struct tipc_link *link = NULL;
970 struct tipc_node *node;
971 int rc = -EHOSTUNREACH;
972
973 node = tipc_node_find(dnode);
974 if (node) {
975 tipc_node_lock(node);
976 link = node->active_links[selector & 1];
977 if (link)
978 rc = __tipc_link_xmit2(link, buf);
979 tipc_node_unlock(node);
980 }
981
982 if (link)
983 return rc;
984
985 if (likely(in_own_node(dnode)))
986 return tipc_sk_rcv(buf);
987
988 kfree_skb_list(buf);
989 return rc;
990}
991
Jon Maloyc64f7a62012-11-16 13:51:31 +0800992/*
Ying Xue247f0f32014-02-18 16:06:46 +0800993 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800994 *
995 * Give a newly added peer node the sequence number where it should
996 * start receiving and acking broadcast packets.
997 *
998 * Called with node locked
999 */
Ying Xue247f0f32014-02-18 16:06:46 +08001000static void tipc_link_sync_xmit(struct tipc_link *l)
Jon Maloyc64f7a62012-11-16 13:51:31 +08001001{
1002 struct sk_buff *buf;
1003 struct tipc_msg *msg;
1004
1005 buf = tipc_buf_acquire(INT_H_SIZE);
1006 if (!buf)
1007 return;
1008
1009 msg = buf_msg(buf);
1010 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
1011 msg_set_last_bcast(msg, l->owner->bclink.acked);
1012 link_add_chain_to_outqueue(l, buf, 0);
1013 tipc_link_push_queue(l);
1014}
1015
1016/*
Ying Xue247f0f32014-02-18 16:06:46 +08001017 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +08001018 * Receive the sequence number where we should start receiving and
1019 * acking broadcast packets from a newly added peer node, and open
1020 * up for reception of such packets.
1021 *
1022 * Called with node locked
1023 */
Ying Xue247f0f32014-02-18 16:06:46 +08001024static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +08001025{
1026 struct tipc_msg *msg = buf_msg(buf);
1027
1028 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
1029 n->bclink.recv_permitted = true;
1030 kfree_skb(buf);
1031}
1032
1033/*
Ying Xue247f0f32014-02-18 16:06:46 +08001034 * tipc_link_names_xmit - send name table entries to new neighbor
Allan Stephens9aa88c22011-05-31 13:38:02 -04001035 *
1036 * Send routine for bulk delivery of name table messages when contact
1037 * with a new neighbor occurs. No link congestion checking is performed
1038 * because name table messages *must* be delivered. The messages must be
1039 * small enough not to require fragmentation.
1040 * Called without any locks held.
1041 */
Ying Xue247f0f32014-02-18 16:06:46 +08001042void tipc_link_names_xmit(struct list_head *message_list, u32 dest)
Allan Stephens9aa88c22011-05-31 13:38:02 -04001043{
1044 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001045 struct tipc_link *l_ptr;
Allan Stephens9aa88c22011-05-31 13:38:02 -04001046 struct sk_buff *buf;
1047 struct sk_buff *temp_buf;
1048
1049 if (list_empty(message_list))
1050 return;
1051
Allan Stephens9aa88c22011-05-31 13:38:02 -04001052 n_ptr = tipc_node_find(dest);
1053 if (n_ptr) {
1054 tipc_node_lock(n_ptr);
1055 l_ptr = n_ptr->active_links[0];
1056 if (l_ptr) {
1057 /* convert circular list to linear list */
1058 ((struct sk_buff *)message_list->prev)->next = NULL;
1059 link_add_chain_to_outqueue(l_ptr,
1060 (struct sk_buff *)message_list->next, 0);
1061 tipc_link_push_queue(l_ptr);
1062 INIT_LIST_HEAD(message_list);
1063 }
1064 tipc_node_unlock(n_ptr);
1065 }
Allan Stephens9aa88c22011-05-31 13:38:02 -04001066
1067 /* discard the messages if they couldn't be sent */
Allan Stephens9aa88c22011-05-31 13:38:02 -04001068 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
1069 list_del((struct list_head *)buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -04001070 kfree_skb(buf);
Allan Stephens9aa88c22011-05-31 13:38:02 -04001071 }
1072}
1073
1074/*
Per Liden4323add2006-01-18 00:38:21 +01001075 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 */
stephen hemminger98056962014-01-04 13:47:48 -08001077static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078{
1079 struct sk_buff *buf = l_ptr->first_out;
1080 u32 r_q_size = l_ptr->retransm_queue_size;
1081 u32 r_q_head = l_ptr->retransm_queue_head;
1082
1083 /* Step to position where retransmission failed, if any, */
1084 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001086 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -04001088 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089
1090 while (buf && less(first, r_q_head)) {
1091 first = mod(first + 1);
1092 buf = buf->next;
1093 }
1094 l_ptr->retransm_queue_head = r_q_head = first;
1095 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1096 }
1097
1098 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +00001099 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001101 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001102 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001103 l_ptr->retransm_queue_head = mod(++r_q_head);
1104 l_ptr->retransm_queue_size = --r_q_size;
1105 l_ptr->stats.retransmitted++;
1106 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 }
1108
1109 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 buf = l_ptr->proto_msg_queue;
1111 if (buf) {
1112 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001113 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001114 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001115 l_ptr->unacked_window = 0;
1116 kfree_skb(buf);
1117 l_ptr->proto_msg_queue = NULL;
1118 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 }
1120
1121 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 buf = l_ptr->next_out;
1123 if (buf) {
1124 struct tipc_msg *msg = buf_msg(buf);
1125 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -04001126 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127
1128 if (mod(next - first) < l_ptr->queue_limit[0]) {
1129 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001130 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001131 tipc_bearer_send(l_ptr->bearer_id, buf,
1132 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001133 if (msg_user(msg) == MSG_BUNDLER)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -05001134 msg_set_type(msg, BUNDLE_CLOSED);
Ying Xue3c294cb2012-11-15 11:34:45 +08001135 l_ptr->next_out = buf->next;
1136 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 }
1138 }
Ying Xue3c294cb2012-11-15 11:34:45 +08001139 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140}
1141
1142/*
1143 * push_queue(): push out the unsent messages of a link where
1144 * congestion has abated. Node is locked
1145 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001146void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147{
1148 u32 res;
1149
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 do {
Per Liden4323add2006-01-18 00:38:21 +01001151 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001152 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153}
1154
Ying Xue3f5a12b2014-05-05 08:56:17 +08001155void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001156{
Allan Stephensd356eeb2006-06-25 23:40:01 -07001157 char addr_string[16];
1158 u32 i;
1159
Ying Xue3f5a12b2014-05-05 08:56:17 +08001160 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001161
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001162 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +08001163 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001164
1165 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +08001166 if (node->links[i]) {
1167 link_print(node->links[i], "Resetting link\n");
1168 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001169 }
1170 }
1171
Ying Xue3f5a12b2014-05-05 08:56:17 +08001172 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001173}
1174
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001175static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001176 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001177{
1178 struct tipc_msg *msg = buf_msg(buf);
1179
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001180 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001181
1182 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001183 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001184 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001185 tipc_link_reset(l_ptr);
1186
1187 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001188 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -07001189 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001190 char addr_string[16];
1191
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001192 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1193 pr_cont("Outstanding acks: %lu\n",
1194 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001195
Allan Stephens01d83ed2011-01-18 13:53:16 -05001196 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -07001197 tipc_node_lock(n_ptr);
1198
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001199 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001200 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +08001201 pr_info("Reception permitted: %d, Acked: %u\n",
1202 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001203 n_ptr->bclink.acked);
1204 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1205 n_ptr->bclink.last_in,
1206 n_ptr->bclink.oos_state,
1207 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001208
Allan Stephensd356eeb2006-06-25 23:40:01 -07001209 tipc_node_unlock(n_ptr);
1210
Ying Xue3f5a12b2014-05-05 08:56:17 +08001211 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001212 l_ptr->stale_count = 0;
1213 }
1214}
1215
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001216void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001217 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218{
1219 struct tipc_msg *msg;
1220
Allan Stephensd356eeb2006-06-25 23:40:01 -07001221 if (!buf)
1222 return;
1223
1224 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001225
Erik Hugne512137e2013-12-06 10:08:00 -05001226 /* Detect repeated retransmit failures */
1227 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1228 if (++l_ptr->stale_count > 100) {
1229 link_retransmit_failure(l_ptr, buf);
1230 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001231 }
1232 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001233 l_ptr->last_retransmitted = msg_seqno(msg);
1234 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001236
Neil Hormanca509102010-03-15 07:58:45 +00001237 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 msg = buf_msg(buf);
1239 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001240 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001241 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001242 buf = buf->next;
1243 retransmits--;
1244 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001246
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1248}
1249
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001250/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 * link_insert_deferred_queue - insert deferred messages back into receive chain
1252 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001253static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 struct sk_buff *buf)
1255{
1256 u32 seq_no;
1257
1258 if (l_ptr->oldest_deferred_in == NULL)
1259 return buf;
1260
Allan Stephensf9057302011-10-24 16:03:12 -04001261 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 if (seq_no == mod(l_ptr->next_in_no)) {
1263 l_ptr->newest_deferred_in->next = buf;
1264 buf = l_ptr->oldest_deferred_in;
1265 l_ptr->oldest_deferred_in = NULL;
1266 l_ptr->deferred_inqueue_sz = 0;
1267 }
1268 return buf;
1269}
1270
Allan Stephens85035562008-04-15 19:04:54 -07001271/**
1272 * link_recv_buf_validate - validate basic format of received message
1273 *
1274 * This routine ensures a TIPC message has an acceptable header, and at least
1275 * as much data as the header indicates it should. The routine also ensures
1276 * that the entire message header is stored in the main fragment of the message
1277 * buffer, to simplify future access to message header fields.
1278 *
1279 * Note: Having extra info present in the message header or data areas is OK.
1280 * TIPC will ignore the excess, under the assumption that it is optional info
1281 * introduced by a later release of the protocol.
1282 */
Allan Stephens85035562008-04-15 19:04:54 -07001283static int link_recv_buf_validate(struct sk_buff *buf)
1284{
1285 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001286 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001287 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1288 };
1289
1290 struct tipc_msg *msg;
1291 u32 tipc_hdr[2];
1292 u32 size;
1293 u32 hdr_size;
1294 u32 min_hdr_size;
1295
Erik Hugne64380a02014-02-11 11:38:26 +01001296 /* If this packet comes from the defer queue, the skb has already
1297 * been validated
1298 */
1299 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1300 return 1;
1301
Allan Stephens85035562008-04-15 19:04:54 -07001302 if (unlikely(buf->len < MIN_H_SIZE))
1303 return 0;
1304
1305 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1306 if (msg == NULL)
1307 return 0;
1308
1309 if (unlikely(msg_version(msg) != TIPC_VERSION))
1310 return 0;
1311
1312 size = msg_size(msg);
1313 hdr_size = msg_hdr_sz(msg);
1314 min_hdr_size = msg_isdata(msg) ?
1315 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1316
1317 if (unlikely((hdr_size < min_hdr_size) ||
1318 (size < hdr_size) ||
1319 (buf->len < size) ||
1320 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1321 return 0;
1322
1323 return pskb_may_pull(buf, hdr_size);
1324}
1325
Allan Stephensb02b69c2010-08-17 11:00:07 +00001326/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001327 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001328 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001329 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001330 *
1331 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1332 * structure (i.e. cannot be NULL), but bearer can be inactive.
1333 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001334void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001337 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001338 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 struct sk_buff *crs;
1340 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001341 struct tipc_msg *msg;
1342 u32 seq_no;
1343 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 u32 released = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001347 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001348
1349 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001350 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001351 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352
Allan Stephensfe13dda2008-04-15 19:03:23 -07001353 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001354 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001355 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001356
Allan Stephens85035562008-04-15 19:04:54 -07001357 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001358 msg = buf_msg(buf);
1359
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001361 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001362 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001363 else
Ying Xue247f0f32014-02-18 16:06:46 +08001364 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 continue;
1366 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001367
Allan Stephensed33a9c2011-04-05 15:15:04 -04001368 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001369 if (unlikely(!msg_short(msg) &&
1370 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001371 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001372
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001373 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001374 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001376 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001377 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001378
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001379 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001381 if (unlikely(!l_ptr))
1382 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001383
Allan Stephensb4b56102011-05-27 11:00:51 -04001384 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001385 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001386 msg_user(msg) == LINK_PROTOCOL &&
1387 (msg_type(msg) == RESET_MSG ||
1388 msg_type(msg) == ACTIVATE_MSG) &&
1389 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001390 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001391
Ying Xue10f465c2014-05-05 08:56:11 +08001392 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001393 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001394
Allan Stephens85035562008-04-15 19:04:54 -07001395 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001396 seq_no = msg_seqno(msg);
1397 ackd = msg_ack(msg);
1398
1399 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001400 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001401 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402
1403 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001404 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001405 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 struct sk_buff *next = crs->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001407 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 crs = next;
1409 released++;
1410 }
1411 if (released) {
1412 l_ptr->first_out = crs;
1413 l_ptr->out_queue_size -= released;
1414 }
Allan Stephens85035562008-04-15 19:04:54 -07001415
1416 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001418 tipc_link_push_queue(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001419
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001421 tipc_link_wakeup_ports(l_ptr, 0);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001422
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1424 l_ptr->stats.sent_acks++;
Ying Xue247f0f32014-02-18 16:06:46 +08001425 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 }
1427
Jon Paul Maloya5377832014-02-13 17:29:15 -05001428 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001429 if (unlikely(!link_working_working(l_ptr))) {
1430 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001431 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001432 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001433 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 continue;
1435 }
Ying Xue3af390e2013-10-30 11:26:57 +08001436
1437 /* Traffic message. Conditionally activate link */
1438 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1439
1440 if (link_working_working(l_ptr)) {
1441 /* Re-insert buffer in front of queue */
1442 buf->next = head;
1443 head = buf;
1444 tipc_node_unlock(n_ptr);
1445 continue;
1446 }
1447 goto unlock_discard;
1448 }
1449
1450 /* Link is now in state WORKING_WORKING */
1451 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 link_handle_out_of_seq_msg(l_ptr, buf);
1453 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001454 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 continue;
1456 }
Ying Xue3af390e2013-10-30 11:26:57 +08001457 l_ptr->next_in_no++;
1458 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001460
1461 /* Deliver packet/message to correct user: */
1462 if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL)) {
1463 if (!tipc_link_tunnel_rcv(n_ptr, &buf)) {
1464 tipc_node_unlock(n_ptr);
1465 continue;
1466 }
1467 msg = buf_msg(buf);
1468 } else if (msg_user(msg) == MSG_FRAGMENTER) {
Jon Paul Maloya5377832014-02-13 17:29:15 -05001469 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001470 if (tipc_buf_append(&l_ptr->reasm_buf, &buf)) {
Jon Paul Maloya5377832014-02-13 17:29:15 -05001471 l_ptr->stats.recv_fragmented++;
1472 msg = buf_msg(buf);
1473 } else {
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001474 if (!l_ptr->reasm_buf)
Jon Paul Maloya5377832014-02-13 17:29:15 -05001475 tipc_link_reset(l_ptr);
1476 tipc_node_unlock(n_ptr);
1477 continue;
1478 }
1479 }
1480
1481 switch (msg_user(msg)) {
1482 case TIPC_LOW_IMPORTANCE:
1483 case TIPC_MEDIUM_IMPORTANCE:
1484 case TIPC_HIGH_IMPORTANCE:
1485 case TIPC_CRITICAL_IMPORTANCE:
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001486 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001487 tipc_node_unlock(n_ptr);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001488 tipc_sk_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001490 case MSG_BUNDLER:
1491 l_ptr->stats.recv_bundles++;
1492 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1493 tipc_node_unlock(n_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +08001494 tipc_link_bundle_rcv(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001495 continue;
1496 case NAME_DISTRIBUTOR:
1497 n_ptr->bclink.recv_permitted = true;
1498 tipc_node_unlock(n_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +08001499 tipc_named_rcv(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001500 continue;
Jon Paul Maloya5377832014-02-13 17:29:15 -05001501 case BCAST_PROTOCOL:
Ying Xue247f0f32014-02-18 16:06:46 +08001502 tipc_link_sync_rcv(n_ptr, buf);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001503 break;
Ying Xue3af390e2013-10-30 11:26:57 +08001504 default:
1505 kfree_skb(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001506 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 }
Per Liden4323add2006-01-18 00:38:21 +01001508 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001509 continue;
1510unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001511 tipc_node_unlock(n_ptr);
1512discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001513 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515}
1516
Ben Hutchings2c530402012-07-10 10:55:09 +00001517/**
Allan Stephens8809b252011-10-25 10:44:35 -04001518 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1519 *
1520 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 */
Allan Stephens8809b252011-10-25 10:44:35 -04001522u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001523 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524{
Allan Stephens8809b252011-10-25 10:44:35 -04001525 struct sk_buff *queue_buf;
1526 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001527 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528
1529 buf->next = NULL;
1530
1531 /* Empty queue ? */
1532 if (*head == NULL) {
1533 *head = *tail = buf;
1534 return 1;
1535 }
1536
1537 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001538 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539 (*tail)->next = buf;
1540 *tail = buf;
1541 return 1;
1542 }
1543
Allan Stephens8809b252011-10-25 10:44:35 -04001544 /* Locate insertion point in queue, then insert; discard if duplicate */
1545 prev = head;
1546 queue_buf = *head;
1547 for (;;) {
1548 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549
Allan Stephens8809b252011-10-25 10:44:35 -04001550 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001551 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001552 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 }
Allan Stephens8809b252011-10-25 10:44:35 -04001554
1555 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557
Allan Stephens8809b252011-10-25 10:44:35 -04001558 prev = &queue_buf->next;
1559 queue_buf = queue_buf->next;
1560 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561
Allan Stephens8809b252011-10-25 10:44:35 -04001562 buf->next = queue_buf;
1563 *prev = buf;
1564 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565}
1566
Allan Stephens8809b252011-10-25 10:44:35 -04001567/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1569 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001570static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571 struct sk_buff *buf)
1572{
Allan Stephensf9057302011-10-24 16:03:12 -04001573 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574
1575 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001576 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 return;
1578 }
1579
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 l_ptr->checkpoint--;
1582
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001583 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 * Discard packet if a duplicate; otherwise add it to deferred queue
1585 * and notify peer of gap as per protocol specification
1586 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 if (less(seq_no, mod(l_ptr->next_in_no))) {
1588 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001589 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 return;
1591 }
1592
Per Liden4323add2006-01-18 00:38:21 +01001593 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1594 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 l_ptr->deferred_inqueue_sz++;
1596 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001597 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001599 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 } else
1601 l_ptr->stats.duplicates++;
1602}
1603
1604/*
1605 * Send protocol message to the other endpoint.
1606 */
Ying Xue247f0f32014-02-18 16:06:46 +08001607void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1608 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001610 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001612 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001613 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614
Allan Stephens92d2c902011-10-25 11:20:26 -04001615 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001616 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001617 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001618 l_ptr->proto_msg_queue = NULL;
1619 }
1620
Ying Xue77a7e072013-12-10 20:45:44 -08001621 /* Don't send protocol message during link changeover */
1622 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001624
1625 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001626 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001627 return;
1628
Allan Stephens92d2c902011-10-25 11:20:26 -04001629 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001631 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001632 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001633 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634
1635 if (msg_typ == STATE_MSG) {
1636 u32 next_sent = mod(l_ptr->next_out_no);
1637
Per Liden4323add2006-01-18 00:38:21 +01001638 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 return;
1640 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001641 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 msg_set_next_sent(msg, next_sent);
1643 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001644 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645 gap = mod(rec - mod(l_ptr->next_in_no));
1646 }
1647 msg_set_seq_gap(msg, gap);
1648 if (gap)
1649 l_ptr->stats.sent_nacks++;
1650 msg_set_link_tolerance(msg, tolerance);
1651 msg_set_linkprio(msg, priority);
1652 msg_set_max_pkt(msg, ack_mtu);
1653 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1654 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001655 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 u32 mtu = l_ptr->max_pkt;
1657
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001658 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 link_working_working(l_ptr) &&
1660 l_ptr->fsm_msg_cnt) {
1661 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001662 if (l_ptr->max_pkt_probes == 10) {
1663 l_ptr->max_pkt_target = (msg_size - 4);
1664 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001666 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669
1670 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001671 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 l_ptr->stats.sent_states++;
1673 } else { /* RESET_MSG or ACTIVATE_MSG */
1674 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1675 msg_set_seq_gap(msg, 0);
1676 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001677 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 msg_set_link_tolerance(msg, l_ptr->tolerance);
1679 msg_set_linkprio(msg, l_ptr->priority);
1680 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1681 }
1682
Allan Stephens75f0aa42011-02-28 15:30:20 -05001683 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1684 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001686 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687
1688 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1689
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001690 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 if (!buf)
1692 return;
1693
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001694 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001695 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696
Ying Xue7a2f7d12014-04-21 10:55:46 +08001697 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001698 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001699 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700}
1701
1702/*
1703 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001704 * Note that network plane id propagates through the network, and may
1705 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 */
Ying Xue247f0f32014-02-18 16:06:46 +08001707static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708{
1709 u32 rec_gap = 0;
1710 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 u32 msg_tol;
1713 struct tipc_msg *msg = buf_msg(buf);
1714
Ying Xue77a7e072013-12-10 20:45:44 -08001715 /* Discard protocol message during link changeover */
1716 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 goto exit;
1718
Ying Xue7a2f7d12014-04-21 10:55:46 +08001719 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001721 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001724
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001726 if (!link_working_unknown(l_ptr) &&
1727 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001728 if (less_eq(msg_session(msg), l_ptr->peer_session))
1729 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001731
1732 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1733 link_working_unknown(l_ptr))) {
1734 /*
1735 * peer has lost contact -- don't allow peer's links
1736 * to reactivate before we recognize loss & clean up
1737 */
Ying Xueca9cf062014-05-08 08:54:40 +08001738 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001739 }
1740
Allan Stephens47361c82011-10-26 10:55:16 -04001741 link_state_event(l_ptr, RESET_MSG);
1742
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 /* fall thru' */
1744 case ACTIVATE_MSG:
1745 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1747
Allan Stephens2db99832010-12-31 18:59:33 +00001748 msg_tol = msg_link_tolerance(msg);
1749 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 link_set_supervision_props(l_ptr, msg_tol);
1751
1752 if (msg_linkprio(msg) > l_ptr->priority)
1753 l_ptr->priority = msg_linkprio(msg);
1754
1755 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001756 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757 if (max_pkt_info < l_ptr->max_pkt_target)
1758 l_ptr->max_pkt_target = max_pkt_info;
1759 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1760 l_ptr->max_pkt = l_ptr->max_pkt_target;
1761 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001762 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764
Allan Stephens4d753132011-10-25 12:19:05 -04001765 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001766 if (!tipc_node_is_up(l_ptr->owner)) {
1767 l_ptr->owner->bclink.last_sent =
1768 l_ptr->owner->bclink.last_in =
1769 msg_last_bcast(msg);
1770 l_ptr->owner->bclink.oos_state = 0;
1771 }
Allan Stephens4d753132011-10-25 12:19:05 -04001772
Per Lidenb97bf3f2006-01-02 19:04:38 +01001773 l_ptr->peer_session = msg_session(msg);
1774 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001775
1776 if (msg_type(msg) == ACTIVATE_MSG)
1777 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 break;
1779 case STATE_MSG:
1780
Allan Stephens2db99832010-12-31 18:59:33 +00001781 msg_tol = msg_link_tolerance(msg);
1782 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001784
1785 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001787 pr_warn("%s<%s>, priority change %u->%u\n",
1788 link_rst_msg, l_ptr->name, l_ptr->priority,
1789 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001791 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792 break;
1793 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001794
1795 /* Record reception; force mismatch at next timeout: */
1796 l_ptr->checkpoint--;
1797
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1799 l_ptr->stats.recv_states++;
1800 if (link_reset_unknown(l_ptr))
1801 break;
1802
1803 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001804 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 mod(l_ptr->next_in_no));
1806 }
1807
1808 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001809 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001810 l_ptr->max_pkt = max_pkt_ack;
1811 l_ptr->max_pkt_probes = 0;
1812 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813
1814 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001815 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001817 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001818 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001819 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820
1821 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001822 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001823 tipc_bclink_update_link_state(l_ptr->owner,
1824 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825
1826 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001827 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1828 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 }
1830 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001831 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001832 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1833 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 }
1835 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836 }
1837exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001838 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839}
1840
1841
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001842/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1843 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001845static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1846 struct tipc_msg *tunnel_hdr,
1847 struct tipc_msg *msg,
1848 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001850 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 struct sk_buff *buf;
1852 u32 length = msg_size(msg);
1853
1854 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001855 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001856 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001858 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001860 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001861 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001862 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001864 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001865 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1866 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Ying Xue247f0f32014-02-18 16:06:46 +08001867 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868}
1869
1870
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001871/* tipc_link_failover_send_queue(): A link has gone down, but a second
1872 * link is still active. We can do failover. Tunnel the failing link's
1873 * whole send queue via the remaining link. This way, we don't lose
1874 * any packets, and sequence order is preserved for subsequent traffic
1875 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001877void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878{
1879 u32 msgcount = l_ptr->out_queue_size;
1880 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001881 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001883 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884
1885 if (!tunnel)
1886 return;
1887
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001888 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001889 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1891 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001892
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 if (!l_ptr->first_out) {
1894 struct sk_buff *buf;
1895
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001896 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001898 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xue247f0f32014-02-18 16:06:46 +08001900 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001902 pr_warn("%sunable to send changeover msg\n",
1903 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904 }
1905 return;
1906 }
Allan Stephensf1310722006-06-25 23:51:37 -07001907
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001908 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001909 l_ptr->owner->active_links[1]);
1910
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 while (crs) {
1912 struct tipc_msg *msg = buf_msg(crs);
1913
1914 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001916 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917
Florian Westphald788d802007-08-02 19:28:06 -07001918 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001920 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001921 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1922 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923 pos += align(msg_size(m));
1924 m = (struct tipc_msg *)pos;
1925 }
1926 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001927 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1928 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 }
1930 crs = crs->next;
1931 }
1932}
1933
Ying Xue247f0f32014-02-18 16:06:46 +08001934/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001935 * duplicate of the first link's send queue via the new link. This way, we
1936 * are guaranteed that currently queued packets from a socket are delivered
1937 * before future traffic from the same socket, even if this is using the
1938 * new link. The last arriving copy of each duplicate packet is dropped at
1939 * the receiving end by the regular protocol check, so packet cardinality
1940 * and sequence order is preserved per sender/receiver socket pair.
1941 * Owner node is locked.
1942 */
Ying Xue247f0f32014-02-18 16:06:46 +08001943void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001944 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945{
1946 struct sk_buff *iter;
1947 struct tipc_msg tunnel_hdr;
1948
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001949 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001950 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1952 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1953 iter = l_ptr->first_out;
1954 while (iter) {
1955 struct sk_buff *outbuf;
1956 struct tipc_msg *msg = buf_msg(iter);
1957 u32 length = msg_size(msg);
1958
1959 if (msg_user(msg) == MSG_BUNDLER)
1960 msg_set_type(msg, CLOSED_MSG);
1961 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001962 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001964 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001966 pr_warn("%sunable to send duplicate msg\n",
1967 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 return;
1969 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001970 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1971 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1972 length);
Ying Xue247f0f32014-02-18 16:06:46 +08001973 __tipc_link_xmit(tunnel, outbuf);
Per Liden4323add2006-01-18 00:38:21 +01001974 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 return;
1976 iter = iter->next;
1977 }
1978}
1979
Per Lidenb97bf3f2006-01-02 19:04:38 +01001980/**
1981 * buf_extract - extracts embedded TIPC message from another message
1982 * @skb: encapsulating message buffer
1983 * @from_pos: offset to extract from
1984 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001985 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986 * encapsulating message itself is left unchanged.
1987 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1989{
1990 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1991 u32 size = msg_size(msg);
1992 struct sk_buff *eb;
1993
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001994 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001995 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001996 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997 return eb;
1998}
1999
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05002000
2001
2002/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
2003 * Owner node is locked.
2004 */
2005static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
2006 struct sk_buff *t_buf)
2007{
2008 struct sk_buff *buf;
2009
2010 if (!tipc_link_is_up(l_ptr))
2011 return;
2012
2013 buf = buf_extract(t_buf, INT_H_SIZE);
2014 if (buf == NULL) {
2015 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
2016 return;
2017 }
2018
2019 /* Add buffer to deferred queue, if applicable: */
2020 link_handle_out_of_seq_msg(l_ptr, buf);
2021}
2022
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002023/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
2024 * Owner node is locked.
2025 */
2026static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
2027 struct sk_buff *t_buf)
2028{
2029 struct tipc_msg *t_msg = buf_msg(t_buf);
2030 struct sk_buff *buf = NULL;
2031 struct tipc_msg *msg;
2032
2033 if (tipc_link_is_up(l_ptr))
2034 tipc_link_reset(l_ptr);
2035
2036 /* First failover packet? */
2037 if (l_ptr->exp_msg_count == START_CHANGEOVER)
2038 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
2039
2040 /* Should there be an inner packet? */
2041 if (l_ptr->exp_msg_count) {
2042 l_ptr->exp_msg_count--;
2043 buf = buf_extract(t_buf, INT_H_SIZE);
2044 if (buf == NULL) {
2045 pr_warn("%sno inner failover pkt\n", link_co_err);
2046 goto exit;
2047 }
2048 msg = buf_msg(buf);
2049
2050 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
2051 kfree_skb(buf);
2052 buf = NULL;
2053 goto exit;
2054 }
2055 if (msg_user(msg) == MSG_FRAGMENTER) {
2056 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04002057 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002058 }
2059 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002060exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05002061 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
2062 tipc_node_detach_link(l_ptr->owner, l_ptr);
2063 kfree(l_ptr);
2064 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05002065 return buf;
2066}
2067
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05002068/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002069 * via other link as result of a failover (ORIGINAL_MSG) or
2070 * a new active link (DUPLICATE_MSG). Failover packets are
2071 * returned to the active link for delivery upwards.
2072 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05002074static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002075 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002077 struct sk_buff *t_buf = *buf;
2078 struct tipc_link *l_ptr;
2079 struct tipc_msg *t_msg = buf_msg(t_buf);
2080 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05002082 *buf = NULL;
2083
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002084 if (bearer_id >= MAX_BEARERS)
2085 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05002086
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002087 l_ptr = n_ptr->links[bearer_id];
2088 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002090
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002091 if (msg_type(t_msg) == DUPLICATE_MSG)
2092 tipc_link_dup_rcv(l_ptr, t_buf);
2093 else if (msg_type(t_msg) == ORIGINAL_MSG)
2094 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05002095 else
2096 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05002098 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05002099 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100}
2101
2102/*
2103 * Bundler functionality:
2104 */
Ying Xue247f0f32014-02-18 16:06:46 +08002105void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106{
2107 u32 msgcount = msg_msgcnt(buf_msg(buf));
2108 u32 pos = INT_H_SIZE;
2109 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05002110 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002111
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112 while (msgcount--) {
2113 obuf = buf_extract(buf, pos);
2114 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002115 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07002116 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002117 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05002118 omsg = buf_msg(obuf);
2119 pos += align(msg_size(omsg));
2120 if (msg_isdata(omsg) || (msg_user(omsg) == CONN_MANAGER)) {
2121 tipc_sk_rcv(obuf);
2122 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
2123 tipc_named_rcv(obuf);
2124 } else {
2125 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
2126 kfree_skb(obuf);
2127 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002129 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002130}
2131
2132/*
2133 * Fragmentation/defragmentation:
2134 */
2135
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002136/*
Ying Xue247f0f32014-02-18 16:06:46 +08002137 * tipc_link_frag_xmit: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002138 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002139 * Returns user data length.
2140 */
Ying Xue247f0f32014-02-18 16:06:46 +08002141static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142{
Allan Stephens77561552011-04-17 13:06:23 -04002143 struct sk_buff *buf_chain = NULL;
2144 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002145 struct tipc_msg *inmsg = buf_msg(buf);
2146 struct tipc_msg fragm_hdr;
2147 u32 insize = msg_size(inmsg);
2148 u32 dsz = msg_data_sz(inmsg);
2149 unchar *crs = buf->data;
2150 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002151 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 u32 fragm_sz = pack_sz - INT_H_SIZE;
Allan Stephens77561552011-04-17 13:06:23 -04002153 u32 fragm_no = 0;
Allan Stephens9c396a72008-06-04 17:36:58 -07002154 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155
2156 if (msg_short(inmsg))
2157 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002158 else
2159 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002160
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161 /* Prepare reusable fragment header: */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002162 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002163 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164
2165 /* Chop up message: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002166 while (rest > 0) {
2167 struct sk_buff *fragm;
2168
2169 if (rest <= fragm_sz) {
2170 fragm_sz = rest;
2171 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2172 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002173 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002174 if (fragm == NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002175 kfree_skb(buf);
Ying Xued77b3832013-12-10 20:45:38 -08002176 kfree_skb_list(buf_chain);
Allan Stephens77561552011-04-17 13:06:23 -04002177 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002178 }
2179 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Allan Stephens77561552011-04-17 13:06:23 -04002180 fragm_no++;
2181 msg_set_fragm_no(&fragm_hdr, fragm_no);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002182 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2183 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2184 fragm_sz);
Allan Stephens77561552011-04-17 13:06:23 -04002185 buf_chain_tail->next = fragm;
2186 buf_chain_tail = fragm;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002187
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 rest -= fragm_sz;
2189 crs += fragm_sz;
2190 msg_set_type(&fragm_hdr, FRAGMENT);
2191 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002192 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002193
2194 /* Append chain of fragments to send queue & send them */
Allan Stephens77561552011-04-17 13:06:23 -04002195 l_ptr->long_msg_seq_no++;
2196 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2197 l_ptr->stats.sent_fragments += fragm_no;
2198 l_ptr->stats.sent_fragmented++;
2199 tipc_link_push_queue(l_ptr);
2200
Per Lidenb97bf3f2006-01-02 19:04:38 +01002201 return dsz;
2202}
2203
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002204static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205{
Allan Stephens5413b4c2011-01-18 13:24:55 -05002206 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2207 return;
2208
Per Lidenb97bf3f2006-01-02 19:04:38 +01002209 l_ptr->tolerance = tolerance;
2210 l_ptr->continuity_interval =
2211 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2212 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2213}
2214
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002215void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002216{
2217 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002218 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2219 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2220 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2221 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002222 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002223 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2224 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2225 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2226 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002227 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002228 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2229 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2230 /* FRAGMENT and LAST_FRAGMENT packets */
2231 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2232}
2233
Jon Paul Maloye099e862014-02-13 17:29:18 -05002234/* tipc_link_find_owner - locate owner node of link by link's name
2235 * @name: pointer to link name string
2236 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002237 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05002238 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002239 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05002240static struct tipc_node *tipc_link_find_owner(const char *link_name,
2241 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002242{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002243 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002244 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002245 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002246 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002247
Jon Paul Maloye099e862014-02-13 17:29:18 -05002248 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08002249 rcu_read_lock();
2250 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002251 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02002252 for (i = 0; i < MAX_BEARERS; i++) {
2253 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05002254 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
2255 *bearer_id = i;
2256 found_node = n_ptr;
2257 break;
2258 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02002259 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002260 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002261 if (found_node)
2262 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002263 }
Ying Xue6c7a7622014-03-27 12:54:37 +08002264 rcu_read_unlock();
2265
Jon Paul Maloye099e862014-02-13 17:29:18 -05002266 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267}
2268
Allan Stephens5c216e12011-10-18 11:34:29 -04002269/**
2270 * link_value_is_valid -- validate proposed link tolerance/priority/window
2271 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002272 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2273 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002274 *
2275 * Returns 1 if value is within range, 0 if not.
2276 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002277static int link_value_is_valid(u16 cmd, u32 new_value)
2278{
2279 switch (cmd) {
2280 case TIPC_CMD_SET_LINK_TOL:
2281 return (new_value >= TIPC_MIN_LINK_TOL) &&
2282 (new_value <= TIPC_MAX_LINK_TOL);
2283 case TIPC_CMD_SET_LINK_PRI:
2284 return (new_value <= TIPC_MAX_LINK_PRI);
2285 case TIPC_CMD_SET_LINK_WINDOW:
2286 return (new_value >= TIPC_MIN_LINK_WIN) &&
2287 (new_value <= TIPC_MAX_LINK_WIN);
2288 }
2289 return 0;
2290}
2291
Allan Stephens5c216e12011-10-18 11:34:29 -04002292/**
2293 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002294 * @name: ptr to link, bearer, or media name
2295 * @new_value: new value of link, bearer, or media setting
2296 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002297 *
Ying Xue7216cd92014-04-21 10:55:48 +08002298 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002299 *
2300 * Returns 0 if value updated and negative value on error.
2301 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002302static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2303{
2304 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002305 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002306 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002307 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002308 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002309 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002310
Jon Paul Maloye099e862014-02-13 17:29:18 -05002311 node = tipc_link_find_owner(name, &bearer_id);
2312 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002313 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002314 l_ptr = node->links[bearer_id];
2315
2316 if (l_ptr) {
2317 switch (cmd) {
2318 case TIPC_CMD_SET_LINK_TOL:
2319 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002320 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2321 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002322 break;
2323 case TIPC_CMD_SET_LINK_PRI:
2324 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002325 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2326 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002327 break;
2328 case TIPC_CMD_SET_LINK_WINDOW:
2329 tipc_link_set_queue_limits(l_ptr, new_value);
2330 break;
2331 default:
2332 res = -EINVAL;
2333 break;
2334 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002335 }
2336 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002337 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002338 }
2339
2340 b_ptr = tipc_bearer_find(name);
2341 if (b_ptr) {
2342 switch (cmd) {
2343 case TIPC_CMD_SET_LINK_TOL:
2344 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002345 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002346 case TIPC_CMD_SET_LINK_PRI:
2347 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002348 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002349 case TIPC_CMD_SET_LINK_WINDOW:
2350 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002351 break;
2352 default:
2353 res = -EINVAL;
2354 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002355 }
Ying Xue636c0372013-10-18 07:23:20 +02002356 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002357 }
2358
2359 m_ptr = tipc_media_find(name);
2360 if (!m_ptr)
2361 return -ENODEV;
2362 switch (cmd) {
2363 case TIPC_CMD_SET_LINK_TOL:
2364 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002365 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002366 case TIPC_CMD_SET_LINK_PRI:
2367 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002368 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002369 case TIPC_CMD_SET_LINK_WINDOW:
2370 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002371 break;
2372 default:
2373 res = -EINVAL;
2374 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002375 }
Ying Xue636c0372013-10-18 07:23:20 +02002376 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002377}
2378
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002379struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002380 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381{
2382 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002383 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002384 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385
2386 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002387 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002388
2389 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2390 new_value = ntohl(args->value);
2391
Allan Stephens5c216e12011-10-18 11:34:29 -04002392 if (!link_value_is_valid(cmd, new_value))
2393 return tipc_cfg_reply_error_string(
2394 "cannot change, value invalid");
2395
Per Liden4323add2006-01-18 00:38:21 +01002396 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002397 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002398 (tipc_bclink_set_queue_limits(new_value) == 0))
2399 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002400 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002401 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002402 }
2403
Allan Stephens5c216e12011-10-18 11:34:29 -04002404 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002406 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002407
Per Liden4323add2006-01-18 00:38:21 +01002408 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409}
2410
2411/**
2412 * link_reset_statistics - reset link statistics
2413 * @l_ptr: pointer to link
2414 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002415static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002416{
2417 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2418 l_ptr->stats.sent_info = l_ptr->next_out_no;
2419 l_ptr->stats.recv_info = l_ptr->next_in_no;
2420}
2421
Per Liden4323add2006-01-18 00:38:21 +01002422struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423{
2424 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002425 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002426 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002427 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002428
2429 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002430 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002431
2432 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002433 if (!strcmp(link_name, tipc_bclink_name)) {
2434 if (tipc_bclink_reset_stats())
2435 return tipc_cfg_reply_error_string("link not found");
2436 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002438 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002439 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002440 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002441
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002442 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002443 l_ptr = node->links[bearer_id];
2444 if (!l_ptr) {
2445 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002446 return tipc_cfg_reply_error_string("link not found");
2447 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002449 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002450 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451}
2452
2453/**
2454 * percent - convert count to a percentage of total (rounding up or down)
2455 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002456static u32 percent(u32 count, u32 total)
2457{
2458 return (count * 100 + (total / 2)) / total;
2459}
2460
2461/**
Per Liden4323add2006-01-18 00:38:21 +01002462 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002463 * @name: link name
2464 * @buf: print buffer area
2465 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002467 * Returns length of print buffer data string (or 0 if error)
2468 */
Per Liden4323add2006-01-18 00:38:21 +01002469static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002471 struct tipc_link *l;
2472 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002473 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002474 char *status;
2475 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002476 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002477 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002478
Per Liden4323add2006-01-18 00:38:21 +01002479 if (!strcmp(name, tipc_bclink_name))
2480 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002481
Jon Paul Maloye099e862014-02-13 17:29:18 -05002482 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002483 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002484 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002485
Per Liden4323add2006-01-18 00:38:21 +01002486 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002487
2488 l = node->links[bearer_id];
2489 if (!l) {
2490 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002491 return 0;
2492 }
2493
Erik Hugnedc1aed32012-06-29 00:50:23 -04002494 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495
Erik Hugnedc1aed32012-06-29 00:50:23 -04002496 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002497 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002498 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002499 status = "STANDBY";
2500 else
2501 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002502
2503 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2504 " %s MTU:%u Priority:%u Tolerance:%u ms"
2505 " Window:%u packets\n",
2506 l->name, status, l->max_pkt, l->priority,
2507 l->tolerance, l->queue_limit[0]);
2508
2509 ret += tipc_snprintf(buf + ret, buf_size - ret,
2510 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2511 l->next_in_no - s->recv_info, s->recv_fragments,
2512 s->recv_fragmented, s->recv_bundles,
2513 s->recv_bundled);
2514
2515 ret += tipc_snprintf(buf + ret, buf_size - ret,
2516 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2517 l->next_out_no - s->sent_info, s->sent_fragments,
2518 s->sent_fragmented, s->sent_bundles,
2519 s->sent_bundled);
2520
2521 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002522 if (!profile_total)
2523 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002524
2525 ret += tipc_snprintf(buf + ret, buf_size - ret,
2526 " TX profile sample:%u packets average:%u octets\n"
2527 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2528 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2529 s->msg_length_counts,
2530 s->msg_lengths_total / profile_total,
2531 percent(s->msg_length_profile[0], profile_total),
2532 percent(s->msg_length_profile[1], profile_total),
2533 percent(s->msg_length_profile[2], profile_total),
2534 percent(s->msg_length_profile[3], profile_total),
2535 percent(s->msg_length_profile[4], profile_total),
2536 percent(s->msg_length_profile[5], profile_total),
2537 percent(s->msg_length_profile[6], profile_total));
2538
2539 ret += tipc_snprintf(buf + ret, buf_size - ret,
2540 " RX states:%u probes:%u naks:%u defs:%u"
2541 " dups:%u\n", s->recv_states, s->recv_probes,
2542 s->recv_nacks, s->deferred_recv, s->duplicates);
2543
2544 ret += tipc_snprintf(buf + ret, buf_size - ret,
2545 " TX states:%u probes:%u naks:%u acks:%u"
2546 " dups:%u\n", s->sent_states, s->sent_probes,
2547 s->sent_nacks, s->sent_acks, s->retransmitted);
2548
2549 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002550 " Congestion link:%u Send queue"
2551 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002552 s->max_queue_sz, s->queue_sz_counts ?
2553 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554
Per Liden4323add2006-01-18 00:38:21 +01002555 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002556 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002557}
2558
Per Liden4323add2006-01-18 00:38:21 +01002559struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002560{
2561 struct sk_buff *buf;
2562 struct tlv_desc *rep_tlv;
2563 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002564 int pb_len;
2565 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002566
2567 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002568 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002569
Erik Hugnedc1aed32012-06-29 00:50:23 -04002570 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002571 if (!buf)
2572 return NULL;
2573
2574 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002575 pb = TLV_DATA(rep_tlv);
2576 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002577 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002578 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002580 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002581 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002582 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002583 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002584 skb_put(buf, TLV_SPACE(str_len));
2585 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2586
2587 return buf;
2588}
2589
Per Lidenb97bf3f2006-01-02 19:04:38 +01002590/**
Per Liden4323add2006-01-18 00:38:21 +01002591 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002592 * @dest: network address of destination node
2593 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002594 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002595 * If no active link can be found, uses default maximum packet size.
2596 */
Per Liden4323add2006-01-18 00:38:21 +01002597u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002598{
David S. Miller6c000552008-09-02 23:38:32 -07002599 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002600 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002601 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002602
Per Lidenb97bf3f2006-01-02 19:04:38 +01002603 if (dest == tipc_own_addr)
2604 return MAX_MSG_SIZE;
2605
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002606 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002607 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002608 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609 l_ptr = n_ptr->active_links[selector & 1];
2610 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002611 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002612 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002613 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002614 return res;
2615}
2616
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002617static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002619 struct tipc_bearer *b_ptr;
2620
2621 rcu_read_lock();
2622 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2623 if (b_ptr)
2624 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2625 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002626
Per Lidenb97bf3f2006-01-02 19:04:38 +01002627 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002628 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002629 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002630 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002631 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002632 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002633 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002634 pr_cont(":WW\n");
2635 else
2636 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002637}