blob: 6c775a107a021391c6f0fb73a2a1354aba0bd21a [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 void tipc_link_sync_xmit(struct tipc_link *l);
89static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +020090static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf);
91static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +000092
Per Lidenb97bf3f2006-01-02 19:04:38 +010093/*
Sam Ravnborg05790c62006-03-20 22:37:04 -080094 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +010095 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080096static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +010097{
98 return (i + 3) & ~3u;
99}
100
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500101static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800103 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900105
Ying Xue7a2f7d12014-04-21 10:55:46 +0800106 rcu_read_lock();
107 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
108 if (!b_ptr) {
109 rcu_read_unlock();
110 return;
111 }
112 max_pkt = (b_ptr->mtu & ~3);
113 rcu_read_unlock();
114
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 if (max_pkt > MAX_MSG_SIZE)
116 max_pkt = MAX_MSG_SIZE;
117
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900118 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
120 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900121 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 l_ptr->max_pkt = MAX_PKT_DEFAULT;
123
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900124 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125}
126
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500127static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128{
129 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400130 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 return mod(l_ptr->next_out_no);
132}
133
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500134static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135{
136 return mod(link_next_sent(l_ptr) - 1);
137}
138
139/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800140 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500142int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143{
144 if (!l_ptr)
145 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000146 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147}
148
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500149int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000151 return (l_ptr->owner->active_links[0] == l_ptr) ||
152 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153}
154
155/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 * link_timeout - handle expiration of link timer
157 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500159static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160{
Per Liden4323add2006-01-18 00:38:21 +0100161 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162
163 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
165 l_ptr->stats.queue_sz_counts++;
166
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 if (l_ptr->first_out) {
168 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
169 u32 length = msg_size(msg);
170
Joe Perchesf64f9e72009-11-29 16:55:45 -0800171 if ((msg_user(msg) == MSG_FRAGMENTER) &&
172 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 length = msg_size(msg_get_wrapped(msg));
174 }
175 if (length) {
176 l_ptr->stats.msg_lengths_total += length;
177 l_ptr->stats.msg_length_counts++;
178 if (length <= 64)
179 l_ptr->stats.msg_length_profile[0]++;
180 else if (length <= 256)
181 l_ptr->stats.msg_length_profile[1]++;
182 else if (length <= 1024)
183 l_ptr->stats.msg_length_profile[2]++;
184 else if (length <= 4096)
185 l_ptr->stats.msg_length_profile[3]++;
186 else if (length <= 16384)
187 l_ptr->stats.msg_length_profile[4]++;
188 else if (length <= 32768)
189 l_ptr->stats.msg_length_profile[5]++;
190 else
191 l_ptr->stats.msg_length_profile[6]++;
192 }
193 }
194
195 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196
197 link_state_event(l_ptr, TIMEOUT_EVT);
198
199 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100200 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201
Per Liden4323add2006-01-18 00:38:21 +0100202 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203}
204
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500205static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206{
207 k_start_timer(&l_ptr->timer, time);
208}
209
210/**
Per Liden4323add2006-01-18 00:38:21 +0100211 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500212 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900215 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 * Returns pointer to link.
217 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500218struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500219 struct tipc_bearer *b_ptr,
220 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500222 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 struct tipc_msg *msg;
224 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500225 char addr_string[16];
226 u32 peer = n_ptr->addr;
227
228 if (n_ptr->link_cnt >= 2) {
229 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400230 pr_err("Attempt to establish third link to %s\n", addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500231 return NULL;
232 }
233
234 if (n_ptr->links[b_ptr->identity]) {
235 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400236 pr_err("Attempt to establish second link on <%s> to %s\n",
237 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500238 return NULL;
239 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700241 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400243 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 return NULL;
245 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246
247 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500248 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400249 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900251 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 if_name,
253 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400254 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500256 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 l_ptr->checkpoint = 1;
Allan Stephensf882cb72011-04-07 09:43:27 -0400258 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800259 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400260 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 l_ptr->state = RESET_UNKNOWN;
262
263 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
264 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000265 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700267 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 msg_set_bearer_id(msg, b_ptr->identity);
269 strcpy((char *)msg_data(msg), if_name);
270
271 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400272 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273
Ying Xue7a2f7d12014-04-21 10:55:46 +0800274 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 link_init_max_pkt(l_ptr);
276
277 l_ptr->next_out_no = 1;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400278 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279
280 link_reset_statistics(l_ptr);
281
Allan Stephens37b9c082011-02-28 11:32:27 -0500282 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500284 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
285 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500286
287 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 return l_ptr;
290}
291
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500292void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500293{
294 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500295 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500296
Ying Xue6c7a7622014-03-27 12:54:37 +0800297 rcu_read_lock();
298 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800299 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500300 l_ptr = n_ptr->links[bearer_id];
301 if (l_ptr) {
302 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500303 if (shutting_down || !tipc_node_is_up(n_ptr)) {
304 tipc_node_detach_link(l_ptr->owner, l_ptr);
305 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800306 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500307
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500308 /* Nobody else can access this link now: */
309 del_timer_sync(&l_ptr->timer);
310 kfree(l_ptr);
311 } else {
312 /* Detach/delete when failover is finished: */
313 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800314 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500315 del_timer_sync(&l_ptr->timer);
316 }
Ying Xuec61dd612014-02-13 17:29:09 -0500317 continue;
318 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800319 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500320 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800321 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500322}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323
324/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400325 * link_schedule_user - schedule user for wakeup after congestion
326 * @link: congested link
327 * @oport: sending port
328 * @chain_sz: size of buffer chain that was attempted sent
329 * @imp: importance of message attempted sent
330 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400332static bool link_schedule_user(struct tipc_link *link, u32 oport,
333 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400335 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400337 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
338 tipc_own_addr, oport, 0, 0);
339 if (!buf)
340 return false;
341 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
342 TIPC_SKB_CB(buf)->chain_imp = imp;
343 __skb_queue_tail(&link->waiting_sks, buf);
344 link->stats.link_congs++;
345 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346}
347
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400348/**
349 * link_prepare_wakeup - prepare users for wakeup after congestion
350 * @link: congested link
351 * Move a number of waiting users, as permitted by available space in
352 * the send queue, from link wait queue to node wait queue for wakeup
353 */
354static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400356 struct sk_buff_head *wq = &link->waiting_sks;
357 struct sk_buff *buf;
358 uint pend_qsz = link->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400360 for (buf = skb_peek(wq); buf; buf = skb_peek(wq)) {
361 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(buf)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400363 pend_qsz += TIPC_SKB_CB(buf)->chain_sz;
364 __skb_queue_tail(&link->owner->waiting_sks, __skb_dequeue(wq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366}
367
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900368/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 * link_release_outqueue - purge link's outbound message queue
370 * @l_ptr: pointer to link
371 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500372static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373{
Ying Xued77b3832013-12-10 20:45:38 -0800374 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 l_ptr->first_out = NULL;
376 l_ptr->out_queue_size = 0;
377}
378
379/**
Per Liden4323add2006-01-18 00:38:21 +0100380 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381 * @l_ptr: pointer to link
382 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500383void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400385 kfree_skb(l_ptr->reasm_buf);
386 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387}
388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900389/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500390 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 * @l_ptr: pointer to link
392 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500393void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394{
Ying Xued77b3832013-12-10 20:45:38 -0800395 kfree_skb_list(l_ptr->oldest_deferred_in);
396 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100397 tipc_link_reset_fragments(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400398 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 l_ptr->proto_msg_queue = NULL;
400}
401
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500402void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 u32 prev_state = l_ptr->state;
405 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700406 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400407 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900408
Allan Stephensa686e682008-06-04 17:29:39 -0700409 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410
Allan Stephensa686e682008-06-04 17:29:39 -0700411 /* Link is down, accept any session */
412 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900414 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900416
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418
419 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
420 return;
421
Per Liden4323add2006-01-18 00:38:21 +0100422 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800423 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000424
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500425 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 l_ptr->reset_checkpoint = checkpoint;
427 l_ptr->exp_msg_count = START_CHANGEOVER;
428 }
429
430 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400432 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 l_ptr->proto_msg_queue = NULL;
Ying Xued77b3832013-12-10 20:45:38 -0800434 kfree_skb_list(l_ptr->oldest_deferred_in);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400435 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
436 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
437 owner->action_flags |= TIPC_WAKEUP_USERS;
438 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 l_ptr->retransm_queue_head = 0;
440 l_ptr->retransm_queue_size = 0;
441 l_ptr->last_out = NULL;
442 l_ptr->first_out = NULL;
443 l_ptr->next_out = NULL;
444 l_ptr->unacked_window = 0;
445 l_ptr->checkpoint = 1;
446 l_ptr->next_out_no = 1;
447 l_ptr->deferred_inqueue_sz = 0;
448 l_ptr->oldest_deferred_in = NULL;
449 l_ptr->newest_deferred_in = NULL;
450 l_ptr->fsm_msg_cnt = 0;
451 l_ptr->stale_count = 0;
452 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453}
454
Ying Xuec61dd612014-02-13 17:29:09 -0500455void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500456{
457 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500458 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500459
Ying Xue6c7a7622014-03-27 12:54:37 +0800460 rcu_read_lock();
461 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800462 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500463 l_ptr = n_ptr->links[bearer_id];
464 if (l_ptr)
465 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800466 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500467 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800468 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500469}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500471static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472{
Allan Stephens5392d642006-06-25 23:52:50 -0700473 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100474 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800475 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476}
477
478/**
479 * link_state_event - link finite state machine
480 * @l_ptr: pointer to link
481 * @event: state machine event to process
482 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000483static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500485 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 u32 cont_intv = l_ptr->continuity_interval;
487
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500488 if (l_ptr->flags & LINK_STOPPED)
489 return;
490
Ying Xue135daee2014-02-13 17:29:08 -0500491 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 return; /* Not yet. */
493
Ying Xue77a7e072013-12-10 20:45:44 -0800494 /* Check whether changeover is going on */
495 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000496 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800498 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
501 switch (l_ptr->state) {
502 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 switch (event) {
504 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 break;
507 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 if (l_ptr->next_in_no != l_ptr->checkpoint) {
509 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100510 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800511 tipc_link_proto_xmit(l_ptr, STATE_MSG,
512 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 l_ptr->fsm_msg_cnt++;
514 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800515 tipc_link_proto_xmit(l_ptr, STATE_MSG,
516 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 l_ptr->fsm_msg_cnt++;
518 }
519 link_set_timer(l_ptr, cont_intv);
520 break;
521 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 l_ptr->state = WORKING_UNKNOWN;
523 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800524 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 l_ptr->fsm_msg_cnt++;
526 link_set_timer(l_ptr, cont_intv / 4);
527 break;
528 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400529 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
530 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100531 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 l_ptr->state = RESET_RESET;
533 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800534 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
535 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 l_ptr->fsm_msg_cnt++;
537 link_set_timer(l_ptr, cont_intv);
538 break;
539 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400540 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 }
542 break;
543 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 switch (event) {
545 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 l_ptr->state = WORKING_WORKING;
548 l_ptr->fsm_msg_cnt = 0;
549 link_set_timer(l_ptr, cont_intv);
550 break;
551 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400552 pr_info("%s<%s>, requested by peer while probing\n",
553 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100554 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 l_ptr->state = RESET_RESET;
556 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800557 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
558 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 l_ptr->fsm_msg_cnt++;
560 link_set_timer(l_ptr, cont_intv);
561 break;
562 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 l_ptr->state = WORKING_WORKING;
565 l_ptr->fsm_msg_cnt = 0;
566 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100567 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800568 tipc_link_proto_xmit(l_ptr, STATE_MSG,
569 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 l_ptr->fsm_msg_cnt++;
571 }
572 link_set_timer(l_ptr, cont_intv);
573 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800574 tipc_link_proto_xmit(l_ptr, STATE_MSG,
575 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 l_ptr->fsm_msg_cnt++;
577 link_set_timer(l_ptr, cont_intv / 4);
578 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400579 pr_warn("%s<%s>, peer not responding\n",
580 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100581 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 l_ptr->state = RESET_UNKNOWN;
583 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800584 tipc_link_proto_xmit(l_ptr, RESET_MSG,
585 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 l_ptr->fsm_msg_cnt++;
587 link_set_timer(l_ptr, cont_intv);
588 }
589 break;
590 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400591 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 }
593 break;
594 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 switch (event) {
596 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 break;
598 case ACTIVATE_MSG:
599 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000600 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 l_ptr->state = WORKING_WORKING;
603 l_ptr->fsm_msg_cnt = 0;
604 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800605 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800607 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800608 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 link_set_timer(l_ptr, cont_intv);
610 break;
611 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 l_ptr->state = RESET_RESET;
613 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800614 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
615 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 l_ptr->fsm_msg_cnt++;
617 link_set_timer(l_ptr, cont_intv);
618 break;
619 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500620 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 /* fall through */
622 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800623 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->fsm_msg_cnt++;
625 link_set_timer(l_ptr, cont_intv);
626 break;
627 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400628 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 }
630 break;
631 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 switch (event) {
633 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 case ACTIVATE_MSG:
635 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000636 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 l_ptr->state = WORKING_WORKING;
639 l_ptr->fsm_msg_cnt = 0;
640 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800641 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800643 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800644 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 link_set_timer(l_ptr, cont_intv);
646 break;
647 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 break;
649 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800650 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
651 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 l_ptr->fsm_msg_cnt++;
653 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 break;
655 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400656 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 }
658 break;
659 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400660 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 }
662}
663
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500664/* tipc_link_cong: determine return value and how to treat the
665 * sent buffer during link congestion.
666 * - For plain, errorless user data messages we keep the buffer and
667 * return -ELINKONG.
668 * - For all other messages we discard the buffer and return -EHOSTUNREACH
669 * - For TIPC internal messages we also reset the link
670 */
671static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
672{
673 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500674 uint imp = tipc_msg_tot_importance(msg);
675 u32 oport = msg_tot_origport(msg);
676
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400677 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500678 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
679 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400680 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500681 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400682 if (unlikely(msg_errcode(msg)))
683 goto drop;
684 if (unlikely(msg_reroute_cnt(msg)))
685 goto drop;
686 if (TIPC_SKB_CB(buf)->wakeup_pending)
687 return -ELINKCONG;
688 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
689 return -ELINKCONG;
690drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500691 kfree_skb_list(buf);
692 return -EHOSTUNREACH;
693}
694
695/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400696 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500697 * @link: link to use
698 * @buf: chain of buffers containing message
699 * Consumes the buffer chain, except when returning -ELINKCONG
700 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
701 * user data messages) or -EHOSTUNREACH (all other messages/senders)
702 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
703 * to act on the return value, since they may need to do more send attempts.
704 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400705int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *buf)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500706{
707 struct tipc_msg *msg = buf_msg(buf);
708 uint psz = msg_size(msg);
709 uint qsz = link->out_queue_size;
710 uint sndlim = link->queue_limit[0];
711 uint imp = tipc_msg_tot_importance(msg);
712 uint mtu = link->max_pkt;
713 uint ack = mod(link->next_in_no - 1);
714 uint seqno = link->next_out_no;
715 uint bc_last_in = link->owner->bclink.last_in;
716 struct tipc_media_addr *addr = &link->media_addr;
717 struct sk_buff *next = buf->next;
718
719 /* Match queue limits against msg importance: */
720 if (unlikely(qsz >= link->queue_limit[imp]))
721 return tipc_link_cong(link, buf);
722
723 /* Has valid packet limit been used ? */
724 if (unlikely(psz > mtu)) {
725 kfree_skb_list(buf);
726 return -EMSGSIZE;
727 }
728
729 /* Prepare each packet for sending, and add to outqueue: */
730 while (buf) {
731 next = buf->next;
732 msg = buf_msg(buf);
733 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
734 msg_set_bcast_ack(msg, bc_last_in);
735
736 if (!link->first_out) {
737 link->first_out = buf;
738 } else if (qsz < sndlim) {
739 link->last_out->next = buf;
740 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
741 link->stats.sent_bundled++;
742 buf = next;
743 next = buf->next;
744 continue;
745 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
746 link->stats.sent_bundled++;
747 link->stats.sent_bundles++;
748 link->last_out->next = buf;
749 if (!link->next_out)
750 link->next_out = buf;
751 } else {
752 link->last_out->next = buf;
753 if (!link->next_out)
754 link->next_out = buf;
755 }
756
757 /* Send packet if possible: */
758 if (likely(++qsz <= sndlim)) {
759 tipc_bearer_send(link->bearer_id, buf, addr);
760 link->next_out = next;
761 link->unacked_window = 0;
762 }
763 seqno++;
764 link->last_out = buf;
765 buf = next;
766 }
767 link->next_out_no = seqno;
768 link->out_queue_size = qsz;
769 return 0;
770}
771
772/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400773 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500774 * @buf: chain of buffers containing message
775 * @dsz: amount of user data to be sent
776 * @dnode: address of destination node
777 * @selector: a number used for deterministic link selection
778 * Consumes the buffer chain, except when returning -ELINKCONG
779 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
780 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400781int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500782{
783 struct tipc_link *link = NULL;
784 struct tipc_node *node;
785 int rc = -EHOSTUNREACH;
786
787 node = tipc_node_find(dnode);
788 if (node) {
789 tipc_node_lock(node);
790 link = node->active_links[selector & 1];
791 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400792 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500793 tipc_node_unlock(node);
794 }
795
796 if (link)
797 return rc;
798
799 if (likely(in_own_node(dnode)))
800 return tipc_sk_rcv(buf);
801
802 kfree_skb_list(buf);
803 return rc;
804}
805
Jon Maloyc64f7a62012-11-16 13:51:31 +0800806/*
Ying Xue247f0f32014-02-18 16:06:46 +0800807 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800808 *
809 * Give a newly added peer node the sequence number where it should
810 * start receiving and acking broadcast packets.
811 *
812 * Called with node locked
813 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400814static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800815{
816 struct sk_buff *buf;
817 struct tipc_msg *msg;
818
819 buf = tipc_buf_acquire(INT_H_SIZE);
820 if (!buf)
821 return;
822
823 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400824 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
825 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400826 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800827}
828
829/*
Ying Xue247f0f32014-02-18 16:06:46 +0800830 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800831 * Receive the sequence number where we should start receiving and
832 * acking broadcast packets from a newly added peer node, and open
833 * up for reception of such packets.
834 *
835 * Called with node locked
836 */
Ying Xue247f0f32014-02-18 16:06:46 +0800837static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800838{
839 struct tipc_msg *msg = buf_msg(buf);
840
841 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
842 n->bclink.recv_permitted = true;
843 kfree_skb(buf);
844}
845
846/*
Per Liden4323add2006-01-18 00:38:21 +0100847 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 */
stephen hemminger98056962014-01-04 13:47:48 -0800849static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850{
851 struct sk_buff *buf = l_ptr->first_out;
852 u32 r_q_size = l_ptr->retransm_queue_size;
853 u32 r_q_head = l_ptr->retransm_queue_head;
854
855 /* Step to position where retransmission failed, if any, */
856 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900858 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -0400860 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861
862 while (buf && less(first, r_q_head)) {
863 first = mod(first + 1);
864 buf = buf->next;
865 }
866 l_ptr->retransm_queue_head = r_q_head = first;
867 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
868 }
869
870 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +0000871 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800874 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800875 l_ptr->retransm_queue_head = mod(++r_q_head);
876 l_ptr->retransm_queue_size = --r_q_size;
877 l_ptr->stats.retransmitted++;
878 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 }
880
881 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 buf = l_ptr->proto_msg_queue;
883 if (buf) {
884 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +0000885 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800886 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800887 l_ptr->unacked_window = 0;
888 kfree_skb(buf);
889 l_ptr->proto_msg_queue = NULL;
890 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 }
892
893 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 buf = l_ptr->next_out;
895 if (buf) {
896 struct tipc_msg *msg = buf_msg(buf);
897 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -0400898 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899
900 if (mod(next - first) < l_ptr->queue_limit[0]) {
901 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900902 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800903 tipc_bearer_send(l_ptr->bearer_id, buf,
904 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800905 if (msg_user(msg) == MSG_BUNDLER)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500906 msg_set_type(msg, BUNDLE_CLOSED);
Ying Xue3c294cb2012-11-15 11:34:45 +0800907 l_ptr->next_out = buf->next;
908 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 }
910 }
Ying Xue3c294cb2012-11-15 11:34:45 +0800911 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912}
913
914/*
915 * push_queue(): push out the unsent messages of a link where
916 * congestion has abated. Node is locked
917 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500918void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919{
920 u32 res;
921
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 do {
Per Liden4323add2006-01-18 00:38:21 +0100923 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700924 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925}
926
Ying Xue3f5a12b2014-05-05 08:56:17 +0800927void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700928{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700929 char addr_string[16];
930 u32 i;
931
Ying Xue3f5a12b2014-05-05 08:56:17 +0800932 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700933
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400934 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800935 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700936
937 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800938 if (node->links[i]) {
939 link_print(node->links[i], "Resetting link\n");
940 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700941 }
942 }
943
Ying Xue3f5a12b2014-05-05 08:56:17 +0800944 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700945}
946
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500947static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400948 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700949{
950 struct tipc_msg *msg = buf_msg(buf);
951
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400952 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700953
954 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700955 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000956 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700957 tipc_link_reset(l_ptr);
958
959 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700960 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700961 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700962 char addr_string[16];
963
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400964 pr_info("Msg seq number: %u, ", msg_seqno(msg));
965 pr_cont("Outstanding acks: %lu\n",
966 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700967
Allan Stephens01d83ed2011-01-18 13:53:16 -0500968 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700969 tipc_node_lock(n_ptr);
970
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000971 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400972 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800973 pr_info("Reception permitted: %d, Acked: %u\n",
974 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400975 n_ptr->bclink.acked);
976 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
977 n_ptr->bclink.last_in,
978 n_ptr->bclink.oos_state,
979 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700980
Allan Stephensd356eeb2006-06-25 23:40:01 -0700981 tipc_node_unlock(n_ptr);
982
Ying Xue3f5a12b2014-05-05 08:56:17 +0800983 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700984 l_ptr->stale_count = 0;
985 }
986}
987
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500988void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +0100989 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990{
991 struct tipc_msg *msg;
992
Allan Stephensd356eeb2006-06-25 23:40:01 -0700993 if (!buf)
994 return;
995
996 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900997
Erik Hugne512137e2013-12-06 10:08:00 -0500998 /* Detect repeated retransmit failures */
999 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1000 if (++l_ptr->stale_count > 100) {
1001 link_retransmit_failure(l_ptr, buf);
1002 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001003 }
1004 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001005 l_ptr->last_retransmitted = msg_seqno(msg);
1006 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001008
Neil Hormanca509102010-03-15 07:58:45 +00001009 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 msg = buf_msg(buf);
1011 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001012 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001013 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001014 buf = buf->next;
1015 retransmits--;
1016 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001018
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1020}
1021
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 * link_insert_deferred_queue - insert deferred messages back into receive chain
1024 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001025static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 struct sk_buff *buf)
1027{
1028 u32 seq_no;
1029
1030 if (l_ptr->oldest_deferred_in == NULL)
1031 return buf;
1032
Allan Stephensf9057302011-10-24 16:03:12 -04001033 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 if (seq_no == mod(l_ptr->next_in_no)) {
1035 l_ptr->newest_deferred_in->next = buf;
1036 buf = l_ptr->oldest_deferred_in;
1037 l_ptr->oldest_deferred_in = NULL;
1038 l_ptr->deferred_inqueue_sz = 0;
1039 }
1040 return buf;
1041}
1042
Allan Stephens85035562008-04-15 19:04:54 -07001043/**
1044 * link_recv_buf_validate - validate basic format of received message
1045 *
1046 * This routine ensures a TIPC message has an acceptable header, and at least
1047 * as much data as the header indicates it should. The routine also ensures
1048 * that the entire message header is stored in the main fragment of the message
1049 * buffer, to simplify future access to message header fields.
1050 *
1051 * Note: Having extra info present in the message header or data areas is OK.
1052 * TIPC will ignore the excess, under the assumption that it is optional info
1053 * introduced by a later release of the protocol.
1054 */
Allan Stephens85035562008-04-15 19:04:54 -07001055static int link_recv_buf_validate(struct sk_buff *buf)
1056{
1057 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001058 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001059 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1060 };
1061
1062 struct tipc_msg *msg;
1063 u32 tipc_hdr[2];
1064 u32 size;
1065 u32 hdr_size;
1066 u32 min_hdr_size;
1067
Erik Hugne64380a02014-02-11 11:38:26 +01001068 /* If this packet comes from the defer queue, the skb has already
1069 * been validated
1070 */
1071 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1072 return 1;
1073
Allan Stephens85035562008-04-15 19:04:54 -07001074 if (unlikely(buf->len < MIN_H_SIZE))
1075 return 0;
1076
1077 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1078 if (msg == NULL)
1079 return 0;
1080
1081 if (unlikely(msg_version(msg) != TIPC_VERSION))
1082 return 0;
1083
1084 size = msg_size(msg);
1085 hdr_size = msg_hdr_sz(msg);
1086 min_hdr_size = msg_isdata(msg) ?
1087 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1088
1089 if (unlikely((hdr_size < min_hdr_size) ||
1090 (size < hdr_size) ||
1091 (buf->len < size) ||
1092 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1093 return 0;
1094
1095 return pskb_may_pull(buf, hdr_size);
1096}
1097
Allan Stephensb02b69c2010-08-17 11:00:07 +00001098/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001099 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001100 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001101 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001102 *
1103 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1104 * structure (i.e. cannot be NULL), but bearer can be inactive.
1105 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001106void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001109 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001110 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 struct sk_buff *crs;
1112 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001113 struct tipc_msg *msg;
1114 u32 seq_no;
1115 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 u32 released = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001119 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001120
1121 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001122 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001123 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124
Allan Stephensfe13dda2008-04-15 19:03:23 -07001125 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001126 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001127 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001128
Allan Stephens85035562008-04-15 19:04:54 -07001129 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001130 msg = buf_msg(buf);
1131
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001133 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001134 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001135 else
Ying Xue247f0f32014-02-18 16:06:46 +08001136 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 continue;
1138 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001139
Allan Stephensed33a9c2011-04-05 15:15:04 -04001140 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001141 if (unlikely(!msg_short(msg) &&
1142 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001143 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001144
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001145 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001146 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001148 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001149 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001150
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001151 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001153 if (unlikely(!l_ptr))
1154 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001155
Allan Stephensb4b56102011-05-27 11:00:51 -04001156 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001157 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001158 msg_user(msg) == LINK_PROTOCOL &&
1159 (msg_type(msg) == RESET_MSG ||
1160 msg_type(msg) == ACTIVATE_MSG) &&
1161 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001162 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001163
Ying Xue10f465c2014-05-05 08:56:11 +08001164 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001165 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001166
Allan Stephens85035562008-04-15 19:04:54 -07001167 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001168 seq_no = msg_seqno(msg);
1169 ackd = msg_ack(msg);
1170
1171 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001172 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001173 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174
1175 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001176 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001177 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 struct sk_buff *next = crs->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001179 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 crs = next;
1181 released++;
1182 }
1183 if (released) {
1184 l_ptr->first_out = crs;
1185 l_ptr->out_queue_size -= released;
1186 }
Allan Stephens85035562008-04-15 19:04:54 -07001187
1188 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001190 tipc_link_push_queue(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001191
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001192 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1193 link_prepare_wakeup(l_ptr);
1194 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1195 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001196
Jon Paul Maloya5377832014-02-13 17:29:15 -05001197 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001198 if (unlikely(!link_working_working(l_ptr))) {
1199 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001200 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001201 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001202 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 continue;
1204 }
Ying Xue3af390e2013-10-30 11:26:57 +08001205
1206 /* Traffic message. Conditionally activate link */
1207 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1208
1209 if (link_working_working(l_ptr)) {
1210 /* Re-insert buffer in front of queue */
1211 buf->next = head;
1212 head = buf;
1213 tipc_node_unlock(n_ptr);
1214 continue;
1215 }
1216 goto unlock_discard;
1217 }
1218
1219 /* Link is now in state WORKING_WORKING */
1220 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 link_handle_out_of_seq_msg(l_ptr, buf);
1222 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001223 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 continue;
1225 }
Ying Xue3af390e2013-10-30 11:26:57 +08001226 l_ptr->next_in_no++;
1227 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001229
Erik Hugne3f53bd82014-07-01 10:22:41 +02001230 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1231 l_ptr->stats.sent_acks++;
1232 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1233 }
1234
Erik Hugne7ae934b2014-07-01 10:22:40 +02001235 if (tipc_link_prepare_input(l_ptr, &buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001236 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 }
Per Liden4323add2006-01-18 00:38:21 +01001239 tipc_node_unlock(n_ptr);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001240 msg = buf_msg(buf);
1241 if (tipc_link_input(l_ptr, buf) != 0)
1242 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001243 continue;
1244unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001245 tipc_node_unlock(n_ptr);
1246discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001247 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249}
1250
Ben Hutchings2c530402012-07-10 10:55:09 +00001251/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001252 * tipc_link_prepare_input - process TIPC link messages
1253 *
1254 * returns nonzero if the message was consumed
1255 *
1256 * Node lock must be held
1257 */
1258static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1259{
1260 struct tipc_node *n;
1261 struct tipc_msg *msg;
1262 int res = -EINVAL;
1263
1264 n = l->owner;
1265 msg = buf_msg(*buf);
1266 switch (msg_user(msg)) {
1267 case CHANGEOVER_PROTOCOL:
1268 if (tipc_link_tunnel_rcv(n, buf))
1269 res = 0;
1270 break;
1271 case MSG_FRAGMENTER:
1272 l->stats.recv_fragments++;
1273 if (tipc_buf_append(&l->reasm_buf, buf)) {
1274 l->stats.recv_fragmented++;
1275 res = 0;
1276 } else if (!l->reasm_buf) {
1277 tipc_link_reset(l);
1278 }
1279 break;
1280 case MSG_BUNDLER:
1281 l->stats.recv_bundles++;
1282 l->stats.recv_bundled += msg_msgcnt(msg);
1283 res = 0;
1284 break;
1285 case NAME_DISTRIBUTOR:
1286 n->bclink.recv_permitted = true;
1287 res = 0;
1288 break;
1289 case BCAST_PROTOCOL:
1290 tipc_link_sync_rcv(n, *buf);
1291 break;
1292 default:
1293 res = 0;
1294 }
1295 return res;
1296}
1297/**
1298 * tipc_link_input - Deliver message too higher layers
1299 */
1300static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1301{
1302 struct tipc_msg *msg = buf_msg(buf);
1303 int res = 0;
1304
1305 switch (msg_user(msg)) {
1306 case TIPC_LOW_IMPORTANCE:
1307 case TIPC_MEDIUM_IMPORTANCE:
1308 case TIPC_HIGH_IMPORTANCE:
1309 case TIPC_CRITICAL_IMPORTANCE:
1310 case CONN_MANAGER:
1311 tipc_sk_rcv(buf);
1312 break;
1313 case NAME_DISTRIBUTOR:
1314 tipc_named_rcv(buf);
1315 break;
1316 case MSG_BUNDLER:
1317 tipc_link_bundle_rcv(buf);
1318 break;
1319 default:
1320 res = -EINVAL;
1321 }
1322 return res;
1323}
1324
1325/**
Allan Stephens8809b252011-10-25 10:44:35 -04001326 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1327 *
1328 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 */
Allan Stephens8809b252011-10-25 10:44:35 -04001330u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001331 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332{
Allan Stephens8809b252011-10-25 10:44:35 -04001333 struct sk_buff *queue_buf;
1334 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001335 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336
1337 buf->next = NULL;
1338
1339 /* Empty queue ? */
1340 if (*head == NULL) {
1341 *head = *tail = buf;
1342 return 1;
1343 }
1344
1345 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001346 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 (*tail)->next = buf;
1348 *tail = buf;
1349 return 1;
1350 }
1351
Allan Stephens8809b252011-10-25 10:44:35 -04001352 /* Locate insertion point in queue, then insert; discard if duplicate */
1353 prev = head;
1354 queue_buf = *head;
1355 for (;;) {
1356 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357
Allan Stephens8809b252011-10-25 10:44:35 -04001358 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001359 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001360 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 }
Allan Stephens8809b252011-10-25 10:44:35 -04001362
1363 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365
Allan Stephens8809b252011-10-25 10:44:35 -04001366 prev = &queue_buf->next;
1367 queue_buf = queue_buf->next;
1368 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369
Allan Stephens8809b252011-10-25 10:44:35 -04001370 buf->next = queue_buf;
1371 *prev = buf;
1372 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373}
1374
Allan Stephens8809b252011-10-25 10:44:35 -04001375/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1377 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001378static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379 struct sk_buff *buf)
1380{
Allan Stephensf9057302011-10-24 16:03:12 -04001381 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382
1383 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001384 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 return;
1386 }
1387
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 l_ptr->checkpoint--;
1390
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 * Discard packet if a duplicate; otherwise add it to deferred queue
1393 * and notify peer of gap as per protocol specification
1394 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395 if (less(seq_no, mod(l_ptr->next_in_no))) {
1396 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001397 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 return;
1399 }
1400
Per Liden4323add2006-01-18 00:38:21 +01001401 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1402 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 l_ptr->deferred_inqueue_sz++;
1404 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001405 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001407 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 } else
1409 l_ptr->stats.duplicates++;
1410}
1411
1412/*
1413 * Send protocol message to the other endpoint.
1414 */
Ying Xue247f0f32014-02-18 16:06:46 +08001415void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1416 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001418 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001420 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001421 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422
Allan Stephens92d2c902011-10-25 11:20:26 -04001423 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001424 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001425 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001426 l_ptr->proto_msg_queue = NULL;
1427 }
1428
Ying Xue77a7e072013-12-10 20:45:44 -08001429 /* Don't send protocol message during link changeover */
1430 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001432
1433 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001434 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001435 return;
1436
Allan Stephens92d2c902011-10-25 11:20:26 -04001437 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001439 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001440 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001441 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442
1443 if (msg_typ == STATE_MSG) {
1444 u32 next_sent = mod(l_ptr->next_out_no);
1445
Per Liden4323add2006-01-18 00:38:21 +01001446 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 return;
1448 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001449 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450 msg_set_next_sent(msg, next_sent);
1451 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001452 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 gap = mod(rec - mod(l_ptr->next_in_no));
1454 }
1455 msg_set_seq_gap(msg, gap);
1456 if (gap)
1457 l_ptr->stats.sent_nacks++;
1458 msg_set_link_tolerance(msg, tolerance);
1459 msg_set_linkprio(msg, priority);
1460 msg_set_max_pkt(msg, ack_mtu);
1461 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1462 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001463 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 u32 mtu = l_ptr->max_pkt;
1465
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001466 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 link_working_working(l_ptr) &&
1468 l_ptr->fsm_msg_cnt) {
1469 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001470 if (l_ptr->max_pkt_probes == 10) {
1471 l_ptr->max_pkt_target = (msg_size - 4);
1472 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001474 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001476 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477
1478 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001479 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 l_ptr->stats.sent_states++;
1481 } else { /* RESET_MSG or ACTIVATE_MSG */
1482 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1483 msg_set_seq_gap(msg, 0);
1484 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001485 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 msg_set_link_tolerance(msg, l_ptr->tolerance);
1487 msg_set_linkprio(msg, l_ptr->priority);
1488 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1489 }
1490
Allan Stephens75f0aa42011-02-28 15:30:20 -05001491 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1492 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001494 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495
1496 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1497
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001498 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499 if (!buf)
1500 return;
1501
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001502 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001503 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504
Ying Xue7a2f7d12014-04-21 10:55:46 +08001505 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001506 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001507 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508}
1509
1510/*
1511 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001512 * Note that network plane id propagates through the network, and may
1513 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 */
Ying Xue247f0f32014-02-18 16:06:46 +08001515static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516{
1517 u32 rec_gap = 0;
1518 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001519 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 u32 msg_tol;
1521 struct tipc_msg *msg = buf_msg(buf);
1522
Ying Xue77a7e072013-12-10 20:45:44 -08001523 /* Discard protocol message during link changeover */
1524 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525 goto exit;
1526
Ying Xue7a2f7d12014-04-21 10:55:46 +08001527 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001529 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001532
Per Lidenb97bf3f2006-01-02 19:04:38 +01001533 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001534 if (!link_working_unknown(l_ptr) &&
1535 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001536 if (less_eq(msg_session(msg), l_ptr->peer_session))
1537 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001539
1540 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1541 link_working_unknown(l_ptr))) {
1542 /*
1543 * peer has lost contact -- don't allow peer's links
1544 * to reactivate before we recognize loss & clean up
1545 */
Ying Xueca9cf062014-05-08 08:54:40 +08001546 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001547 }
1548
Allan Stephens47361c82011-10-26 10:55:16 -04001549 link_state_event(l_ptr, RESET_MSG);
1550
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 /* fall thru' */
1552 case ACTIVATE_MSG:
1553 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1555
Allan Stephens2db99832010-12-31 18:59:33 +00001556 msg_tol = msg_link_tolerance(msg);
1557 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 link_set_supervision_props(l_ptr, msg_tol);
1559
1560 if (msg_linkprio(msg) > l_ptr->priority)
1561 l_ptr->priority = msg_linkprio(msg);
1562
1563 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001564 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 if (max_pkt_info < l_ptr->max_pkt_target)
1566 l_ptr->max_pkt_target = max_pkt_info;
1567 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1568 l_ptr->max_pkt = l_ptr->max_pkt_target;
1569 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001570 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572
Allan Stephens4d753132011-10-25 12:19:05 -04001573 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001574 if (!tipc_node_is_up(l_ptr->owner)) {
1575 l_ptr->owner->bclink.last_sent =
1576 l_ptr->owner->bclink.last_in =
1577 msg_last_bcast(msg);
1578 l_ptr->owner->bclink.oos_state = 0;
1579 }
Allan Stephens4d753132011-10-25 12:19:05 -04001580
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 l_ptr->peer_session = msg_session(msg);
1582 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001583
1584 if (msg_type(msg) == ACTIVATE_MSG)
1585 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 break;
1587 case STATE_MSG:
1588
Allan Stephens2db99832010-12-31 18:59:33 +00001589 msg_tol = msg_link_tolerance(msg);
1590 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001592
1593 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001595 pr_warn("%s<%s>, priority change %u->%u\n",
1596 link_rst_msg, l_ptr->name, l_ptr->priority,
1597 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001599 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 break;
1601 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001602
1603 /* Record reception; force mismatch at next timeout: */
1604 l_ptr->checkpoint--;
1605
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1607 l_ptr->stats.recv_states++;
1608 if (link_reset_unknown(l_ptr))
1609 break;
1610
1611 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001612 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 mod(l_ptr->next_in_no));
1614 }
1615
1616 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001617 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001618 l_ptr->max_pkt = max_pkt_ack;
1619 l_ptr->max_pkt_probes = 0;
1620 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621
1622 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001623 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001625 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001626 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001627 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628
1629 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001630 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001631 tipc_bclink_update_link_state(l_ptr->owner,
1632 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633
1634 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001635 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1636 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 }
1638 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001640 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1641 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 }
1643 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 }
1645exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001646 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647}
1648
1649
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001650/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1651 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001653static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1654 struct tipc_msg *tunnel_hdr,
1655 struct tipc_msg *msg,
1656 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001658 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 struct sk_buff *buf;
1660 u32 length = msg_size(msg);
1661
1662 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001663 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001664 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001666 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001668 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001669 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001670 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001672 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001673 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1674 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001675 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676}
1677
1678
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001679/* tipc_link_failover_send_queue(): A link has gone down, but a second
1680 * link is still active. We can do failover. Tunnel the failing link's
1681 * whole send queue via the remaining link. This way, we don't lose
1682 * any packets, and sequence order is preserved for subsequent traffic
1683 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001685void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686{
1687 u32 msgcount = l_ptr->out_queue_size;
1688 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001689 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001691 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692
1693 if (!tunnel)
1694 return;
1695
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001696 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001697 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1699 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001700
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 if (!l_ptr->first_out) {
1702 struct sk_buff *buf;
1703
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001704 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001706 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001708 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001710 pr_warn("%sunable to send changeover msg\n",
1711 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 }
1713 return;
1714 }
Allan Stephensf1310722006-06-25 23:51:37 -07001715
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001716 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001717 l_ptr->owner->active_links[1]);
1718
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 while (crs) {
1720 struct tipc_msg *msg = buf_msg(crs);
1721
1722 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001724 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725
Florian Westphald788d802007-08-02 19:28:06 -07001726 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001728 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001729 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1730 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731 pos += align(msg_size(m));
1732 m = (struct tipc_msg *)pos;
1733 }
1734 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001735 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1736 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 }
1738 crs = crs->next;
1739 }
1740}
1741
Ying Xue247f0f32014-02-18 16:06:46 +08001742/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001743 * duplicate of the first link's send queue via the new link. This way, we
1744 * are guaranteed that currently queued packets from a socket are delivered
1745 * before future traffic from the same socket, even if this is using the
1746 * new link. The last arriving copy of each duplicate packet is dropped at
1747 * the receiving end by the regular protocol check, so packet cardinality
1748 * and sequence order is preserved per sender/receiver socket pair.
1749 * Owner node is locked.
1750 */
Ying Xue247f0f32014-02-18 16:06:46 +08001751void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001752 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753{
1754 struct sk_buff *iter;
1755 struct tipc_msg tunnel_hdr;
1756
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001757 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001758 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1760 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1761 iter = l_ptr->first_out;
1762 while (iter) {
1763 struct sk_buff *outbuf;
1764 struct tipc_msg *msg = buf_msg(iter);
1765 u32 length = msg_size(msg);
1766
1767 if (msg_user(msg) == MSG_BUNDLER)
1768 msg_set_type(msg, CLOSED_MSG);
1769 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001770 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001772 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001773 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001774 pr_warn("%sunable to send duplicate msg\n",
1775 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 return;
1777 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001778 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1779 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1780 length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001781 __tipc_link_xmit(tunnel, outbuf);
Per Liden4323add2006-01-18 00:38:21 +01001782 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 return;
1784 iter = iter->next;
1785 }
1786}
1787
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788/**
1789 * buf_extract - extracts embedded TIPC message from another message
1790 * @skb: encapsulating message buffer
1791 * @from_pos: offset to extract from
1792 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001793 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 * encapsulating message itself is left unchanged.
1795 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1797{
1798 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1799 u32 size = msg_size(msg);
1800 struct sk_buff *eb;
1801
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001802 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001804 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 return eb;
1806}
1807
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001808
1809
1810/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1811 * Owner node is locked.
1812 */
1813static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1814 struct sk_buff *t_buf)
1815{
1816 struct sk_buff *buf;
1817
1818 if (!tipc_link_is_up(l_ptr))
1819 return;
1820
1821 buf = buf_extract(t_buf, INT_H_SIZE);
1822 if (buf == NULL) {
1823 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1824 return;
1825 }
1826
1827 /* Add buffer to deferred queue, if applicable: */
1828 link_handle_out_of_seq_msg(l_ptr, buf);
1829}
1830
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001831/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1832 * Owner node is locked.
1833 */
1834static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1835 struct sk_buff *t_buf)
1836{
1837 struct tipc_msg *t_msg = buf_msg(t_buf);
1838 struct sk_buff *buf = NULL;
1839 struct tipc_msg *msg;
1840
1841 if (tipc_link_is_up(l_ptr))
1842 tipc_link_reset(l_ptr);
1843
1844 /* First failover packet? */
1845 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1846 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1847
1848 /* Should there be an inner packet? */
1849 if (l_ptr->exp_msg_count) {
1850 l_ptr->exp_msg_count--;
1851 buf = buf_extract(t_buf, INT_H_SIZE);
1852 if (buf == NULL) {
1853 pr_warn("%sno inner failover pkt\n", link_co_err);
1854 goto exit;
1855 }
1856 msg = buf_msg(buf);
1857
1858 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1859 kfree_skb(buf);
1860 buf = NULL;
1861 goto exit;
1862 }
1863 if (msg_user(msg) == MSG_FRAGMENTER) {
1864 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001865 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001866 }
1867 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001868exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001869 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1870 tipc_node_detach_link(l_ptr->owner, l_ptr);
1871 kfree(l_ptr);
1872 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001873 return buf;
1874}
1875
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001876/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001877 * via other link as result of a failover (ORIGINAL_MSG) or
1878 * a new active link (DUPLICATE_MSG). Failover packets are
1879 * returned to the active link for delivery upwards.
1880 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001882static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001883 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001885 struct sk_buff *t_buf = *buf;
1886 struct tipc_link *l_ptr;
1887 struct tipc_msg *t_msg = buf_msg(t_buf);
1888 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001890 *buf = NULL;
1891
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001892 if (bearer_id >= MAX_BEARERS)
1893 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001894
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001895 l_ptr = n_ptr->links[bearer_id];
1896 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001899 if (msg_type(t_msg) == DUPLICATE_MSG)
1900 tipc_link_dup_rcv(l_ptr, t_buf);
1901 else if (msg_type(t_msg) == ORIGINAL_MSG)
1902 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001903 else
1904 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001906 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001907 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001908}
1909
1910/*
1911 * Bundler functionality:
1912 */
Ying Xue247f0f32014-02-18 16:06:46 +08001913void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914{
1915 u32 msgcount = msg_msgcnt(buf_msg(buf));
1916 u32 pos = INT_H_SIZE;
1917 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001918 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 while (msgcount--) {
1921 obuf = buf_extract(buf, pos);
1922 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001923 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001924 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001925 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001926 omsg = buf_msg(obuf);
1927 pos += align(msg_size(omsg));
1928 if (msg_isdata(omsg) || (msg_user(omsg) == CONN_MANAGER)) {
1929 tipc_sk_rcv(obuf);
1930 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1931 tipc_named_rcv(obuf);
1932 } else {
1933 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1934 kfree_skb(obuf);
1935 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001937 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001938}
1939
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001940static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001942 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1943 return;
1944
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945 l_ptr->tolerance = tolerance;
1946 l_ptr->continuity_interval =
1947 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1948 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1949}
1950
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001951void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952{
1953 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001954 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1955 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1956 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1957 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001959 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1960 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1961 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1962 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1965 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1966 /* FRAGMENT and LAST_FRAGMENT packets */
1967 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1968}
1969
Jon Paul Maloye099e862014-02-13 17:29:18 -05001970/* tipc_link_find_owner - locate owner node of link by link's name
1971 * @name: pointer to link name string
1972 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001973 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001974 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001976static struct tipc_node *tipc_link_find_owner(const char *link_name,
1977 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001979 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001980 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001981 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001982 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983
Jon Paul Maloye099e862014-02-13 17:29:18 -05001984 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001985 rcu_read_lock();
1986 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001987 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001988 for (i = 0; i < MAX_BEARERS; i++) {
1989 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001990 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1991 *bearer_id = i;
1992 found_node = n_ptr;
1993 break;
1994 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001995 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001996 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001997 if (found_node)
1998 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001999 }
Ying Xue6c7a7622014-03-27 12:54:37 +08002000 rcu_read_unlock();
2001
Jon Paul Maloye099e862014-02-13 17:29:18 -05002002 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002003}
2004
Allan Stephens5c216e12011-10-18 11:34:29 -04002005/**
2006 * link_value_is_valid -- validate proposed link tolerance/priority/window
2007 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002008 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2009 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002010 *
2011 * Returns 1 if value is within range, 0 if not.
2012 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002013static int link_value_is_valid(u16 cmd, u32 new_value)
2014{
2015 switch (cmd) {
2016 case TIPC_CMD_SET_LINK_TOL:
2017 return (new_value >= TIPC_MIN_LINK_TOL) &&
2018 (new_value <= TIPC_MAX_LINK_TOL);
2019 case TIPC_CMD_SET_LINK_PRI:
2020 return (new_value <= TIPC_MAX_LINK_PRI);
2021 case TIPC_CMD_SET_LINK_WINDOW:
2022 return (new_value >= TIPC_MIN_LINK_WIN) &&
2023 (new_value <= TIPC_MAX_LINK_WIN);
2024 }
2025 return 0;
2026}
2027
Allan Stephens5c216e12011-10-18 11:34:29 -04002028/**
2029 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002030 * @name: ptr to link, bearer, or media name
2031 * @new_value: new value of link, bearer, or media setting
2032 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002033 *
Ying Xue7216cd92014-04-21 10:55:48 +08002034 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002035 *
2036 * Returns 0 if value updated and negative value on error.
2037 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002038static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2039{
2040 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002041 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002042 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002043 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002044 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002045 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002046
Jon Paul Maloye099e862014-02-13 17:29:18 -05002047 node = tipc_link_find_owner(name, &bearer_id);
2048 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002049 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002050 l_ptr = node->links[bearer_id];
2051
2052 if (l_ptr) {
2053 switch (cmd) {
2054 case TIPC_CMD_SET_LINK_TOL:
2055 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002056 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2057 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002058 break;
2059 case TIPC_CMD_SET_LINK_PRI:
2060 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002061 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2062 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002063 break;
2064 case TIPC_CMD_SET_LINK_WINDOW:
2065 tipc_link_set_queue_limits(l_ptr, new_value);
2066 break;
2067 default:
2068 res = -EINVAL;
2069 break;
2070 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002071 }
2072 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002073 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002074 }
2075
2076 b_ptr = tipc_bearer_find(name);
2077 if (b_ptr) {
2078 switch (cmd) {
2079 case TIPC_CMD_SET_LINK_TOL:
2080 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002081 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002082 case TIPC_CMD_SET_LINK_PRI:
2083 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002084 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002085 case TIPC_CMD_SET_LINK_WINDOW:
2086 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002087 break;
2088 default:
2089 res = -EINVAL;
2090 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002091 }
Ying Xue636c0372013-10-18 07:23:20 +02002092 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002093 }
2094
2095 m_ptr = tipc_media_find(name);
2096 if (!m_ptr)
2097 return -ENODEV;
2098 switch (cmd) {
2099 case TIPC_CMD_SET_LINK_TOL:
2100 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002101 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002102 case TIPC_CMD_SET_LINK_PRI:
2103 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002104 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002105 case TIPC_CMD_SET_LINK_WINDOW:
2106 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002107 break;
2108 default:
2109 res = -EINVAL;
2110 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002111 }
Ying Xue636c0372013-10-18 07:23:20 +02002112 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002113}
2114
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002115struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002116 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117{
2118 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002119 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002120 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121
2122 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002123 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124
2125 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2126 new_value = ntohl(args->value);
2127
Allan Stephens5c216e12011-10-18 11:34:29 -04002128 if (!link_value_is_valid(cmd, new_value))
2129 return tipc_cfg_reply_error_string(
2130 "cannot change, value invalid");
2131
Per Liden4323add2006-01-18 00:38:21 +01002132 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002133 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002134 (tipc_bclink_set_queue_limits(new_value) == 0))
2135 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002136 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002137 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138 }
2139
Allan Stephens5c216e12011-10-18 11:34:29 -04002140 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002142 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143
Per Liden4323add2006-01-18 00:38:21 +01002144 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002145}
2146
2147/**
2148 * link_reset_statistics - reset link statistics
2149 * @l_ptr: pointer to link
2150 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002151static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152{
2153 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2154 l_ptr->stats.sent_info = l_ptr->next_out_no;
2155 l_ptr->stats.recv_info = l_ptr->next_in_no;
2156}
2157
Per Liden4323add2006-01-18 00:38:21 +01002158struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159{
2160 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002161 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002162 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002163 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164
2165 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002166 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167
2168 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002169 if (!strcmp(link_name, tipc_bclink_name)) {
2170 if (tipc_bclink_reset_stats())
2171 return tipc_cfg_reply_error_string("link not found");
2172 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002174 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002175 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002176 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002177
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002178 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002179 l_ptr = node->links[bearer_id];
2180 if (!l_ptr) {
2181 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002182 return tipc_cfg_reply_error_string("link not found");
2183 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002185 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002186 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002187}
2188
2189/**
2190 * percent - convert count to a percentage of total (rounding up or down)
2191 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192static u32 percent(u32 count, u32 total)
2193{
2194 return (count * 100 + (total / 2)) / total;
2195}
2196
2197/**
Per Liden4323add2006-01-18 00:38:21 +01002198 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002199 * @name: link name
2200 * @buf: print buffer area
2201 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002202 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203 * Returns length of print buffer data string (or 0 if error)
2204 */
Per Liden4323add2006-01-18 00:38:21 +01002205static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002207 struct tipc_link *l;
2208 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002209 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210 char *status;
2211 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002212 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002213 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214
Per Liden4323add2006-01-18 00:38:21 +01002215 if (!strcmp(name, tipc_bclink_name))
2216 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217
Jon Paul Maloye099e862014-02-13 17:29:18 -05002218 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002219 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002220 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002221
Per Liden4323add2006-01-18 00:38:21 +01002222 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002223
2224 l = node->links[bearer_id];
2225 if (!l) {
2226 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002227 return 0;
2228 }
2229
Erik Hugnedc1aed32012-06-29 00:50:23 -04002230 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231
Erik Hugnedc1aed32012-06-29 00:50:23 -04002232 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002233 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002234 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002235 status = "STANDBY";
2236 else
2237 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002238
2239 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2240 " %s MTU:%u Priority:%u Tolerance:%u ms"
2241 " Window:%u packets\n",
2242 l->name, status, l->max_pkt, l->priority,
2243 l->tolerance, l->queue_limit[0]);
2244
2245 ret += tipc_snprintf(buf + ret, buf_size - ret,
2246 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2247 l->next_in_no - s->recv_info, s->recv_fragments,
2248 s->recv_fragmented, s->recv_bundles,
2249 s->recv_bundled);
2250
2251 ret += tipc_snprintf(buf + ret, buf_size - ret,
2252 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2253 l->next_out_no - s->sent_info, s->sent_fragments,
2254 s->sent_fragmented, s->sent_bundles,
2255 s->sent_bundled);
2256
2257 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002258 if (!profile_total)
2259 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002260
2261 ret += tipc_snprintf(buf + ret, buf_size - ret,
2262 " TX profile sample:%u packets average:%u octets\n"
2263 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2264 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2265 s->msg_length_counts,
2266 s->msg_lengths_total / profile_total,
2267 percent(s->msg_length_profile[0], profile_total),
2268 percent(s->msg_length_profile[1], profile_total),
2269 percent(s->msg_length_profile[2], profile_total),
2270 percent(s->msg_length_profile[3], profile_total),
2271 percent(s->msg_length_profile[4], profile_total),
2272 percent(s->msg_length_profile[5], profile_total),
2273 percent(s->msg_length_profile[6], profile_total));
2274
2275 ret += tipc_snprintf(buf + ret, buf_size - ret,
2276 " RX states:%u probes:%u naks:%u defs:%u"
2277 " dups:%u\n", s->recv_states, s->recv_probes,
2278 s->recv_nacks, s->deferred_recv, s->duplicates);
2279
2280 ret += tipc_snprintf(buf + ret, buf_size - ret,
2281 " TX states:%u probes:%u naks:%u acks:%u"
2282 " dups:%u\n", s->sent_states, s->sent_probes,
2283 s->sent_nacks, s->sent_acks, s->retransmitted);
2284
2285 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002286 " Congestion link:%u Send queue"
2287 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002288 s->max_queue_sz, s->queue_sz_counts ?
2289 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290
Per Liden4323add2006-01-18 00:38:21 +01002291 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002292 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002293}
2294
Per Liden4323add2006-01-18 00:38:21 +01002295struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002296{
2297 struct sk_buff *buf;
2298 struct tlv_desc *rep_tlv;
2299 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002300 int pb_len;
2301 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302
2303 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002304 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002305
Erik Hugnedc1aed32012-06-29 00:50:23 -04002306 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307 if (!buf)
2308 return NULL;
2309
2310 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002311 pb = TLV_DATA(rep_tlv);
2312 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002313 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002314 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002316 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002317 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002318 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002319 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002320 skb_put(buf, TLV_SPACE(str_len));
2321 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2322
2323 return buf;
2324}
2325
Per Lidenb97bf3f2006-01-02 19:04:38 +01002326/**
Per Liden4323add2006-01-18 00:38:21 +01002327 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002328 * @dest: network address of destination node
2329 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002330 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002331 * If no active link can be found, uses default maximum packet size.
2332 */
Per Liden4323add2006-01-18 00:38:21 +01002333u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002334{
David S. Miller6c000552008-09-02 23:38:32 -07002335 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002336 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002338
Per Lidenb97bf3f2006-01-02 19:04:38 +01002339 if (dest == tipc_own_addr)
2340 return MAX_MSG_SIZE;
2341
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002342 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002343 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002344 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002345 l_ptr = n_ptr->active_links[selector & 1];
2346 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002347 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002348 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002349 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 return res;
2351}
2352
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002353static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002354{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002355 struct tipc_bearer *b_ptr;
2356
2357 rcu_read_lock();
2358 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2359 if (b_ptr)
2360 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2361 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002362
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002364 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002365 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002366 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002367 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002368 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002369 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002370 pr_cont(":WW\n");
2371 else
2372 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002373}