blob: fd340ad742ea375bc769362e3d782454a8abe277 [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 Maloyc64f7a62012-11-16 13:51:31 +08004 * Copyright (c) 1996-2007, 2012, 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "discover.h"
42#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
Ying Xue796c75d2013-06-17 10:54:48 -040044#include <linux/pkt_sched.h>
45
Erik Hugne2cf8aa12012-06-29 00:16:37 -040046/*
47 * Error message prefixes
48 */
49static const char *link_co_err = "Link changeover error, ";
50static const char *link_rst_msg = "Resetting link ";
51static const char *link_unk_evt = "Unknown link event ";
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090053/*
Allan Stephensa686e682008-06-04 17:29:39 -070054 * Out-of-range value for link session numbers
55 */
Allan Stephensa686e682008-06-04 17:29:39 -070056#define INVALID_SESSION 0x10000
57
58/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090059 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010060 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010061#define STARTING_EVT 856384768 /* link processing trigger */
62#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
63#define TIMEOUT_EVT 560817u /* link timer expired */
64
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090065/*
66 * The following two 'message types' is really just implementation
67 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010068 * They must not be considered part of the protocol
69 */
70#define OPEN_MSG 0
71#define CLOSED_MSG 1
72
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090073/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010074 * State value stored in 'exp_msg_count'
75 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010076#define START_CHANGEOVER 100000u
77
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050078static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +010079 struct sk_buff *buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050080static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf);
81static int link_recv_changeover_msg(struct tipc_link **l_ptr,
82 struct sk_buff **buf);
83static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050084static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +020086 unsigned int len, u32 destnode);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050087static void link_state_event(struct tipc_link *l_ptr, u32 event);
88static void link_reset_statistics(struct tipc_link *l_ptr);
89static void link_print(struct tipc_link *l_ptr, const char *str);
90static void link_start(struct tipc_link *l_ptr);
91static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +080092static void tipc_link_send_sync(struct tipc_link *l);
93static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +000094
Per Lidenb97bf3f2006-01-02 19:04:38 +010095/*
Sam Ravnborg05790c62006-03-20 22:37:04 -080096 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080098static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +010099{
100 return (i + 3) & ~3u;
101}
102
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500103static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104{
105 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900106
Allan Stephens2d627b92011-01-07 13:00:11 -0500107 max_pkt = (l_ptr->b_ptr->mtu & ~3);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108 if (max_pkt > MAX_MSG_SIZE)
109 max_pkt = MAX_MSG_SIZE;
110
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900111 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
113 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900114 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 l_ptr->max_pkt = MAX_PKT_DEFAULT;
116
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900117 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118}
119
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500120static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121{
122 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400123 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 return mod(l_ptr->next_out_no);
125}
126
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500127static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128{
129 return mod(link_next_sent(l_ptr) - 1);
130}
131
132/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800133 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500135int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136{
137 if (!l_ptr)
138 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000139 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140}
141
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500142int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000144 return (l_ptr->owner->active_links[0] == l_ptr) ||
145 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146}
147
148/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 * link_timeout - handle expiration of link timer
150 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900151 *
Per Liden4323add2006-01-18 00:38:21 +0100152 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
153 * with tipc_link_delete(). (There is no risk that the node will be deleted by
154 * another thread because tipc_link_delete() always cancels the link timer before
155 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500157static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158{
Per Liden4323add2006-01-18 00:38:21 +0100159 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160
161 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
163 l_ptr->stats.queue_sz_counts++;
164
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 if (l_ptr->first_out) {
166 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
167 u32 length = msg_size(msg);
168
Joe Perchesf64f9e72009-11-29 16:55:45 -0800169 if ((msg_user(msg) == MSG_FRAGMENTER) &&
170 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 length = msg_size(msg_get_wrapped(msg));
172 }
173 if (length) {
174 l_ptr->stats.msg_lengths_total += length;
175 l_ptr->stats.msg_length_counts++;
176 if (length <= 64)
177 l_ptr->stats.msg_length_profile[0]++;
178 else if (length <= 256)
179 l_ptr->stats.msg_length_profile[1]++;
180 else if (length <= 1024)
181 l_ptr->stats.msg_length_profile[2]++;
182 else if (length <= 4096)
183 l_ptr->stats.msg_length_profile[3]++;
184 else if (length <= 16384)
185 l_ptr->stats.msg_length_profile[4]++;
186 else if (length <= 32768)
187 l_ptr->stats.msg_length_profile[5]++;
188 else
189 l_ptr->stats.msg_length_profile[6]++;
190 }
191 }
192
193 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194
195 link_state_event(l_ptr, TIMEOUT_EVT);
196
197 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100198 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199
Per Liden4323add2006-01-18 00:38:21 +0100200 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201}
202
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500203static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204{
205 k_start_timer(&l_ptr->timer, time);
206}
207
208/**
Per Liden4323add2006-01-18 00:38:21 +0100209 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500210 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900213 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 * Returns pointer to link.
215 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500216struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Allan Stephens37b9c082011-02-28 11:32:27 -0500217 struct tipc_bearer *b_ptr,
Per Liden4323add2006-01-18 00:38:21 +0100218 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500220 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 struct tipc_msg *msg;
222 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500223 char addr_string[16];
224 u32 peer = n_ptr->addr;
225
226 if (n_ptr->link_cnt >= 2) {
227 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400228 pr_err("Attempt to establish third link to %s\n", addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500229 return NULL;
230 }
231
232 if (n_ptr->links[b_ptr->identity]) {
233 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400234 pr_err("Attempt to establish second link on <%s> to %s\n",
235 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500236 return NULL;
237 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700239 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400241 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 return NULL;
243 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
245 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500246 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400247 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900249 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 if_name,
251 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400252 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500254 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 l_ptr->checkpoint = 1;
Allan Stephensf882cb72011-04-07 09:43:27 -0400256 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 l_ptr->b_ptr = b_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -0400258 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 l_ptr->state = RESET_UNKNOWN;
260
261 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
262 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000263 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700265 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 msg_set_bearer_id(msg, b_ptr->identity);
267 strcpy((char *)msg_data(msg), if_name);
268
269 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400270 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271
272 link_init_max_pkt(l_ptr);
273
274 l_ptr->next_out_no = 1;
275 INIT_LIST_HEAD(&l_ptr->waiting_ports);
276
277 link_reset_statistics(l_ptr);
278
Allan Stephens37b9c082011-02-28 11:32:27 -0500279 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280
Florian Westphal945710652007-07-26 00:05:07 -0700281 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
282 list_add_tail(&l_ptr->link_list, &b_ptr->links);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000283 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 return l_ptr;
286}
287
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900288/**
Per Liden4323add2006-01-18 00:38:21 +0100289 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900291 *
Per Liden4323add2006-01-18 00:38:21 +0100292 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900294 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500296void tipc_link_delete(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297{
298 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400299 pr_err("Attempt to delete non-existent link\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 return;
301 }
302
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900304
Per Liden4323add2006-01-18 00:38:21 +0100305 tipc_node_lock(l_ptr->owner);
306 tipc_link_reset(l_ptr);
307 tipc_node_detach_link(l_ptr->owner, l_ptr);
308 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 list_del_init(&l_ptr->link_list);
Per Liden4323add2006-01-18 00:38:21 +0100310 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 k_term_timer(&l_ptr->timer);
312 kfree(l_ptr);
313}
314
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500315static void link_start(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316{
Allan Stephens214dda42011-01-24 16:22:43 -0500317 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 link_state_event(l_ptr, STARTING_EVT);
Allan Stephens214dda42011-01-24 16:22:43 -0500319 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320}
321
322/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900323 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 * @l_ptr: pointer to link
325 * @origport: reference to sending port
326 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900327 *
328 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 * has abated.
330 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500331static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500333 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
Per Liden4323add2006-01-18 00:38:21 +0100335 spin_lock_bh(&tipc_port_list_lock);
336 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 if (p_ptr) {
338 if (!p_ptr->wakeup)
339 goto exit;
340 if (!list_empty(&p_ptr->wait_list))
341 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500342 p_ptr->congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000343 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
345 l_ptr->stats.link_congs++;
346exit:
Per Liden4323add2006-01-18 00:38:21 +0100347 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 }
Per Liden4323add2006-01-18 00:38:21 +0100349 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 return -ELINKCONG;
351}
352
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500353void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500355 struct tipc_port *p_ptr;
356 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
358
359 if (all)
360 win = 100000;
361 if (win <= 0)
362 return;
Per Liden4323add2006-01-18 00:38:21 +0100363 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 return;
365 if (link_congested(l_ptr))
366 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900367 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 wait_list) {
369 if (win <= 0)
370 break;
371 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500372 spin_lock_bh(p_ptr->lock);
373 p_ptr->congested = 0;
374 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500376 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 }
378
379exit:
Per Liden4323add2006-01-18 00:38:21 +0100380 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381}
382
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900383/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 * link_release_outqueue - purge link's outbound message queue
385 * @l_ptr: pointer to link
386 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500387static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388{
389 struct sk_buff *buf = l_ptr->first_out;
390 struct sk_buff *next;
391
392 while (buf) {
393 next = buf->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -0400394 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 buf = next;
396 }
397 l_ptr->first_out = NULL;
398 l_ptr->out_queue_size = 0;
399}
400
401/**
Per Liden4323add2006-01-18 00:38:21 +0100402 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 * @l_ptr: pointer to link
404 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500405void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406{
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100407 kfree_skb(l_ptr->reasm_head);
408 l_ptr->reasm_head = NULL;
409 l_ptr->reasm_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410}
411
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412/**
Per Liden4323add2006-01-18 00:38:21 +0100413 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 * @l_ptr: pointer to link
415 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500416void tipc_link_stop(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417{
418 struct sk_buff *buf;
419 struct sk_buff *next;
420
421 buf = l_ptr->oldest_deferred_in;
422 while (buf) {
423 next = buf->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -0400424 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 buf = next;
426 }
427
428 buf = l_ptr->first_out;
429 while (buf) {
430 next = buf->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -0400431 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 buf = next;
433 }
434
Per Liden4323add2006-01-18 00:38:21 +0100435 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436
Allan Stephens5f6d9122011-11-04 13:24:29 -0400437 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 l_ptr->proto_msg_queue = NULL;
439}
440
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500441void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442{
443 struct sk_buff *buf;
444 u32 prev_state = l_ptr->state;
445 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700446 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900447
Allan Stephensa686e682008-06-04 17:29:39 -0700448 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449
Allan Stephensa686e682008-06-04 17:29:39 -0700450 /* Link is down, accept any session */
451 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900453 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900455
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457
458 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
459 return;
460
Per Liden4323add2006-01-18 00:38:21 +0100461 tipc_node_link_down(l_ptr->owner, l_ptr);
462 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000463
Paul Gortmaker8f19afb2011-02-28 11:36:21 -0400464 if (was_active_link && tipc_node_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 l_ptr->owner->permit_changeover) {
466 l_ptr->reset_checkpoint = checkpoint;
467 l_ptr->exp_msg_count = START_CHANGEOVER;
468 }
469
470 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400472 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 l_ptr->proto_msg_queue = NULL;
474 buf = l_ptr->oldest_deferred_in;
475 while (buf) {
476 struct sk_buff *next = buf->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -0400477 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478 buf = next;
479 }
480 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100481 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482
483 l_ptr->retransm_queue_head = 0;
484 l_ptr->retransm_queue_size = 0;
485 l_ptr->last_out = NULL;
486 l_ptr->first_out = NULL;
487 l_ptr->next_out = NULL;
488 l_ptr->unacked_window = 0;
489 l_ptr->checkpoint = 1;
490 l_ptr->next_out_no = 1;
491 l_ptr->deferred_inqueue_sz = 0;
492 l_ptr->oldest_deferred_in = NULL;
493 l_ptr->newest_deferred_in = NULL;
494 l_ptr->fsm_msg_cnt = 0;
495 l_ptr->stale_count = 0;
496 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497}
498
499
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500500static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501{
Allan Stephens5392d642006-06-25 23:52:50 -0700502 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100503 tipc_node_link_up(l_ptr->owner, l_ptr);
504 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505}
506
507/**
508 * link_state_event - link finite state machine
509 * @l_ptr: pointer to link
510 * @event: state machine event to process
511 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000512static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500514 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 u32 cont_intv = l_ptr->continuity_interval;
516
517 if (!l_ptr->started && (event != STARTING_EVT))
518 return; /* Not yet. */
519
520 if (link_blocked(l_ptr)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000521 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 return; /* Changeover going on */
524 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525
526 switch (l_ptr->state) {
527 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 switch (event) {
529 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 break;
532 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 if (l_ptr->next_in_no != l_ptr->checkpoint) {
534 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100535 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100537 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 l_ptr->fsm_msg_cnt++;
539 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900540 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100541 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 l_ptr->fsm_msg_cnt++;
543 }
544 link_set_timer(l_ptr, cont_intv);
545 break;
546 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 l_ptr->state = WORKING_UNKNOWN;
548 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100549 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 l_ptr->fsm_msg_cnt++;
551 link_set_timer(l_ptr, cont_intv / 4);
552 break;
553 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400554 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
555 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100556 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 l_ptr->state = RESET_RESET;
558 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100559 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 l_ptr->fsm_msg_cnt++;
561 link_set_timer(l_ptr, cont_intv);
562 break;
563 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400564 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 }
566 break;
567 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 switch (event) {
569 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 l_ptr->state = WORKING_WORKING;
572 l_ptr->fsm_msg_cnt = 0;
573 link_set_timer(l_ptr, cont_intv);
574 break;
575 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400576 pr_info("%s<%s>, requested by peer while probing\n",
577 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100578 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 l_ptr->state = RESET_RESET;
580 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100581 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 l_ptr->fsm_msg_cnt++;
583 link_set_timer(l_ptr, cont_intv);
584 break;
585 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 l_ptr->state = WORKING_WORKING;
588 l_ptr->fsm_msg_cnt = 0;
589 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100590 if (tipc_bclink_acks_missing(l_ptr->owner)) {
591 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
592 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 l_ptr->fsm_msg_cnt++;
594 }
595 link_set_timer(l_ptr, cont_intv);
596 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100598 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 l_ptr->fsm_msg_cnt++;
600 link_set_timer(l_ptr, cont_intv / 4);
601 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400602 pr_warn("%s<%s>, peer not responding\n",
603 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100604 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 l_ptr->state = RESET_UNKNOWN;
606 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100607 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
608 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 l_ptr->fsm_msg_cnt++;
610 link_set_timer(l_ptr, cont_intv);
611 }
612 break;
613 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400614 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 }
616 break;
617 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 switch (event) {
619 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 break;
621 case ACTIVATE_MSG:
622 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000623 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 l_ptr->state = WORKING_WORKING;
626 l_ptr->fsm_msg_cnt = 0;
627 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100628 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800630 if (l_ptr->owner->working_links == 1)
631 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 link_set_timer(l_ptr, cont_intv);
633 break;
634 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 l_ptr->state = RESET_RESET;
636 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100637 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 l_ptr->fsm_msg_cnt++;
639 link_set_timer(l_ptr, cont_intv);
640 break;
641 case STARTING_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 l_ptr->started = 1;
643 /* fall through */
644 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100645 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 l_ptr->fsm_msg_cnt++;
647 link_set_timer(l_ptr, cont_intv);
648 break;
649 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400650 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 }
652 break;
653 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 switch (event) {
655 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 case ACTIVATE_MSG:
657 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000658 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 l_ptr->state = WORKING_WORKING;
661 l_ptr->fsm_msg_cnt = 0;
662 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100663 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800665 if (l_ptr->owner->working_links == 1)
666 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 link_set_timer(l_ptr, cont_intv);
668 break;
669 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 break;
671 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100672 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 l_ptr->fsm_msg_cnt++;
674 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 break;
676 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400677 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 }
679 break;
680 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400681 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 }
683}
684
685/*
686 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900687 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400689static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 struct sk_buff *buf)
691{
692 struct tipc_msg *bundler_msg = buf_msg(bundler);
693 struct tipc_msg *msg = buf_msg(buf);
694 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700695 u32 bundle_size = msg_size(bundler_msg);
696 u32 to_pos = align(bundle_size);
697 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698
699 if (msg_user(bundler_msg) != MSG_BUNDLER)
700 return 0;
701 if (msg_type(bundler_msg) != OPEN_MSG)
702 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700703 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000705 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700706 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707
Allan Stephense49060c2006-06-29 12:32:46 -0700708 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300709 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 msg_set_size(bundler_msg, to_pos + size);
711 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400712 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 l_ptr->stats.sent_bundled++;
714 return 1;
715}
716
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500717static void link_add_to_outqueue(struct tipc_link *l_ptr,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800718 struct sk_buff *buf,
719 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720{
721 u32 ack = mod(l_ptr->next_in_no - 1);
722 u32 seqno = mod(l_ptr->next_out_no++);
723
724 msg_set_word(msg, 2, ((ack << 16) | seqno));
725 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
726 buf->next = NULL;
727 if (l_ptr->first_out) {
728 l_ptr->last_out->next = buf;
729 l_ptr->last_out = buf;
730 } else
731 l_ptr->first_out = l_ptr->last_out = buf;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500732
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 l_ptr->out_queue_size++;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500734 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
735 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736}
737
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500738static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
Allan Stephensdc63d912011-04-21 11:50:42 -0400739 struct sk_buff *buf_chain,
740 u32 long_msgno)
741{
742 struct sk_buff *buf;
743 struct tipc_msg *msg;
744
745 if (!l_ptr->next_out)
746 l_ptr->next_out = buf_chain;
747 while (buf_chain) {
748 buf = buf_chain;
749 buf_chain = buf_chain->next;
750
751 msg = buf_msg(buf);
752 msg_set_long_msgno(msg, long_msgno);
753 link_add_to_outqueue(l_ptr, buf, msg);
754 }
755}
756
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900757/*
758 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 * inside TIPC when the 'fast path' in tipc_send_buf
760 * has failed, and from link_send()
761 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500762int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763{
764 struct tipc_msg *msg = buf_msg(buf);
765 u32 size = msg_size(msg);
766 u32 dsz = msg_data_sz(msg);
767 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000768 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000770 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 /* Match msg importance against queue limits: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 if (unlikely(queue_size >= queue_limit)) {
774 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
Allan Stephensbebc55a2011-04-19 10:17:58 -0400775 link_schedule_port(l_ptr, msg_origport(msg), size);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400776 kfree_skb(buf);
Allan Stephensbebc55a2011-04-19 10:17:58 -0400777 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 }
Allan Stephens5f6d9122011-11-04 13:24:29 -0400779 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 if (imp > CONN_MANAGER) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400781 pr_warn("%s<%s>, send queue full", link_rst_msg,
782 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100783 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 }
785 return dsz;
786 }
787
788 /* Fragmentation needed ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000790 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400792 /* Packet can be queued or sent. */
Erik Hugne512137e2013-12-06 10:08:00 -0500793 if (likely(!link_congested(l_ptr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 link_add_to_outqueue(l_ptr, buf, msg);
795
Ying Xue3c294cb2012-11-15 11:34:45 +0800796 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
797 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 return dsz;
799 }
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400800 /* Congestion: can message be bundled ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
802 (msg_user(msg) != MSG_FRAGMENTER)) {
803
804 /* Try adding message to an existing bundle */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 if (l_ptr->next_out &&
Ying Xue3c294cb2012-11-15 11:34:45 +0800806 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 return dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808
809 /* Try creating a new bundle */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000811 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 struct tipc_msg bundler_hdr;
813
814 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000815 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700816 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300817 skb_copy_to_linear_data(bundler, &bundler_hdr,
818 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 skb_trim(bundler, INT_H_SIZE);
820 link_bundle_buf(l_ptr, bundler, buf);
821 buf = bundler;
822 msg = buf_msg(buf);
823 l_ptr->stats.sent_bundles++;
824 }
825 }
826 }
827 if (!l_ptr->next_out)
828 l_ptr->next_out = buf;
829 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 return dsz;
831}
832
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833/*
834 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 * not been selected yet, and the the owner node is not locked
836 * Called by TIPC internal users, e.g. the name distributor
837 */
Per Liden4323add2006-01-18 00:38:21 +0100838int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500840 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700841 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 int res = -ELINKCONG;
843
Per Liden4323add2006-01-18 00:38:21 +0100844 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000845 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100847 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000849 if (l_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100850 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000851 else
Allan Stephens5f6d9122011-11-04 13:24:29 -0400852 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100853 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 } else {
Allan Stephens5f6d9122011-11-04 13:24:29 -0400855 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 }
Per Liden4323add2006-01-18 00:38:21 +0100857 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 return res;
859}
860
Jon Maloyc64f7a62012-11-16 13:51:31 +0800861/*
862 * tipc_link_send_sync - synchronize broadcast link endpoints.
863 *
864 * Give a newly added peer node the sequence number where it should
865 * start receiving and acking broadcast packets.
866 *
867 * Called with node locked
868 */
869static void tipc_link_send_sync(struct tipc_link *l)
870{
871 struct sk_buff *buf;
872 struct tipc_msg *msg;
873
874 buf = tipc_buf_acquire(INT_H_SIZE);
875 if (!buf)
876 return;
877
878 msg = buf_msg(buf);
879 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
880 msg_set_last_bcast(msg, l->owner->bclink.acked);
881 link_add_chain_to_outqueue(l, buf, 0);
882 tipc_link_push_queue(l);
883}
884
885/*
886 * tipc_link_recv_sync - synchronize broadcast link endpoints.
887 * Receive the sequence number where we should start receiving and
888 * acking broadcast packets from a newly added peer node, and open
889 * up for reception of such packets.
890 *
891 * Called with node locked
892 */
893static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
894{
895 struct tipc_msg *msg = buf_msg(buf);
896
897 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
898 n->bclink.recv_permitted = true;
899 kfree_skb(buf);
900}
901
902/*
Allan Stephens9aa88c22011-05-31 13:38:02 -0400903 * tipc_link_send_names - send name table entries to new neighbor
904 *
905 * Send routine for bulk delivery of name table messages when contact
906 * with a new neighbor occurs. No link congestion checking is performed
907 * because name table messages *must* be delivered. The messages must be
908 * small enough not to require fragmentation.
909 * Called without any locks held.
910 */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400911void tipc_link_send_names(struct list_head *message_list, u32 dest)
912{
913 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500914 struct tipc_link *l_ptr;
Allan Stephens9aa88c22011-05-31 13:38:02 -0400915 struct sk_buff *buf;
916 struct sk_buff *temp_buf;
917
918 if (list_empty(message_list))
919 return;
920
921 read_lock_bh(&tipc_net_lock);
922 n_ptr = tipc_node_find(dest);
923 if (n_ptr) {
924 tipc_node_lock(n_ptr);
925 l_ptr = n_ptr->active_links[0];
926 if (l_ptr) {
927 /* convert circular list to linear list */
928 ((struct sk_buff *)message_list->prev)->next = NULL;
929 link_add_chain_to_outqueue(l_ptr,
930 (struct sk_buff *)message_list->next, 0);
931 tipc_link_push_queue(l_ptr);
932 INIT_LIST_HEAD(message_list);
933 }
934 tipc_node_unlock(n_ptr);
935 }
936 read_unlock_bh(&tipc_net_lock);
937
938 /* discard the messages if they couldn't be sent */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400939 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
940 list_del((struct list_head *)buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400941 kfree_skb(buf);
Allan Stephens9aa88c22011-05-31 13:38:02 -0400942 }
943}
944
945/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900946 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 * destination link is known and the header is complete,
948 * inclusive total message length. Very time critical.
949 * Link is locked. Returns user data length.
950 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500951static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800952 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953{
954 struct tipc_msg *msg = buf_msg(buf);
955 int res = msg_data_sz(msg);
956
957 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +0000958 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Erik Hugne512137e2013-12-06 10:08:00 -0500959 link_add_to_outqueue(l_ptr, buf, msg);
960 tipc_bearer_send(l_ptr->b_ptr, buf,
961 &l_ptr->media_addr);
962 l_ptr->unacked_window = 0;
963 return res;
964 }
965 else
Allan Stephens15e979d2010-05-11 14:30:10 +0000966 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967 }
Per Liden4323add2006-01-18 00:38:21 +0100968 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969}
970
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900971/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900972 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900974 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 * Returns user data length or errno.
976 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500977int tipc_link_send_sections_fast(struct tipc_port *sender,
Per Liden4323add2006-01-18 00:38:21 +0100978 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +0200979 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500981 struct tipc_msg *hdr = &sender->phdr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500982 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700984 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 int res;
986 u32 selector = msg_origport(hdr) & 1;
987
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988again:
989 /*
990 * Try building message using port's max_pkt hint.
991 * (Must not hold any locks while building message.)
992 */
Ying Xue9446b872013-10-18 07:23:15 +0200993 res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
Ying Xue7410f962013-06-17 10:54:49 -0400994 /* Exit if build request was invalid */
995 if (unlikely(res < 0))
996 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997
Per Liden4323add2006-01-18 00:38:21 +0100998 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000999 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001001 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 l_ptr = node->active_links[selector];
1003 if (likely(l_ptr)) {
1004 if (likely(buf)) {
1005 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001006 &sender->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007exit:
Per Liden4323add2006-01-18 00:38:21 +01001008 tipc_node_unlock(node);
1009 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 return res;
1011 }
1012
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 /* Exit if link (or bearer) is congested */
Erik Hugne512137e2013-12-06 10:08:00 -05001014 if (link_congested(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 res = link_schedule_port(l_ptr,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001016 sender->ref, res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 goto exit;
1018 }
1019
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001020 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 * Message size exceeds max_pkt hint; update hint,
1022 * then re-try fast path or fragment the message
1023 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001024 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001025 tipc_node_unlock(node);
1026 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027
1028
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001029 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 goto again;
1031
Ying Xue9446b872013-10-18 07:23:15 +02001032 return link_send_sections_long(sender, msg_sect, len,
Allan Stephens26896902011-04-21 10:42:07 -05001033 destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 }
Per Liden4323add2006-01-18 00:38:21 +01001035 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 }
Per Liden4323add2006-01-18 00:38:21 +01001037 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038
1039 /* Couldn't find a link to the destination node */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 if (buf)
1041 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1042 if (res >= 0)
Ying Xue9446b872013-10-18 07:23:15 +02001043 return tipc_port_reject_sections(sender, hdr, msg_sect,
1044 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 return res;
1046}
1047
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001048/*
1049 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 * Link and bearer congestion status have been checked to be ok,
1053 * and are ignored if they change.
1054 *
1055 * Note that fragments do not use the full link MTU so that they won't have
1056 * to undergo refragmentation if link changeover causes them to be sent
1057 * over another link with an additional tunnel header added as prefix.
1058 * (Refragmentation will still occur if the other link has a smaller MTU.)
1059 *
1060 * Returns user data length or errno.
1061 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001062static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +02001064 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001066 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001067 struct tipc_node *node;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001068 struct tipc_msg *hdr = &sender->phdr;
Ying Xue9446b872013-10-18 07:23:15 +02001069 u32 dsz = len;
Allan Stephens0e659672010-12-31 18:59:32 +00001070 u32 max_pkt, fragm_sz, rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 struct tipc_msg fragm_hdr;
Allan Stephens0e659672010-12-31 18:59:32 +00001072 struct sk_buff *buf, *buf_chain, *prev;
1073 u32 fragm_crs, fragm_rest, hsz, sect_rest;
Ying Xue40682432013-10-18 07:23:16 +02001074 const unchar __user *sect_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 int curr_sect;
1076 u32 fragm_no;
Ying Xue126c0522013-06-17 10:54:50 -04001077 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078
1079again:
1080 fragm_no = 1;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001081 max_pkt = sender->max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001083 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 /* leave room for fragmentation header in each fragment */
1085 rest = dsz;
1086 fragm_crs = 0;
1087 fragm_rest = 0;
1088 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001089 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 curr_sect = -1;
1091
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001092 /* Prepare reusable fragment header */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001093 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001094 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 msg_set_size(&fragm_hdr, max_pkt);
1096 msg_set_fragm_no(&fragm_hdr, 1);
1097
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001098 /* Prepare header of first fragment */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001099 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 if (!buf)
1101 return -ENOMEM;
1102 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001103 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001105 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001107 /* Chop up message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 fragm_crs = INT_H_SIZE + hsz;
1109 fragm_rest = fragm_sz - hsz;
1110
1111 do { /* For all sections */
1112 u32 sz;
1113
1114 if (!sect_rest) {
1115 sect_rest = msg_sect[++curr_sect].iov_len;
Ying Xue40682432013-10-18 07:23:16 +02001116 sect_crs = msg_sect[curr_sect].iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 }
1118
1119 if (sect_rest < fragm_rest)
1120 sz = sect_rest;
1121 else
1122 sz = fragm_rest;
1123
Ying Xuef1733d72013-06-17 10:54:43 -04001124 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
Ying Xue126c0522013-06-17 10:54:50 -04001125 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126error:
Ying Xuef1733d72013-06-17 10:54:43 -04001127 for (; buf_chain; buf_chain = buf) {
1128 buf = buf_chain->next;
1129 kfree_skb(buf_chain);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 }
Ying Xue126c0522013-06-17 10:54:50 -04001131 return res;
Ying Xuef1733d72013-06-17 10:54:43 -04001132 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 sect_crs += sz;
1134 sect_rest -= sz;
1135 fragm_crs += sz;
1136 fragm_rest -= sz;
1137 rest -= sz;
1138
1139 if (!fragm_rest && rest) {
1140
1141 /* Initiate new fragment: */
1142 if (rest <= fragm_sz) {
1143 fragm_sz = rest;
Allan Stephens0e659672010-12-31 18:59:32 +00001144 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 } else {
1146 msg_set_type(&fragm_hdr, FRAGMENT);
1147 }
1148 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1149 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1150 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001151 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Ying Xue126c0522013-06-17 10:54:50 -04001152 if (!buf) {
1153 res = -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 goto error;
Ying Xue126c0522013-06-17 10:54:50 -04001155 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001157 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001159 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 fragm_crs = INT_H_SIZE;
1161 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 }
Allan Stephens0e659672010-12-31 18:59:32 +00001163 } while (rest > 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001165 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 * Now we have a buffer chain. Select a link and check
1167 * that packet size is still OK
1168 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001169 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001171 tipc_node_lock(node);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001172 l_ptr = node->active_links[sender->ref & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001174 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 goto reject;
1176 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001177 if (l_ptr->max_pkt < max_pkt) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001178 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001179 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 for (; buf_chain; buf_chain = buf) {
1181 buf = buf_chain->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001182 kfree_skb(buf_chain);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 }
1184 goto again;
1185 }
1186 } else {
1187reject:
1188 for (; buf_chain; buf_chain = buf) {
1189 buf = buf_chain->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001190 kfree_skb(buf_chain);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 }
Ying Xue9446b872013-10-18 07:23:15 +02001192 return tipc_port_reject_sections(sender, hdr, msg_sect,
1193 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 }
1195
Allan Stephensdc63d912011-04-21 11:50:42 -04001196 /* Append chain of fragments to send queue & send them */
Allan Stephense0f08592011-04-17 11:44:24 -04001197 l_ptr->long_msg_seq_no++;
Allan Stephensdc63d912011-04-21 11:50:42 -04001198 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
1199 l_ptr->stats.sent_fragments += fragm_no;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 l_ptr->stats.sent_fragmented++;
Per Liden4323add2006-01-18 00:38:21 +01001201 tipc_link_push_queue(l_ptr);
1202 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 return dsz;
1204}
1205
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001206/*
Per Liden4323add2006-01-18 00:38:21 +01001207 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001209u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210{
1211 struct sk_buff *buf = l_ptr->first_out;
1212 u32 r_q_size = l_ptr->retransm_queue_size;
1213 u32 r_q_head = l_ptr->retransm_queue_head;
1214
1215 /* Step to position where retransmission failed, if any, */
1216 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001218 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -04001220 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221
1222 while (buf && less(first, r_q_head)) {
1223 first = mod(first + 1);
1224 buf = buf->next;
1225 }
1226 l_ptr->retransm_queue_head = r_q_head = first;
1227 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1228 }
1229
1230 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +00001231 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001233 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001234 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1235 l_ptr->retransm_queue_head = mod(++r_q_head);
1236 l_ptr->retransm_queue_size = --r_q_size;
1237 l_ptr->stats.retransmitted++;
1238 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 }
1240
1241 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 buf = l_ptr->proto_msg_queue;
1243 if (buf) {
1244 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001245 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001246 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1247 l_ptr->unacked_window = 0;
1248 kfree_skb(buf);
1249 l_ptr->proto_msg_queue = NULL;
1250 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 }
1252
1253 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 buf = l_ptr->next_out;
1255 if (buf) {
1256 struct tipc_msg *msg = buf_msg(buf);
1257 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -04001258 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259
1260 if (mod(next - first) < l_ptr->queue_limit[0]) {
1261 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001262 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001263 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1264 if (msg_user(msg) == MSG_BUNDLER)
1265 msg_set_type(msg, CLOSED_MSG);
1266 l_ptr->next_out = buf->next;
1267 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 }
1269 }
Ying Xue3c294cb2012-11-15 11:34:45 +08001270 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271}
1272
1273/*
1274 * push_queue(): push out the unsent messages of a link where
1275 * congestion has abated. Node is locked
1276 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001277void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278{
1279 u32 res;
1280
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 do {
Per Liden4323add2006-01-18 00:38:21 +01001282 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001283 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284}
1285
Allan Stephensd356eeb2006-06-25 23:40:01 -07001286static void link_reset_all(unsigned long addr)
1287{
David S. Miller6c000552008-09-02 23:38:32 -07001288 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001289 char addr_string[16];
1290 u32 i;
1291
1292 read_lock_bh(&tipc_net_lock);
1293 n_ptr = tipc_node_find((u32)addr);
1294 if (!n_ptr) {
1295 read_unlock_bh(&tipc_net_lock);
1296 return; /* node no longer exists */
1297 }
1298
1299 tipc_node_lock(n_ptr);
1300
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001301 pr_warn("Resetting all links to %s\n",
1302 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001303
1304 for (i = 0; i < MAX_BEARERS; i++) {
1305 if (n_ptr->links[i]) {
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001306 link_print(n_ptr->links[i], "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001307 tipc_link_reset(n_ptr->links[i]);
1308 }
1309 }
1310
1311 tipc_node_unlock(n_ptr);
1312 read_unlock_bh(&tipc_net_lock);
1313}
1314
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001315static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001316 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001317{
1318 struct tipc_msg *msg = buf_msg(buf);
1319
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001320 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001321
1322 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001323 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001324 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001325 tipc_link_reset(l_ptr);
1326
1327 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001328 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -07001329 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001330 char addr_string[16];
1331
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001332 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1333 pr_cont("Outstanding acks: %lu\n",
1334 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001335
Allan Stephens01d83ed2011-01-18 13:53:16 -05001336 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -07001337 tipc_node_lock(n_ptr);
1338
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001339 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001340 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +08001341 pr_info("Reception permitted: %d, Acked: %u\n",
1342 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001343 n_ptr->bclink.acked);
1344 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1345 n_ptr->bclink.last_in,
1346 n_ptr->bclink.oos_state,
1347 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001348
1349 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1350
1351 tipc_node_unlock(n_ptr);
1352
1353 l_ptr->stale_count = 0;
1354 }
1355}
1356
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001357void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001358 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359{
1360 struct tipc_msg *msg;
1361
Allan Stephensd356eeb2006-06-25 23:40:01 -07001362 if (!buf)
1363 return;
1364
1365 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001366
Erik Hugne512137e2013-12-06 10:08:00 -05001367 /* Detect repeated retransmit failures */
1368 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1369 if (++l_ptr->stale_count > 100) {
1370 link_retransmit_failure(l_ptr, buf);
1371 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001372 }
1373 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001374 l_ptr->last_retransmitted = msg_seqno(msg);
1375 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001377
Neil Hormanca509102010-03-15 07:58:45 +00001378 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379 msg = buf_msg(buf);
1380 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001381 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001382 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1383 buf = buf->next;
1384 retransmits--;
1385 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001387
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1389}
1390
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 * link_insert_deferred_queue - insert deferred messages back into receive chain
1393 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001394static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395 struct sk_buff *buf)
1396{
1397 u32 seq_no;
1398
1399 if (l_ptr->oldest_deferred_in == NULL)
1400 return buf;
1401
Allan Stephensf9057302011-10-24 16:03:12 -04001402 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 if (seq_no == mod(l_ptr->next_in_no)) {
1404 l_ptr->newest_deferred_in->next = buf;
1405 buf = l_ptr->oldest_deferred_in;
1406 l_ptr->oldest_deferred_in = NULL;
1407 l_ptr->deferred_inqueue_sz = 0;
1408 }
1409 return buf;
1410}
1411
Allan Stephens85035562008-04-15 19:04:54 -07001412/**
1413 * link_recv_buf_validate - validate basic format of received message
1414 *
1415 * This routine ensures a TIPC message has an acceptable header, and at least
1416 * as much data as the header indicates it should. The routine also ensures
1417 * that the entire message header is stored in the main fragment of the message
1418 * buffer, to simplify future access to message header fields.
1419 *
1420 * Note: Having extra info present in the message header or data areas is OK.
1421 * TIPC will ignore the excess, under the assumption that it is optional info
1422 * introduced by a later release of the protocol.
1423 */
Allan Stephens85035562008-04-15 19:04:54 -07001424static int link_recv_buf_validate(struct sk_buff *buf)
1425{
1426 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001427 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001428 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1429 };
1430
1431 struct tipc_msg *msg;
1432 u32 tipc_hdr[2];
1433 u32 size;
1434 u32 hdr_size;
1435 u32 min_hdr_size;
1436
1437 if (unlikely(buf->len < MIN_H_SIZE))
1438 return 0;
1439
1440 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1441 if (msg == NULL)
1442 return 0;
1443
1444 if (unlikely(msg_version(msg) != TIPC_VERSION))
1445 return 0;
1446
1447 size = msg_size(msg);
1448 hdr_size = msg_hdr_sz(msg);
1449 min_hdr_size = msg_isdata(msg) ?
1450 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1451
1452 if (unlikely((hdr_size < min_hdr_size) ||
1453 (size < hdr_size) ||
1454 (buf->len < size) ||
1455 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1456 return 0;
1457
1458 return pskb_may_pull(buf, hdr_size);
1459}
1460
Allan Stephensb02b69c2010-08-17 11:00:07 +00001461/**
1462 * tipc_recv_msg - process TIPC messages arriving from off-node
1463 * @head: pointer to message buffer chain
1464 * @tb_ptr: pointer to bearer message arrived on
1465 *
1466 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1467 * structure (i.e. cannot be NULL), but bearer can be inactive.
1468 */
Allan Stephens2d627b92011-01-07 13:00:11 -05001469void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470{
Per Liden4323add2006-01-18 00:38:21 +01001471 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001473 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001474 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 struct sk_buff *crs;
1476 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001477 struct tipc_msg *msg;
1478 u32 seq_no;
1479 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 u32 released = 0;
1481 int type;
1482
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 head = head->next;
Allan Stephens85035562008-04-15 19:04:54 -07001484
Allan Stephensb02b69c2010-08-17 11:00:07 +00001485 /* Ensure bearer is still enabled */
Allan Stephensb02b69c2010-08-17 11:00:07 +00001486 if (unlikely(!b_ptr->active))
Ying Xue3af390e2013-10-30 11:26:57 +08001487 goto discard;
Allan Stephensb02b69c2010-08-17 11:00:07 +00001488
Allan Stephens85035562008-04-15 19:04:54 -07001489 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001490 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001491 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492
Allan Stephensfe13dda2008-04-15 19:03:23 -07001493 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001494 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001495 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001496
Allan Stephens85035562008-04-15 19:04:54 -07001497 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001498 msg = buf_msg(buf);
1499
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001501 if (msg_user(msg) == LINK_CONFIG)
1502 tipc_disc_recv_msg(buf, b_ptr);
1503 else
1504 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 continue;
1506 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001507
Allan Stephensed33a9c2011-04-05 15:15:04 -04001508 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001509 if (unlikely(!msg_short(msg) &&
1510 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001511 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001512
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001513 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001514 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001516 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001517 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001518
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001519 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001521 if (unlikely(!l_ptr))
1522 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001523
Allan Stephensb4b56102011-05-27 11:00:51 -04001524 /* Verify that communication with node is currently allowed */
Allan Stephensb4b56102011-05-27 11:00:51 -04001525 if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
1526 msg_user(msg) == LINK_PROTOCOL &&
1527 (msg_type(msg) == RESET_MSG ||
1528 msg_type(msg) == ACTIVATE_MSG) &&
1529 !msg_redundant_link(msg))
1530 n_ptr->block_setup &= ~WAIT_PEER_DOWN;
1531
Ying Xue3af390e2013-10-30 11:26:57 +08001532 if (n_ptr->block_setup)
1533 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001534
Allan Stephens85035562008-04-15 19:04:54 -07001535 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001536 seq_no = msg_seqno(msg);
1537 ackd = msg_ack(msg);
1538
1539 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001540 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001541 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542
1543 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001544 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001545 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001546 struct sk_buff *next = crs->next;
1547
Allan Stephens5f6d9122011-11-04 13:24:29 -04001548 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549 crs = next;
1550 released++;
1551 }
1552 if (released) {
1553 l_ptr->first_out = crs;
1554 l_ptr->out_queue_size -= released;
1555 }
Allan Stephens85035562008-04-15 19:04:54 -07001556
1557 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001559 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001561 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1563 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001564 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 }
1566
Allan Stephens85035562008-04-15 19:04:54 -07001567 /* Now (finally!) process the incoming message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568protocol_check:
Ying Xue3af390e2013-10-30 11:26:57 +08001569 if (unlikely(!link_working_working(l_ptr))) {
1570 if (msg_user(msg) == LINK_PROTOCOL) {
1571 link_recv_proto_msg(l_ptr, buf);
1572 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001573 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 continue;
1575 }
Ying Xue3af390e2013-10-30 11:26:57 +08001576
1577 /* Traffic message. Conditionally activate link */
1578 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1579
1580 if (link_working_working(l_ptr)) {
1581 /* Re-insert buffer in front of queue */
1582 buf->next = head;
1583 head = buf;
1584 tipc_node_unlock(n_ptr);
1585 continue;
1586 }
1587 goto unlock_discard;
1588 }
1589
1590 /* Link is now in state WORKING_WORKING */
1591 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592 link_handle_out_of_seq_msg(l_ptr, buf);
1593 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001594 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 continue;
1596 }
Ying Xue3af390e2013-10-30 11:26:57 +08001597 l_ptr->next_in_no++;
1598 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599 head = link_insert_deferred_queue(l_ptr, head);
Ying Xue3af390e2013-10-30 11:26:57 +08001600deliver:
1601 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001602 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001603 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604 continue;
1605 }
Ying Xue3af390e2013-10-30 11:26:57 +08001606 switch (msg_user(msg)) {
1607 int ret;
1608 case MSG_BUNDLER:
1609 l_ptr->stats.recv_bundles++;
1610 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1611 tipc_node_unlock(n_ptr);
1612 tipc_link_recv_bundle(buf);
1613 continue;
1614 case NAME_DISTRIBUTOR:
1615 n_ptr->bclink.recv_permitted = true;
1616 tipc_node_unlock(n_ptr);
1617 tipc_named_recv(buf);
1618 continue;
1619 case BCAST_PROTOCOL:
1620 tipc_link_recv_sync(n_ptr, buf);
Per Liden4323add2006-01-18 00:38:21 +01001621 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001623 case CONN_MANAGER:
1624 tipc_node_unlock(n_ptr);
1625 tipc_port_recv_proto_msg(buf);
1626 continue;
1627 case MSG_FRAGMENTER:
1628 l_ptr->stats.recv_fragments++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001629 ret = tipc_link_recv_fragment(&l_ptr->reasm_head,
1630 &l_ptr->reasm_tail,
1631 &buf);
1632 if (ret == LINK_REASM_COMPLETE) {
Ying Xue3af390e2013-10-30 11:26:57 +08001633 l_ptr->stats.recv_fragmented++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001634 msg = buf_msg(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001635 goto deliver;
1636 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001637 if (ret == LINK_REASM_ERROR)
Erik Hugnea715b492013-11-06 09:28:07 +01001638 tipc_link_reset(l_ptr);
Erik Hugne528f6f42013-11-06 09:28:05 +01001639 tipc_node_unlock(n_ptr);
1640 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001641 case CHANGEOVER_PROTOCOL:
1642 type = msg_type(msg);
1643 if (link_recv_changeover_msg(&l_ptr, &buf)) {
1644 msg = buf_msg(buf);
1645 seq_no = msg_seqno(msg);
1646 if (type == ORIGINAL_MSG)
1647 goto deliver;
1648 goto protocol_check;
1649 }
1650 break;
1651 default:
1652 kfree_skb(buf);
1653 buf = NULL;
1654 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 }
Per Liden4323add2006-01-18 00:38:21 +01001656 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001657 tipc_net_route_msg(buf);
1658 continue;
1659unlock_discard:
1660
1661 tipc_node_unlock(n_ptr);
1662discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001663 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664 }
Per Liden4323add2006-01-18 00:38:21 +01001665 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666}
1667
Ben Hutchings2c530402012-07-10 10:55:09 +00001668/**
Allan Stephens8809b252011-10-25 10:44:35 -04001669 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1670 *
1671 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 */
Allan Stephens8809b252011-10-25 10:44:35 -04001673u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001674 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675{
Allan Stephens8809b252011-10-25 10:44:35 -04001676 struct sk_buff *queue_buf;
1677 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001678 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679
1680 buf->next = NULL;
1681
1682 /* Empty queue ? */
1683 if (*head == NULL) {
1684 *head = *tail = buf;
1685 return 1;
1686 }
1687
1688 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001689 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 (*tail)->next = buf;
1691 *tail = buf;
1692 return 1;
1693 }
1694
Allan Stephens8809b252011-10-25 10:44:35 -04001695 /* Locate insertion point in queue, then insert; discard if duplicate */
1696 prev = head;
1697 queue_buf = *head;
1698 for (;;) {
1699 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700
Allan Stephens8809b252011-10-25 10:44:35 -04001701 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001702 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001703 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704 }
Allan Stephens8809b252011-10-25 10:44:35 -04001705
1706 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708
Allan Stephens8809b252011-10-25 10:44:35 -04001709 prev = &queue_buf->next;
1710 queue_buf = queue_buf->next;
1711 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712
Allan Stephens8809b252011-10-25 10:44:35 -04001713 buf->next = queue_buf;
1714 *prev = buf;
1715 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716}
1717
Allan Stephens8809b252011-10-25 10:44:35 -04001718/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1720 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001721static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 struct sk_buff *buf)
1723{
Allan Stephensf9057302011-10-24 16:03:12 -04001724 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725
1726 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1727 link_recv_proto_msg(l_ptr, buf);
1728 return;
1729 }
1730
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 l_ptr->checkpoint--;
1733
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001734 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 * Discard packet if a duplicate; otherwise add it to deferred queue
1736 * and notify peer of gap as per protocol specification
1737 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 if (less(seq_no, mod(l_ptr->next_in_no))) {
1739 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001740 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 return;
1742 }
1743
Per Liden4323add2006-01-18 00:38:21 +01001744 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1745 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 l_ptr->deferred_inqueue_sz++;
1747 l_ptr->stats.deferred_recv++;
1748 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01001749 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 } else
1751 l_ptr->stats.duplicates++;
1752}
1753
1754/*
1755 * Send protocol message to the other endpoint.
1756 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001757void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001758 int probe_msg, u32 gap, u32 tolerance,
1759 u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001761 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001763 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001764 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765
Allan Stephens92d2c902011-10-25 11:20:26 -04001766 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001767 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001768 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001769 l_ptr->proto_msg_queue = NULL;
1770 }
1771
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 if (link_blocked(l_ptr))
1773 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001774
1775 /* Abort non-RESET send if communication with node is prohibited */
Allan Stephensb4b56102011-05-27 11:00:51 -04001776 if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
1777 return;
1778
Allan Stephens92d2c902011-10-25 11:20:26 -04001779 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 msg_set_type(msg, msg_typ);
1781 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001782 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001783 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784
1785 if (msg_typ == STATE_MSG) {
1786 u32 next_sent = mod(l_ptr->next_out_no);
1787
Per Liden4323add2006-01-18 00:38:21 +01001788 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 return;
1790 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001791 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792 msg_set_next_sent(msg, next_sent);
1793 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001794 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795 gap = mod(rec - mod(l_ptr->next_in_no));
1796 }
1797 msg_set_seq_gap(msg, gap);
1798 if (gap)
1799 l_ptr->stats.sent_nacks++;
1800 msg_set_link_tolerance(msg, tolerance);
1801 msg_set_linkprio(msg, priority);
1802 msg_set_max_pkt(msg, ack_mtu);
1803 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1804 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001805 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 u32 mtu = l_ptr->max_pkt;
1807
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001808 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 link_working_working(l_ptr) &&
1810 l_ptr->fsm_msg_cnt) {
1811 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001812 if (l_ptr->max_pkt_probes == 10) {
1813 l_ptr->max_pkt_target = (msg_size - 4);
1814 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001816 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001818 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819
1820 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001821 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 l_ptr->stats.sent_states++;
1823 } else { /* RESET_MSG or ACTIVATE_MSG */
1824 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1825 msg_set_seq_gap(msg, 0);
1826 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001827 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828 msg_set_link_tolerance(msg, l_ptr->tolerance);
1829 msg_set_linkprio(msg, l_ptr->priority);
1830 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1831 }
1832
Allan Stephens75f0aa42011-02-28 15:30:20 -05001833 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1834 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001836 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837
1838 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1839
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001840 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 if (!buf)
1842 return;
1843
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001844 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001845 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846
Ying Xue3c294cb2012-11-15 11:34:45 +08001847 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001848 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001849 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850}
1851
1852/*
1853 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001854 * Note that network plane id propagates through the network, and may
1855 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001857static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858{
1859 u32 rec_gap = 0;
1860 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001861 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 u32 msg_tol;
1863 struct tipc_msg *msg = buf_msg(buf);
1864
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 if (link_blocked(l_ptr))
1866 goto exit;
1867
1868 /* record unnumbered packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 l_ptr->checkpoint--;
1870
1871 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
1872 if (tipc_own_addr > msg_prevnode(msg))
1873 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
1874
1875 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
1876
1877 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001878
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001880 if (!link_working_unknown(l_ptr) &&
1881 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001882 if (less_eq(msg_session(msg), l_ptr->peer_session))
1883 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001885
1886 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1887 link_working_unknown(l_ptr))) {
1888 /*
1889 * peer has lost contact -- don't allow peer's links
1890 * to reactivate before we recognize loss & clean up
1891 */
1892 l_ptr->owner->block_setup = WAIT_NODE_DOWN;
1893 }
1894
Allan Stephens47361c82011-10-26 10:55:16 -04001895 link_state_event(l_ptr, RESET_MSG);
1896
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 /* fall thru' */
1898 case ACTIVATE_MSG:
1899 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1901
Allan Stephens2db99832010-12-31 18:59:33 +00001902 msg_tol = msg_link_tolerance(msg);
1903 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904 link_set_supervision_props(l_ptr, msg_tol);
1905
1906 if (msg_linkprio(msg) > l_ptr->priority)
1907 l_ptr->priority = msg_linkprio(msg);
1908
1909 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001910 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 if (max_pkt_info < l_ptr->max_pkt_target)
1912 l_ptr->max_pkt_target = max_pkt_info;
1913 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1914 l_ptr->max_pkt = l_ptr->max_pkt_target;
1915 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001916 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918
Allan Stephens4d753132011-10-25 12:19:05 -04001919 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001920 if (!tipc_node_is_up(l_ptr->owner)) {
1921 l_ptr->owner->bclink.last_sent =
1922 l_ptr->owner->bclink.last_in =
1923 msg_last_bcast(msg);
1924 l_ptr->owner->bclink.oos_state = 0;
1925 }
Allan Stephens4d753132011-10-25 12:19:05 -04001926
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927 l_ptr->peer_session = msg_session(msg);
1928 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001929
1930 if (msg_type(msg) == ACTIVATE_MSG)
1931 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932 break;
1933 case STATE_MSG:
1934
Allan Stephens2db99832010-12-31 18:59:33 +00001935 msg_tol = msg_link_tolerance(msg);
1936 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001938
1939 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001941 pr_warn("%s<%s>, priority change %u->%u\n",
1942 link_rst_msg, l_ptr->name, l_ptr->priority,
1943 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001944 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001945 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 break;
1947 }
1948 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1949 l_ptr->stats.recv_states++;
1950 if (link_reset_unknown(l_ptr))
1951 break;
1952
1953 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001954 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955 mod(l_ptr->next_in_no));
1956 }
1957
1958 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001959 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001960 l_ptr->max_pkt = max_pkt_ack;
1961 l_ptr->max_pkt_probes = 0;
1962 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963
1964 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001965 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001967 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001968 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001969 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970
1971 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001972 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001973 tipc_bclink_update_link_state(l_ptr->owner,
1974 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975
1976 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001977 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
1978 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 }
1980 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001982 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1983 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984 }
1985 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986 }
1987exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001988 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989}
1990
1991
1992/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001993 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994 * another bearer. Owner node is locked.
1995 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001996static void tipc_link_tunnel(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001997 struct tipc_msg *tunnel_hdr, struct tipc_msg *msg,
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001998 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002000 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002001 struct sk_buff *buf;
2002 u32 length = msg_size(msg);
2003
2004 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002005 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002006 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002008 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002010 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002011 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002012 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002014 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002015 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2016 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01002017 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018}
2019
2020
2021
2022/*
2023 * changeover(): Send whole message queue via the remaining link
2024 * Owner node is locked.
2025 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002026void tipc_link_changeover(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027{
2028 u32 msgcount = l_ptr->out_queue_size;
2029 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002030 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002032 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033
2034 if (!tunnel)
2035 return;
2036
Allan Stephens5392d642006-06-25 23:52:50 -07002037 if (!l_ptr->owner->permit_changeover) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002038 pr_warn("%speer did not permit changeover\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002040 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002042 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002043 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2045 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002046
Per Lidenb97bf3f2006-01-02 19:04:38 +01002047 if (!l_ptr->first_out) {
2048 struct sk_buff *buf;
2049
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002050 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002052 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002054 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002056 pr_warn("%sunable to send changeover msg\n",
2057 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058 }
2059 return;
2060 }
Allan Stephensf1310722006-06-25 23:51:37 -07002061
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002062 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002063 l_ptr->owner->active_links[1]);
2064
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065 while (crs) {
2066 struct tipc_msg *msg = buf_msg(crs);
2067
2068 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00002070 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071
Florian Westphald788d802007-08-02 19:28:06 -07002072 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00002074 msg_set_seqno(m, msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002075 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2076 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077 pos += align(msg_size(m));
2078 m = (struct tipc_msg *)pos;
2079 }
2080 } else {
Per Liden4323add2006-01-18 00:38:21 +01002081 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2082 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083 }
2084 crs = crs->next;
2085 }
2086}
2087
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002088void tipc_link_send_duplicate(struct tipc_link *l_ptr, struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089{
2090 struct sk_buff *iter;
2091 struct tipc_msg tunnel_hdr;
2092
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002093 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002094 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2096 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2097 iter = l_ptr->first_out;
2098 while (iter) {
2099 struct sk_buff *outbuf;
2100 struct tipc_msg *msg = buf_msg(iter);
2101 u32 length = msg_size(msg);
2102
2103 if (msg_user(msg) == MSG_BUNDLER)
2104 msg_set_type(msg, CLOSED_MSG);
2105 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002106 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002108 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002109 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002110 pr_warn("%sunable to send duplicate msg\n",
2111 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112 return;
2113 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002114 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2115 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2116 length);
Per Liden4323add2006-01-18 00:38:21 +01002117 tipc_link_send_buf(tunnel, outbuf);
2118 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119 return;
2120 iter = iter->next;
2121 }
2122}
2123
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124/**
2125 * buf_extract - extracts embedded TIPC message from another message
2126 * @skb: encapsulating message buffer
2127 * @from_pos: offset to extract from
2128 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002129 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002130 * encapsulating message itself is left unchanged.
2131 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002132static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2133{
2134 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2135 u32 size = msg_size(msg);
2136 struct sk_buff *eb;
2137
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002138 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002139 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002140 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141 return eb;
2142}
2143
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002144/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002145 * link_recv_changeover_msg(): Receive tunneled packet sent
2146 * via other link. Node is locked. Return extracted buffer.
2147 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002148static int link_recv_changeover_msg(struct tipc_link **l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 struct sk_buff **buf)
2150{
2151 struct sk_buff *tunnel_buf = *buf;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002152 struct tipc_link *dest_link;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002153 struct tipc_msg *msg;
2154 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2155 u32 msg_typ = msg_type(tunnel_msg);
2156 u32 msg_count = msg_msgcnt(tunnel_msg);
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002157 u32 bearer_id = msg_bearer_id(tunnel_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002158
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002159 if (bearer_id >= MAX_BEARERS)
2160 goto exit;
2161 dest_link = (*l_ptr)->owner->links[bearer_id];
Allan Stephensb29f1422010-12-31 18:59:25 +00002162 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002163 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002164 if (dest_link == *l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002165 pr_err("Unexpected changeover message on link <%s>\n",
2166 (*l_ptr)->name);
Allan Stephensf1310722006-06-25 23:51:37 -07002167 goto exit;
2168 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002169 *l_ptr = dest_link;
2170 msg = msg_get_wrapped(tunnel_msg);
2171
2172 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002173 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002174 goto exit;
Allan Stephens0e659672010-12-31 18:59:32 +00002175 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002176 if (*buf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002177 pr_warn("%sduplicate msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002178 goto exit;
2179 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002180 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002181 return 1;
2182 }
2183
2184 /* First original message ?: */
Per Liden4323add2006-01-18 00:38:21 +01002185 if (tipc_link_is_up(dest_link)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002186 pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
2187 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002188 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002189 dest_link->exp_msg_count = msg_count;
2190 if (!msg_count)
2191 goto exit;
2192 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193 dest_link->exp_msg_count = msg_count;
2194 if (!msg_count)
2195 goto exit;
2196 }
2197
2198 /* Receive original message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002199 if (dest_link->exp_msg_count == 0) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002200 pr_warn("%sgot too many tunnelled messages\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002201 goto exit;
2202 }
2203 dest_link->exp_msg_count--;
2204 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205 goto exit;
2206 } else {
2207 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2208 if (*buf != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002209 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210 return 1;
2211 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002212 pr_warn("%soriginal msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002213 }
2214 }
2215exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002216 *buf = NULL;
Allan Stephens5f6d9122011-11-04 13:24:29 -04002217 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002218 return 0;
2219}
2220
2221/*
2222 * Bundler functionality:
2223 */
Per Liden4323add2006-01-18 00:38:21 +01002224void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002225{
2226 u32 msgcount = msg_msgcnt(buf_msg(buf));
2227 u32 pos = INT_H_SIZE;
2228 struct sk_buff *obuf;
2229
Per Lidenb97bf3f2006-01-02 19:04:38 +01002230 while (msgcount--) {
2231 obuf = buf_extract(buf, pos);
2232 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002233 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07002234 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002235 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002236 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002237 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002238 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002239 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240}
2241
2242/*
2243 * Fragmentation/defragmentation:
2244 */
2245
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002246/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002247 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002248 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002249 * Returns user data length.
2250 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002251static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002252{
Allan Stephens77561552011-04-17 13:06:23 -04002253 struct sk_buff *buf_chain = NULL;
2254 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002255 struct tipc_msg *inmsg = buf_msg(buf);
2256 struct tipc_msg fragm_hdr;
2257 u32 insize = msg_size(inmsg);
2258 u32 dsz = msg_data_sz(inmsg);
2259 unchar *crs = buf->data;
2260 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002261 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002262 u32 fragm_sz = pack_sz - INT_H_SIZE;
Allan Stephens77561552011-04-17 13:06:23 -04002263 u32 fragm_no = 0;
Allan Stephens9c396a72008-06-04 17:36:58 -07002264 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002265
2266 if (msg_short(inmsg))
2267 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002268 else
2269 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002270
Per Lidenb97bf3f2006-01-02 19:04:38 +01002271 /* Prepare reusable fragment header: */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002272 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002273 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002274
2275 /* Chop up message: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002276 while (rest > 0) {
2277 struct sk_buff *fragm;
2278
2279 if (rest <= fragm_sz) {
2280 fragm_sz = rest;
2281 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2282 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002283 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002284 if (fragm == NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002285 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002286 while (buf_chain) {
2287 buf = buf_chain;
2288 buf_chain = buf_chain->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04002289 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002290 }
2291 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002292 }
2293 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Allan Stephens77561552011-04-17 13:06:23 -04002294 fragm_no++;
2295 msg_set_fragm_no(&fragm_hdr, fragm_no);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002296 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2297 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2298 fragm_sz);
Allan Stephens77561552011-04-17 13:06:23 -04002299 buf_chain_tail->next = fragm;
2300 buf_chain_tail = fragm;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002301
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302 rest -= fragm_sz;
2303 crs += fragm_sz;
2304 msg_set_type(&fragm_hdr, FRAGMENT);
2305 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002306 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002307
2308 /* Append chain of fragments to send queue & send them */
Allan Stephens77561552011-04-17 13:06:23 -04002309 l_ptr->long_msg_seq_no++;
2310 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2311 l_ptr->stats.sent_fragments += fragm_no;
2312 l_ptr->stats.sent_fragmented++;
2313 tipc_link_push_queue(l_ptr);
2314
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315 return dsz;
2316}
2317
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002318/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002319 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002320 * the reassembled buffer if message is complete.
2321 */
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002322int tipc_link_recv_fragment(struct sk_buff **head, struct sk_buff **tail,
2323 struct sk_buff **fbuf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002324{
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002325 struct sk_buff *frag = *fbuf;
2326 struct tipc_msg *msg = buf_msg(frag);
2327 u32 fragid = msg_type(msg);
2328 bool headstolen;
2329 int delta;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002330
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002331 skb_pull(frag, msg_hdr_sz(msg));
2332 if (fragid == FIRST_FRAGMENT) {
2333 if (*head || skb_unclone(frag, GFP_ATOMIC))
2334 goto out_free;
2335 *head = frag;
2336 skb_frag_list_init(*head);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337 return 0;
Erik Hugne3db0a192013-11-13 09:35:11 +01002338 } else if (*head &&
2339 skb_try_coalesce(*head, frag, &headstolen, &delta)) {
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002340 kfree_skb_partial(frag, headstolen);
2341 } else {
2342 if (!*head)
2343 goto out_free;
2344 if (!skb_has_frag_list(*head))
2345 skb_shinfo(*head)->frag_list = frag;
2346 else
2347 (*tail)->next = frag;
2348 *tail = frag;
2349 (*head)->truesize += frag->truesize;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002351 if (fragid == LAST_FRAGMENT) {
2352 *fbuf = *head;
2353 *tail = *head = NULL;
2354 return LINK_REASM_COMPLETE;
2355 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002356 return 0;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002357out_free:
2358 pr_warn_ratelimited("Link unable to reassemble fragmented message\n");
2359 kfree_skb(*fbuf);
2360 return LINK_REASM_ERROR;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002361}
2362
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002363static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002364{
Allan Stephens5413b4c2011-01-18 13:24:55 -05002365 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2366 return;
2367
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368 l_ptr->tolerance = tolerance;
2369 l_ptr->continuity_interval =
2370 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2371 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2372}
2373
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002374void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375{
2376 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002377 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2378 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2379 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2380 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002382 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2383 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2384 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2385 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002386 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2388 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2389 /* FRAGMENT and LAST_FRAGMENT packets */
2390 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2391}
2392
2393/**
2394 * link_find_link - locate link by name
Ben Hutchings2c530402012-07-10 10:55:09 +00002395 * @name: ptr to link name string
2396 * @node: ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002397 *
Per Liden4323add2006-01-18 00:38:21 +01002398 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002400 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002401 * Returns pointer to link (or 0 if invalid link name).
2402 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002403static struct tipc_link *link_find_link(const char *name,
2404 struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002406 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002407 struct tipc_node *n_ptr;
2408 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409
Erik Hugnebbfbe472013-10-18 07:23:21 +02002410 list_for_each_entry(n_ptr, &tipc_node_list, list) {
2411 for (i = 0; i < MAX_BEARERS; i++) {
2412 l_ptr = n_ptr->links[i];
2413 if (l_ptr && !strcmp(l_ptr->name, name))
2414 goto found;
2415 }
2416 }
2417 l_ptr = NULL;
2418 n_ptr = NULL;
2419found:
2420 *node = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002421 return l_ptr;
2422}
2423
Allan Stephens5c216e12011-10-18 11:34:29 -04002424/**
2425 * link_value_is_valid -- validate proposed link tolerance/priority/window
2426 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002427 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2428 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002429 *
2430 * Returns 1 if value is within range, 0 if not.
2431 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002432static int link_value_is_valid(u16 cmd, u32 new_value)
2433{
2434 switch (cmd) {
2435 case TIPC_CMD_SET_LINK_TOL:
2436 return (new_value >= TIPC_MIN_LINK_TOL) &&
2437 (new_value <= TIPC_MAX_LINK_TOL);
2438 case TIPC_CMD_SET_LINK_PRI:
2439 return (new_value <= TIPC_MAX_LINK_PRI);
2440 case TIPC_CMD_SET_LINK_WINDOW:
2441 return (new_value >= TIPC_MIN_LINK_WIN) &&
2442 (new_value <= TIPC_MAX_LINK_WIN);
2443 }
2444 return 0;
2445}
2446
Allan Stephens5c216e12011-10-18 11:34:29 -04002447/**
2448 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002449 * @name: ptr to link, bearer, or media name
2450 * @new_value: new value of link, bearer, or media setting
2451 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002452 *
2453 * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
2454 *
2455 * Returns 0 if value updated and negative value on error.
2456 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002457static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2458{
2459 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002460 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002461 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002462 struct tipc_media *m_ptr;
Ying Xue636c0372013-10-18 07:23:20 +02002463 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002464
2465 l_ptr = link_find_link(name, &node);
2466 if (l_ptr) {
2467 /*
2468 * acquire node lock for tipc_link_send_proto_msg().
2469 * see "TIPC locking policy" in net.c.
2470 */
2471 tipc_node_lock(node);
2472 switch (cmd) {
2473 case TIPC_CMD_SET_LINK_TOL:
2474 link_set_supervision_props(l_ptr, new_value);
2475 tipc_link_send_proto_msg(l_ptr,
2476 STATE_MSG, 0, 0, new_value, 0, 0);
2477 break;
2478 case TIPC_CMD_SET_LINK_PRI:
2479 l_ptr->priority = new_value;
2480 tipc_link_send_proto_msg(l_ptr,
2481 STATE_MSG, 0, 0, 0, new_value, 0);
2482 break;
2483 case TIPC_CMD_SET_LINK_WINDOW:
2484 tipc_link_set_queue_limits(l_ptr, new_value);
2485 break;
Ying Xue636c0372013-10-18 07:23:20 +02002486 default:
2487 res = -EINVAL;
2488 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002489 }
2490 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002491 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002492 }
2493
2494 b_ptr = tipc_bearer_find(name);
2495 if (b_ptr) {
2496 switch (cmd) {
2497 case TIPC_CMD_SET_LINK_TOL:
2498 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002499 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002500 case TIPC_CMD_SET_LINK_PRI:
2501 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002502 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002503 case TIPC_CMD_SET_LINK_WINDOW:
2504 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002505 break;
2506 default:
2507 res = -EINVAL;
2508 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002509 }
Ying Xue636c0372013-10-18 07:23:20 +02002510 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002511 }
2512
2513 m_ptr = tipc_media_find(name);
2514 if (!m_ptr)
2515 return -ENODEV;
2516 switch (cmd) {
2517 case TIPC_CMD_SET_LINK_TOL:
2518 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002519 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002520 case TIPC_CMD_SET_LINK_PRI:
2521 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002522 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002523 case TIPC_CMD_SET_LINK_WINDOW:
2524 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002525 break;
2526 default:
2527 res = -EINVAL;
2528 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002529 }
Ying Xue636c0372013-10-18 07:23:20 +02002530 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002531}
2532
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002533struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002534 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002535{
2536 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002537 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002538 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002539
2540 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002541 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002542
2543 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2544 new_value = ntohl(args->value);
2545
Allan Stephens5c216e12011-10-18 11:34:29 -04002546 if (!link_value_is_valid(cmd, new_value))
2547 return tipc_cfg_reply_error_string(
2548 "cannot change, value invalid");
2549
Per Liden4323add2006-01-18 00:38:21 +01002550 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002551 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002552 (tipc_bclink_set_queue_limits(new_value) == 0))
2553 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002554 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002555 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002556 }
2557
Per Liden4323add2006-01-18 00:38:21 +01002558 read_lock_bh(&tipc_net_lock);
Allan Stephens5c216e12011-10-18 11:34:29 -04002559 res = link_cmd_set_value(args->name, new_value, cmd);
Per Liden4323add2006-01-18 00:38:21 +01002560 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002562 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002563
Per Liden4323add2006-01-18 00:38:21 +01002564 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002565}
2566
2567/**
2568 * link_reset_statistics - reset link statistics
2569 * @l_ptr: pointer to link
2570 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002571static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572{
2573 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2574 l_ptr->stats.sent_info = l_ptr->next_out_no;
2575 l_ptr->stats.recv_info = l_ptr->next_in_no;
2576}
2577
Per Liden4323add2006-01-18 00:38:21 +01002578struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579{
2580 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002581 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002582 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002583
2584 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002585 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586
2587 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002588 if (!strcmp(link_name, tipc_bclink_name)) {
2589 if (tipc_bclink_reset_stats())
2590 return tipc_cfg_reply_error_string("link not found");
2591 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002592 }
2593
Per Liden4323add2006-01-18 00:38:21 +01002594 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002595 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002596 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002597 read_unlock_bh(&tipc_net_lock);
2598 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002599 }
2600
Per Liden4323add2006-01-18 00:38:21 +01002601 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002602 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002603 tipc_node_unlock(node);
2604 read_unlock_bh(&tipc_net_lock);
2605 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002606}
2607
2608/**
2609 * percent - convert count to a percentage of total (rounding up or down)
2610 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611static u32 percent(u32 count, u32 total)
2612{
2613 return (count * 100 + (total / 2)) / total;
2614}
2615
2616/**
Per Liden4323add2006-01-18 00:38:21 +01002617 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618 * @name: link name
2619 * @buf: print buffer area
2620 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002621 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 * Returns length of print buffer data string (or 0 if error)
2623 */
Per Liden4323add2006-01-18 00:38:21 +01002624static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002625{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002626 struct tipc_link *l;
2627 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002628 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002629 char *status;
2630 u32 profile_total = 0;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002631 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632
Per Liden4323add2006-01-18 00:38:21 +01002633 if (!strcmp(name, tipc_bclink_name))
2634 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635
Per Liden4323add2006-01-18 00:38:21 +01002636 read_lock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002637 l = link_find_link(name, &node);
2638 if (!l) {
Per Liden4323add2006-01-18 00:38:21 +01002639 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002640 return 0;
2641 }
Per Liden4323add2006-01-18 00:38:21 +01002642 tipc_node_lock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002643 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644
Erik Hugnedc1aed32012-06-29 00:50:23 -04002645 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002646 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002647 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002648 status = "STANDBY";
2649 else
2650 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002651
2652 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2653 " %s MTU:%u Priority:%u Tolerance:%u ms"
2654 " Window:%u packets\n",
2655 l->name, status, l->max_pkt, l->priority,
2656 l->tolerance, l->queue_limit[0]);
2657
2658 ret += tipc_snprintf(buf + ret, buf_size - ret,
2659 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2660 l->next_in_no - s->recv_info, s->recv_fragments,
2661 s->recv_fragmented, s->recv_bundles,
2662 s->recv_bundled);
2663
2664 ret += tipc_snprintf(buf + ret, buf_size - ret,
2665 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2666 l->next_out_no - s->sent_info, s->sent_fragments,
2667 s->sent_fragmented, s->sent_bundles,
2668 s->sent_bundled);
2669
2670 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002671 if (!profile_total)
2672 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002673
2674 ret += tipc_snprintf(buf + ret, buf_size - ret,
2675 " TX profile sample:%u packets average:%u octets\n"
2676 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2677 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2678 s->msg_length_counts,
2679 s->msg_lengths_total / profile_total,
2680 percent(s->msg_length_profile[0], profile_total),
2681 percent(s->msg_length_profile[1], profile_total),
2682 percent(s->msg_length_profile[2], profile_total),
2683 percent(s->msg_length_profile[3], profile_total),
2684 percent(s->msg_length_profile[4], profile_total),
2685 percent(s->msg_length_profile[5], profile_total),
2686 percent(s->msg_length_profile[6], profile_total));
2687
2688 ret += tipc_snprintf(buf + ret, buf_size - ret,
2689 " RX states:%u probes:%u naks:%u defs:%u"
2690 " dups:%u\n", s->recv_states, s->recv_probes,
2691 s->recv_nacks, s->deferred_recv, s->duplicates);
2692
2693 ret += tipc_snprintf(buf + ret, buf_size - ret,
2694 " TX states:%u probes:%u naks:%u acks:%u"
2695 " dups:%u\n", s->sent_states, s->sent_probes,
2696 s->sent_nacks, s->sent_acks, s->retransmitted);
2697
2698 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002699 " Congestion link:%u Send queue"
2700 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002701 s->max_queue_sz, s->queue_sz_counts ?
2702 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002703
Per Liden4323add2006-01-18 00:38:21 +01002704 tipc_node_unlock(node);
2705 read_unlock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002706 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002707}
2708
Per Liden4323add2006-01-18 00:38:21 +01002709struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710{
2711 struct sk_buff *buf;
2712 struct tlv_desc *rep_tlv;
2713 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002714 int pb_len;
2715 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716
2717 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002718 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002719
Erik Hugnedc1aed32012-06-29 00:50:23 -04002720 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002721 if (!buf)
2722 return NULL;
2723
2724 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002725 pb = TLV_DATA(rep_tlv);
2726 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002727 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002728 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002729 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002730 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002731 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002732 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002733 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002734 skb_put(buf, TLV_SPACE(str_len));
2735 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2736
2737 return buf;
2738}
2739
Per Lidenb97bf3f2006-01-02 19:04:38 +01002740/**
Per Liden4323add2006-01-18 00:38:21 +01002741 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002742 * @dest: network address of destination node
2743 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002744 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002745 * If no active link can be found, uses default maximum packet size.
2746 */
Per Liden4323add2006-01-18 00:38:21 +01002747u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002748{
David S. Miller6c000552008-09-02 23:38:32 -07002749 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002750 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002751 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002752
Per Lidenb97bf3f2006-01-02 19:04:38 +01002753 if (dest == tipc_own_addr)
2754 return MAX_MSG_SIZE;
2755
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002756 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002757 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002758 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002759 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002760 l_ptr = n_ptr->active_links[selector & 1];
2761 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002762 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002763 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002764 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002765 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002766 return res;
2767}
2768
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002769static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002770{
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002771 pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002772
Per Lidenb97bf3f2006-01-02 19:04:38 +01002773 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002774 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002775 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002776 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002777 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002778 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002779 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002780 pr_cont(":WW\n");
2781 else
2782 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002783}