blob: 3ff34e8a37d7f4e67abd854233360d84e3ab921e [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/link.c: TIPC link code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy170b3922014-01-07 17:02:41 -05004 * Copyright (c) 1996-2007, 2012-2014, Ericsson AB
Ying Xue198d73b2013-06-17 10:54:42 -04005 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
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);
Jon Paul Maloy170b3922014-01-07 17:02:41 -050081static int tipc_link_tunnel_rcv(struct tipc_link **l_ptr,
82 struct sk_buff **buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050083static 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);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050090static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +080091static void tipc_link_send_sync(struct tipc_link *l);
92static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +000093
Per Lidenb97bf3f2006-01-02 19:04:38 +010094/*
Sam Ravnborg05790c62006-03-20 22:37:04 -080095 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +010096 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080097static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +010098{
99 return (i + 3) & ~3u;
100}
101
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500102static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103{
104 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900105
Allan Stephens2d627b92011-01-07 13:00:11 -0500106 max_pkt = (l_ptr->b_ptr->mtu & ~3);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107 if (max_pkt > MAX_MSG_SIZE)
108 max_pkt = MAX_MSG_SIZE;
109
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900110 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
112 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900113 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 l_ptr->max_pkt = MAX_PKT_DEFAULT;
115
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900116 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117}
118
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500119static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120{
121 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400122 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 return mod(l_ptr->next_out_no);
124}
125
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500126static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
128 return mod(link_next_sent(l_ptr) - 1);
129}
130
131/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800132 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500134int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135{
136 if (!l_ptr)
137 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000138 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139}
140
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500141int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000143 return (l_ptr->owner->active_links[0] == l_ptr) ||
144 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145}
146
147/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 * link_timeout - handle expiration of link timer
149 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900150 *
Per Liden4323add2006-01-18 00:38:21 +0100151 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
152 * with tipc_link_delete(). (There is no risk that the node will be deleted by
153 * another thread because tipc_link_delete() always cancels the link timer before
154 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500156static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157{
Per Liden4323add2006-01-18 00:38:21 +0100158 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159
160 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
162 l_ptr->stats.queue_sz_counts++;
163
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 if (l_ptr->first_out) {
165 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
166 u32 length = msg_size(msg);
167
Joe Perchesf64f9e72009-11-29 16:55:45 -0800168 if ((msg_user(msg) == MSG_FRAGMENTER) &&
169 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 length = msg_size(msg_get_wrapped(msg));
171 }
172 if (length) {
173 l_ptr->stats.msg_lengths_total += length;
174 l_ptr->stats.msg_length_counts++;
175 if (length <= 64)
176 l_ptr->stats.msg_length_profile[0]++;
177 else if (length <= 256)
178 l_ptr->stats.msg_length_profile[1]++;
179 else if (length <= 1024)
180 l_ptr->stats.msg_length_profile[2]++;
181 else if (length <= 4096)
182 l_ptr->stats.msg_length_profile[3]++;
183 else if (length <= 16384)
184 l_ptr->stats.msg_length_profile[4]++;
185 else if (length <= 32768)
186 l_ptr->stats.msg_length_profile[5]++;
187 else
188 l_ptr->stats.msg_length_profile[6]++;
189 }
190 }
191
192 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193
194 link_state_event(l_ptr, TIMEOUT_EVT);
195
196 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100197 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198
Per Liden4323add2006-01-18 00:38:21 +0100199 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200}
201
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500202static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203{
204 k_start_timer(&l_ptr->timer, time);
205}
206
207/**
Per Liden4323add2006-01-18 00:38:21 +0100208 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500209 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900212 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 * Returns pointer to link.
214 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500215struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Allan Stephens37b9c082011-02-28 11:32:27 -0500216 struct tipc_bearer *b_ptr,
Per Liden4323add2006-01-18 00:38:21 +0100217 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500219 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 struct tipc_msg *msg;
221 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500222 char addr_string[16];
223 u32 peer = n_ptr->addr;
224
225 if (n_ptr->link_cnt >= 2) {
226 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400227 pr_err("Attempt to establish third link to %s\n", addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500228 return NULL;
229 }
230
231 if (n_ptr->links[b_ptr->identity]) {
232 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400233 pr_err("Attempt to establish second link on <%s> to %s\n",
234 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500235 return NULL;
236 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700238 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400240 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 return NULL;
242 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
244 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500245 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400246 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 if_name,
250 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400251 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500253 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 l_ptr->checkpoint = 1;
Allan Stephensf882cb72011-04-07 09:43:27 -0400255 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 l_ptr->b_ptr = b_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -0400257 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 l_ptr->state = RESET_UNKNOWN;
259
260 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
261 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000262 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700264 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 msg_set_bearer_id(msg, b_ptr->identity);
266 strcpy((char *)msg_data(msg), if_name);
267
268 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400269 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270
271 link_init_max_pkt(l_ptr);
272
273 l_ptr->next_out_no = 1;
274 INIT_LIST_HEAD(&l_ptr->waiting_ports);
275
276 link_reset_statistics(l_ptr);
277
Allan Stephens37b9c082011-02-28 11:32:27 -0500278 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500280 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
281 (unsigned long)l_ptr);
Florian Westphal945710652007-07-26 00:05:07 -0700282 list_add_tail(&l_ptr->link_list, &b_ptr->links);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500283
284 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 return l_ptr;
287}
288
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900289/**
Per Liden4323add2006-01-18 00:38:21 +0100290 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900292 *
Per Liden4323add2006-01-18 00:38:21 +0100293 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900295 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500297void tipc_link_delete(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298{
299 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400300 pr_err("Attempt to delete non-existent link\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 return;
302 }
303
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900305
Per Liden4323add2006-01-18 00:38:21 +0100306 tipc_node_lock(l_ptr->owner);
307 tipc_link_reset(l_ptr);
308 tipc_node_detach_link(l_ptr->owner, l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500309 tipc_link_purge_queues(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 list_del_init(&l_ptr->link_list);
Per Liden4323add2006-01-18 00:38:21 +0100311 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 k_term_timer(&l_ptr->timer);
313 kfree(l_ptr);
314}
315
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316
317/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900318 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 * @l_ptr: pointer to link
320 * @origport: reference to sending port
321 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900322 *
323 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 * has abated.
325 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500326static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500328 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329
Per Liden4323add2006-01-18 00:38:21 +0100330 spin_lock_bh(&tipc_port_list_lock);
331 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 if (p_ptr) {
333 if (!p_ptr->wakeup)
334 goto exit;
335 if (!list_empty(&p_ptr->wait_list))
336 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500337 p_ptr->congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000338 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
340 l_ptr->stats.link_congs++;
341exit:
Per Liden4323add2006-01-18 00:38:21 +0100342 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 }
Per Liden4323add2006-01-18 00:38:21 +0100344 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 return -ELINKCONG;
346}
347
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500348void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500350 struct tipc_port *p_ptr;
351 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
353
354 if (all)
355 win = 100000;
356 if (win <= 0)
357 return;
Per Liden4323add2006-01-18 00:38:21 +0100358 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 return;
360 if (link_congested(l_ptr))
361 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900362 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 wait_list) {
364 if (win <= 0)
365 break;
366 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500367 spin_lock_bh(p_ptr->lock);
368 p_ptr->congested = 0;
369 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500371 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 }
373
374exit:
Per Liden4323add2006-01-18 00:38:21 +0100375 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376}
377
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 * link_release_outqueue - purge link's outbound message queue
380 * @l_ptr: pointer to link
381 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500382static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383{
Ying Xued77b3832013-12-10 20:45:38 -0800384 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 l_ptr->first_out = NULL;
386 l_ptr->out_queue_size = 0;
387}
388
389/**
Per Liden4323add2006-01-18 00:38:21 +0100390 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 * @l_ptr: pointer to link
392 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500393void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394{
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100395 kfree_skb(l_ptr->reasm_head);
396 l_ptr->reasm_head = NULL;
397 l_ptr->reasm_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398}
399
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900400/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500401 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 * @l_ptr: pointer to link
403 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500404void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405{
Ying Xued77b3832013-12-10 20:45:38 -0800406 kfree_skb_list(l_ptr->oldest_deferred_in);
407 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100408 tipc_link_reset_fragments(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400409 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 l_ptr->proto_msg_queue = NULL;
411}
412
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500413void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 u32 prev_state = l_ptr->state;
416 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700417 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900418
Allan Stephensa686e682008-06-04 17:29:39 -0700419 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420
Allan Stephensa686e682008-06-04 17:29:39 -0700421 /* Link is down, accept any session */
422 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900426
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428
429 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
430 return;
431
Per Liden4323add2006-01-18 00:38:21 +0100432 tipc_node_link_down(l_ptr->owner, l_ptr);
433 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000434
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500435 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 l_ptr->reset_checkpoint = checkpoint;
437 l_ptr->exp_msg_count = START_CHANGEOVER;
438 }
439
440 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400442 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 l_ptr->proto_msg_queue = NULL;
Ying Xued77b3832013-12-10 20:45:38 -0800444 kfree_skb_list(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100446 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447
448 l_ptr->retransm_queue_head = 0;
449 l_ptr->retransm_queue_size = 0;
450 l_ptr->last_out = NULL;
451 l_ptr->first_out = NULL;
452 l_ptr->next_out = NULL;
453 l_ptr->unacked_window = 0;
454 l_ptr->checkpoint = 1;
455 l_ptr->next_out_no = 1;
456 l_ptr->deferred_inqueue_sz = 0;
457 l_ptr->oldest_deferred_in = NULL;
458 l_ptr->newest_deferred_in = NULL;
459 l_ptr->fsm_msg_cnt = 0;
460 l_ptr->stale_count = 0;
461 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462}
463
Ying Xuee0ca2c32014-02-13 17:29:06 -0500464void tipc_link_reset_list(struct tipc_bearer *b_ptr)
465{
466 struct tipc_link *l_ptr;
467
468 list_for_each_entry(l_ptr, &b_ptr->links, link_list) {
469 struct tipc_node *n_ptr = l_ptr->owner;
470
471 spin_lock_bh(&n_ptr->lock);
472 tipc_link_reset(l_ptr);
473 spin_unlock_bh(&n_ptr->lock);
474 }
475}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500477static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478{
Allan Stephens5392d642006-06-25 23:52:50 -0700479 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100480 tipc_node_link_up(l_ptr->owner, l_ptr);
481 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482}
483
484/**
485 * link_state_event - link finite state machine
486 * @l_ptr: pointer to link
487 * @event: state machine event to process
488 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000489static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500491 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 u32 cont_intv = l_ptr->continuity_interval;
493
494 if (!l_ptr->started && (event != STARTING_EVT))
495 return; /* Not yet. */
496
Ying Xue77a7e072013-12-10 20:45:44 -0800497 /* Check whether changeover is going on */
498 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000499 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800501 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503
504 switch (l_ptr->state) {
505 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 switch (event) {
507 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 break;
510 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 if (l_ptr->next_in_no != l_ptr->checkpoint) {
512 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100513 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900514 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100515 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 l_ptr->fsm_msg_cnt++;
517 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900518 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100519 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 l_ptr->fsm_msg_cnt++;
521 }
522 link_set_timer(l_ptr, cont_intv);
523 break;
524 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 l_ptr->state = WORKING_UNKNOWN;
526 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100527 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 l_ptr->fsm_msg_cnt++;
529 link_set_timer(l_ptr, cont_intv / 4);
530 break;
531 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400532 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
533 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100534 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 l_ptr->state = RESET_RESET;
536 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100537 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 l_ptr->fsm_msg_cnt++;
539 link_set_timer(l_ptr, cont_intv);
540 break;
541 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400542 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 }
544 break;
545 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 switch (event) {
547 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 l_ptr->state = WORKING_WORKING;
550 l_ptr->fsm_msg_cnt = 0;
551 link_set_timer(l_ptr, cont_intv);
552 break;
553 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400554 pr_info("%s<%s>, requested by peer while probing\n",
555 link_rst_msg, 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 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 l_ptr->state = WORKING_WORKING;
566 l_ptr->fsm_msg_cnt = 0;
567 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100568 if (tipc_bclink_acks_missing(l_ptr->owner)) {
569 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
570 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 l_ptr->fsm_msg_cnt++;
572 }
573 link_set_timer(l_ptr, cont_intv);
574 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900575 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100576 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 l_ptr->fsm_msg_cnt++;
578 link_set_timer(l_ptr, cont_intv / 4);
579 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400580 pr_warn("%s<%s>, peer not responding\n",
581 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100582 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583 l_ptr->state = RESET_UNKNOWN;
584 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100585 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
586 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 l_ptr->fsm_msg_cnt++;
588 link_set_timer(l_ptr, cont_intv);
589 }
590 break;
591 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400592 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 }
594 break;
595 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 switch (event) {
597 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 break;
599 case ACTIVATE_MSG:
600 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000601 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 l_ptr->state = WORKING_WORKING;
604 l_ptr->fsm_msg_cnt = 0;
605 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100606 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800608 if (l_ptr->owner->working_links == 1)
609 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 link_set_timer(l_ptr, cont_intv);
611 break;
612 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 l_ptr->state = RESET_RESET;
614 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100615 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 l_ptr->fsm_msg_cnt++;
617 link_set_timer(l_ptr, cont_intv);
618 break;
619 case STARTING_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 l_ptr->started = 1;
621 /* fall through */
622 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100623 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->fsm_msg_cnt++;
625 link_set_timer(l_ptr, cont_intv);
626 break;
627 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400628 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 }
630 break;
631 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 switch (event) {
633 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 case ACTIVATE_MSG:
635 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000636 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 l_ptr->state = WORKING_WORKING;
639 l_ptr->fsm_msg_cnt = 0;
640 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100641 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800643 if (l_ptr->owner->working_links == 1)
644 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 link_set_timer(l_ptr, cont_intv);
646 break;
647 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 break;
649 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100650 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 l_ptr->fsm_msg_cnt++;
652 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 break;
654 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400655 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 }
657 break;
658 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400659 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 }
661}
662
663/*
664 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400667static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 struct sk_buff *buf)
669{
670 struct tipc_msg *bundler_msg = buf_msg(bundler);
671 struct tipc_msg *msg = buf_msg(buf);
672 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700673 u32 bundle_size = msg_size(bundler_msg);
674 u32 to_pos = align(bundle_size);
675 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676
677 if (msg_user(bundler_msg) != MSG_BUNDLER)
678 return 0;
679 if (msg_type(bundler_msg) != OPEN_MSG)
680 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700681 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000683 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700684 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685
Allan Stephense49060c2006-06-29 12:32:46 -0700686 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300687 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 msg_set_size(bundler_msg, to_pos + size);
689 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400690 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 l_ptr->stats.sent_bundled++;
692 return 1;
693}
694
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500695static void link_add_to_outqueue(struct tipc_link *l_ptr,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800696 struct sk_buff *buf,
697 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698{
699 u32 ack = mod(l_ptr->next_in_no - 1);
700 u32 seqno = mod(l_ptr->next_out_no++);
701
702 msg_set_word(msg, 2, ((ack << 16) | seqno));
703 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
704 buf->next = NULL;
705 if (l_ptr->first_out) {
706 l_ptr->last_out->next = buf;
707 l_ptr->last_out = buf;
708 } else
709 l_ptr->first_out = l_ptr->last_out = buf;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500710
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 l_ptr->out_queue_size++;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500712 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
713 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714}
715
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500716static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
Allan Stephensdc63d912011-04-21 11:50:42 -0400717 struct sk_buff *buf_chain,
718 u32 long_msgno)
719{
720 struct sk_buff *buf;
721 struct tipc_msg *msg;
722
723 if (!l_ptr->next_out)
724 l_ptr->next_out = buf_chain;
725 while (buf_chain) {
726 buf = buf_chain;
727 buf_chain = buf_chain->next;
728
729 msg = buf_msg(buf);
730 msg_set_long_msgno(msg, long_msgno);
731 link_add_to_outqueue(l_ptr, buf, msg);
732 }
733}
734
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900735/*
736 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 * inside TIPC when the 'fast path' in tipc_send_buf
738 * has failed, and from link_send()
739 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500740int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741{
742 struct tipc_msg *msg = buf_msg(buf);
743 u32 size = msg_size(msg);
744 u32 dsz = msg_data_sz(msg);
745 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000746 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000748 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 /* Match msg importance against queue limits: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 if (unlikely(queue_size >= queue_limit)) {
752 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
Allan Stephensbebc55a2011-04-19 10:17:58 -0400753 link_schedule_port(l_ptr, msg_origport(msg), size);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400754 kfree_skb(buf);
Allan Stephensbebc55a2011-04-19 10:17:58 -0400755 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756 }
Allan Stephens5f6d9122011-11-04 13:24:29 -0400757 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 if (imp > CONN_MANAGER) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400759 pr_warn("%s<%s>, send queue full", link_rst_msg,
760 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100761 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 }
763 return dsz;
764 }
765
766 /* Fragmentation needed ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000768 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400770 /* Packet can be queued or sent. */
Erik Hugne512137e2013-12-06 10:08:00 -0500771 if (likely(!link_congested(l_ptr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 link_add_to_outqueue(l_ptr, buf, msg);
773
Ying Xue3c294cb2012-11-15 11:34:45 +0800774 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
775 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 return dsz;
777 }
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400778 /* Congestion: can message be bundled ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
780 (msg_user(msg) != MSG_FRAGMENTER)) {
781
782 /* Try adding message to an existing bundle */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 if (l_ptr->next_out &&
Ying Xue3c294cb2012-11-15 11:34:45 +0800784 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 return dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786
787 /* Try creating a new bundle */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000789 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 struct tipc_msg bundler_hdr;
791
792 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000793 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700794 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300795 skb_copy_to_linear_data(bundler, &bundler_hdr,
796 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797 skb_trim(bundler, INT_H_SIZE);
798 link_bundle_buf(l_ptr, bundler, buf);
799 buf = bundler;
800 msg = buf_msg(buf);
801 l_ptr->stats.sent_bundles++;
802 }
803 }
804 }
805 if (!l_ptr->next_out)
806 l_ptr->next_out = buf;
807 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 return dsz;
809}
810
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811/*
812 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 * not been selected yet, and the the owner node is not locked
814 * Called by TIPC internal users, e.g. the name distributor
815 */
Per Liden4323add2006-01-18 00:38:21 +0100816int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500818 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700819 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 int res = -ELINKCONG;
821
Per Liden4323add2006-01-18 00:38:21 +0100822 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000823 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100825 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000827 if (l_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100828 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000829 else
Allan Stephens5f6d9122011-11-04 13:24:29 -0400830 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100831 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 } else {
Allan Stephens5f6d9122011-11-04 13:24:29 -0400833 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 }
Per Liden4323add2006-01-18 00:38:21 +0100835 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 return res;
837}
838
Jon Maloyc64f7a62012-11-16 13:51:31 +0800839/*
840 * tipc_link_send_sync - synchronize broadcast link endpoints.
841 *
842 * Give a newly added peer node the sequence number where it should
843 * start receiving and acking broadcast packets.
844 *
845 * Called with node locked
846 */
847static void tipc_link_send_sync(struct tipc_link *l)
848{
849 struct sk_buff *buf;
850 struct tipc_msg *msg;
851
852 buf = tipc_buf_acquire(INT_H_SIZE);
853 if (!buf)
854 return;
855
856 msg = buf_msg(buf);
857 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
858 msg_set_last_bcast(msg, l->owner->bclink.acked);
859 link_add_chain_to_outqueue(l, buf, 0);
860 tipc_link_push_queue(l);
861}
862
863/*
864 * tipc_link_recv_sync - synchronize broadcast link endpoints.
865 * Receive the sequence number where we should start receiving and
866 * acking broadcast packets from a newly added peer node, and open
867 * up for reception of such packets.
868 *
869 * Called with node locked
870 */
871static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
872{
873 struct tipc_msg *msg = buf_msg(buf);
874
875 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
876 n->bclink.recv_permitted = true;
877 kfree_skb(buf);
878}
879
880/*
Allan Stephens9aa88c22011-05-31 13:38:02 -0400881 * tipc_link_send_names - send name table entries to new neighbor
882 *
883 * Send routine for bulk delivery of name table messages when contact
884 * with a new neighbor occurs. No link congestion checking is performed
885 * because name table messages *must* be delivered. The messages must be
886 * small enough not to require fragmentation.
887 * Called without any locks held.
888 */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400889void tipc_link_send_names(struct list_head *message_list, u32 dest)
890{
891 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500892 struct tipc_link *l_ptr;
Allan Stephens9aa88c22011-05-31 13:38:02 -0400893 struct sk_buff *buf;
894 struct sk_buff *temp_buf;
895
896 if (list_empty(message_list))
897 return;
898
899 read_lock_bh(&tipc_net_lock);
900 n_ptr = tipc_node_find(dest);
901 if (n_ptr) {
902 tipc_node_lock(n_ptr);
903 l_ptr = n_ptr->active_links[0];
904 if (l_ptr) {
905 /* convert circular list to linear list */
906 ((struct sk_buff *)message_list->prev)->next = NULL;
907 link_add_chain_to_outqueue(l_ptr,
908 (struct sk_buff *)message_list->next, 0);
909 tipc_link_push_queue(l_ptr);
910 INIT_LIST_HEAD(message_list);
911 }
912 tipc_node_unlock(n_ptr);
913 }
914 read_unlock_bh(&tipc_net_lock);
915
916 /* discard the messages if they couldn't be sent */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400917 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
918 list_del((struct list_head *)buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400919 kfree_skb(buf);
Allan Stephens9aa88c22011-05-31 13:38:02 -0400920 }
921}
922
923/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900924 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 * destination link is known and the header is complete,
926 * inclusive total message length. Very time critical.
927 * Link is locked. Returns user data length.
928 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500929static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800930 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931{
932 struct tipc_msg *msg = buf_msg(buf);
933 int res = msg_data_sz(msg);
934
935 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +0000936 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Erik Hugne512137e2013-12-06 10:08:00 -0500937 link_add_to_outqueue(l_ptr, buf, msg);
938 tipc_bearer_send(l_ptr->b_ptr, buf,
939 &l_ptr->media_addr);
940 l_ptr->unacked_window = 0;
941 return res;
942 }
943 else
Allan Stephens15e979d2010-05-11 14:30:10 +0000944 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 }
Per Liden4323add2006-01-18 00:38:21 +0100946 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947}
948
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900949/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900950 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900952 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953 * Returns user data length or errno.
954 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500955int tipc_link_send_sections_fast(struct tipc_port *sender,
Per Liden4323add2006-01-18 00:38:21 +0100956 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +0200957 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500959 struct tipc_msg *hdr = &sender->phdr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500960 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700962 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 int res;
964 u32 selector = msg_origport(hdr) & 1;
965
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966again:
967 /*
968 * Try building message using port's max_pkt hint.
969 * (Must not hold any locks while building message.)
970 */
Ying Xue9446b872013-10-18 07:23:15 +0200971 res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
Ying Xue7410f962013-06-17 10:54:49 -0400972 /* Exit if build request was invalid */
973 if (unlikely(res < 0))
974 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975
Per Liden4323add2006-01-18 00:38:21 +0100976 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000977 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +0100979 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 l_ptr = node->active_links[selector];
981 if (likely(l_ptr)) {
982 if (likely(buf)) {
983 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500984 &sender->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985exit:
Per Liden4323add2006-01-18 00:38:21 +0100986 tipc_node_unlock(node);
987 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 return res;
989 }
990
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 /* Exit if link (or bearer) is congested */
Erik Hugne512137e2013-12-06 10:08:00 -0500992 if (link_congested(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 res = link_schedule_port(l_ptr,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500994 sender->ref, res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 goto exit;
996 }
997
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900998 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 * Message size exceeds max_pkt hint; update hint,
1000 * then re-try fast path or fragment the message
1001 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001002 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001003 tipc_node_unlock(node);
1004 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005
1006
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001007 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 goto again;
1009
Ying Xue9446b872013-10-18 07:23:15 +02001010 return link_send_sections_long(sender, msg_sect, len,
Allan Stephens26896902011-04-21 10:42:07 -05001011 destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 }
Per Liden4323add2006-01-18 00:38:21 +01001013 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 }
Per Liden4323add2006-01-18 00:38:21 +01001015 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016
1017 /* Couldn't find a link to the destination node */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 if (buf)
1019 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1020 if (res >= 0)
Ying Xue9446b872013-10-18 07:23:15 +02001021 return tipc_port_reject_sections(sender, hdr, msg_sect,
1022 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 return res;
1024}
1025
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026/*
1027 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 * Link and bearer congestion status have been checked to be ok,
1031 * and are ignored if they change.
1032 *
1033 * Note that fragments do not use the full link MTU so that they won't have
1034 * to undergo refragmentation if link changeover causes them to be sent
1035 * over another link with an additional tunnel header added as prefix.
1036 * (Refragmentation will still occur if the other link has a smaller MTU.)
1037 *
1038 * Returns user data length or errno.
1039 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001040static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +02001042 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001044 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001045 struct tipc_node *node;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001046 struct tipc_msg *hdr = &sender->phdr;
Ying Xue9446b872013-10-18 07:23:15 +02001047 u32 dsz = len;
Allan Stephens0e659672010-12-31 18:59:32 +00001048 u32 max_pkt, fragm_sz, rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 struct tipc_msg fragm_hdr;
Allan Stephens0e659672010-12-31 18:59:32 +00001050 struct sk_buff *buf, *buf_chain, *prev;
1051 u32 fragm_crs, fragm_rest, hsz, sect_rest;
Ying Xue40682432013-10-18 07:23:16 +02001052 const unchar __user *sect_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 int curr_sect;
1054 u32 fragm_no;
Ying Xue126c0522013-06-17 10:54:50 -04001055 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056
1057again:
1058 fragm_no = 1;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001059 max_pkt = sender->max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001061 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 /* leave room for fragmentation header in each fragment */
1063 rest = dsz;
1064 fragm_crs = 0;
1065 fragm_rest = 0;
1066 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001067 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 curr_sect = -1;
1069
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001070 /* Prepare reusable fragment header */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001071 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001072 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 msg_set_size(&fragm_hdr, max_pkt);
1074 msg_set_fragm_no(&fragm_hdr, 1);
1075
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001076 /* Prepare header of first fragment */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001077 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 if (!buf)
1079 return -ENOMEM;
1080 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001081 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001083 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001085 /* Chop up message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 fragm_crs = INT_H_SIZE + hsz;
1087 fragm_rest = fragm_sz - hsz;
1088
1089 do { /* For all sections */
1090 u32 sz;
1091
1092 if (!sect_rest) {
1093 sect_rest = msg_sect[++curr_sect].iov_len;
Ying Xue40682432013-10-18 07:23:16 +02001094 sect_crs = msg_sect[curr_sect].iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 }
1096
1097 if (sect_rest < fragm_rest)
1098 sz = sect_rest;
1099 else
1100 sz = fragm_rest;
1101
Ying Xuef1733d72013-06-17 10:54:43 -04001102 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
Ying Xue126c0522013-06-17 10:54:50 -04001103 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104error:
Ying Xued77b3832013-12-10 20:45:38 -08001105 kfree_skb_list(buf_chain);
Ying Xue126c0522013-06-17 10:54:50 -04001106 return res;
Ying Xuef1733d72013-06-17 10:54:43 -04001107 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 sect_crs += sz;
1109 sect_rest -= sz;
1110 fragm_crs += sz;
1111 fragm_rest -= sz;
1112 rest -= sz;
1113
1114 if (!fragm_rest && rest) {
1115
1116 /* Initiate new fragment: */
1117 if (rest <= fragm_sz) {
1118 fragm_sz = rest;
Allan Stephens0e659672010-12-31 18:59:32 +00001119 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 } else {
1121 msg_set_type(&fragm_hdr, FRAGMENT);
1122 }
1123 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1124 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1125 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001126 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Ying Xue126c0522013-06-17 10:54:50 -04001127 if (!buf) {
1128 res = -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 goto error;
Ying Xue126c0522013-06-17 10:54:50 -04001130 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001132 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001134 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 fragm_crs = INT_H_SIZE;
1136 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 }
Allan Stephens0e659672010-12-31 18:59:32 +00001138 } while (rest > 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001140 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 * Now we have a buffer chain. Select a link and check
1142 * that packet size is still OK
1143 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001144 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001146 tipc_node_lock(node);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001147 l_ptr = node->active_links[sender->ref & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001149 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 goto reject;
1151 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001152 if (l_ptr->max_pkt < max_pkt) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001153 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001154 tipc_node_unlock(node);
Ying Xued77b3832013-12-10 20:45:38 -08001155 kfree_skb_list(buf_chain);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 goto again;
1157 }
1158 } else {
1159reject:
Ying Xued77b3832013-12-10 20:45:38 -08001160 kfree_skb_list(buf_chain);
Ying Xue9446b872013-10-18 07:23:15 +02001161 return tipc_port_reject_sections(sender, hdr, msg_sect,
1162 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 }
1164
Allan Stephensdc63d912011-04-21 11:50:42 -04001165 /* Append chain of fragments to send queue & send them */
Allan Stephense0f08592011-04-17 11:44:24 -04001166 l_ptr->long_msg_seq_no++;
Allan Stephensdc63d912011-04-21 11:50:42 -04001167 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
1168 l_ptr->stats.sent_fragments += fragm_no;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 l_ptr->stats.sent_fragmented++;
Per Liden4323add2006-01-18 00:38:21 +01001170 tipc_link_push_queue(l_ptr);
1171 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 return dsz;
1173}
1174
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175/*
Per Liden4323add2006-01-18 00:38:21 +01001176 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 */
stephen hemminger98056962014-01-04 13:47:48 -08001178static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179{
1180 struct sk_buff *buf = l_ptr->first_out;
1181 u32 r_q_size = l_ptr->retransm_queue_size;
1182 u32 r_q_head = l_ptr->retransm_queue_head;
1183
1184 /* Step to position where retransmission failed, if any, */
1185 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001187 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -04001189 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190
1191 while (buf && less(first, r_q_head)) {
1192 first = mod(first + 1);
1193 buf = buf->next;
1194 }
1195 l_ptr->retransm_queue_head = r_q_head = first;
1196 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1197 }
1198
1199 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +00001200 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001202 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001203 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1204 l_ptr->retransm_queue_head = mod(++r_q_head);
1205 l_ptr->retransm_queue_size = --r_q_size;
1206 l_ptr->stats.retransmitted++;
1207 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 }
1209
1210 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 buf = l_ptr->proto_msg_queue;
1212 if (buf) {
1213 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001214 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001215 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1216 l_ptr->unacked_window = 0;
1217 kfree_skb(buf);
1218 l_ptr->proto_msg_queue = NULL;
1219 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 }
1221
1222 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 buf = l_ptr->next_out;
1224 if (buf) {
1225 struct tipc_msg *msg = buf_msg(buf);
1226 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -04001227 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228
1229 if (mod(next - first) < l_ptr->queue_limit[0]) {
1230 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001231 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001232 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1233 if (msg_user(msg) == MSG_BUNDLER)
1234 msg_set_type(msg, CLOSED_MSG);
1235 l_ptr->next_out = buf->next;
1236 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 }
1238 }
Ying Xue3c294cb2012-11-15 11:34:45 +08001239 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240}
1241
1242/*
1243 * push_queue(): push out the unsent messages of a link where
1244 * congestion has abated. Node is locked
1245 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001246void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247{
1248 u32 res;
1249
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 do {
Per Liden4323add2006-01-18 00:38:21 +01001251 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001252 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253}
1254
Allan Stephensd356eeb2006-06-25 23:40:01 -07001255static void link_reset_all(unsigned long addr)
1256{
David S. Miller6c000552008-09-02 23:38:32 -07001257 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001258 char addr_string[16];
1259 u32 i;
1260
1261 read_lock_bh(&tipc_net_lock);
1262 n_ptr = tipc_node_find((u32)addr);
1263 if (!n_ptr) {
1264 read_unlock_bh(&tipc_net_lock);
1265 return; /* node no longer exists */
1266 }
1267
1268 tipc_node_lock(n_ptr);
1269
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001270 pr_warn("Resetting all links to %s\n",
1271 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001272
1273 for (i = 0; i < MAX_BEARERS; i++) {
1274 if (n_ptr->links[i]) {
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001275 link_print(n_ptr->links[i], "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001276 tipc_link_reset(n_ptr->links[i]);
1277 }
1278 }
1279
1280 tipc_node_unlock(n_ptr);
1281 read_unlock_bh(&tipc_net_lock);
1282}
1283
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001284static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001285 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001286{
1287 struct tipc_msg *msg = buf_msg(buf);
1288
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001289 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001290
1291 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001292 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001293 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001294 tipc_link_reset(l_ptr);
1295
1296 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001297 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -07001298 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001299 char addr_string[16];
1300
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001301 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1302 pr_cont("Outstanding acks: %lu\n",
1303 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001304
Allan Stephens01d83ed2011-01-18 13:53:16 -05001305 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -07001306 tipc_node_lock(n_ptr);
1307
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001308 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001309 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +08001310 pr_info("Reception permitted: %d, Acked: %u\n",
1311 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001312 n_ptr->bclink.acked);
1313 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1314 n_ptr->bclink.last_in,
1315 n_ptr->bclink.oos_state,
1316 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001317
1318 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1319
1320 tipc_node_unlock(n_ptr);
1321
1322 l_ptr->stale_count = 0;
1323 }
1324}
1325
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001326void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001327 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328{
1329 struct tipc_msg *msg;
1330
Allan Stephensd356eeb2006-06-25 23:40:01 -07001331 if (!buf)
1332 return;
1333
1334 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001335
Erik Hugne512137e2013-12-06 10:08:00 -05001336 /* Detect repeated retransmit failures */
1337 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1338 if (++l_ptr->stale_count > 100) {
1339 link_retransmit_failure(l_ptr, buf);
1340 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001341 }
1342 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001343 l_ptr->last_retransmitted = msg_seqno(msg);
1344 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001346
Neil Hormanca509102010-03-15 07:58:45 +00001347 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 msg = buf_msg(buf);
1349 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001350 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001351 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1352 buf = buf->next;
1353 retransmits--;
1354 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001356
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1358}
1359
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001360/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 * link_insert_deferred_queue - insert deferred messages back into receive chain
1362 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001363static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 struct sk_buff *buf)
1365{
1366 u32 seq_no;
1367
1368 if (l_ptr->oldest_deferred_in == NULL)
1369 return buf;
1370
Allan Stephensf9057302011-10-24 16:03:12 -04001371 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 if (seq_no == mod(l_ptr->next_in_no)) {
1373 l_ptr->newest_deferred_in->next = buf;
1374 buf = l_ptr->oldest_deferred_in;
1375 l_ptr->oldest_deferred_in = NULL;
1376 l_ptr->deferred_inqueue_sz = 0;
1377 }
1378 return buf;
1379}
1380
Allan Stephens85035562008-04-15 19:04:54 -07001381/**
1382 * link_recv_buf_validate - validate basic format of received message
1383 *
1384 * This routine ensures a TIPC message has an acceptable header, and at least
1385 * as much data as the header indicates it should. The routine also ensures
1386 * that the entire message header is stored in the main fragment of the message
1387 * buffer, to simplify future access to message header fields.
1388 *
1389 * Note: Having extra info present in the message header or data areas is OK.
1390 * TIPC will ignore the excess, under the assumption that it is optional info
1391 * introduced by a later release of the protocol.
1392 */
Allan Stephens85035562008-04-15 19:04:54 -07001393static int link_recv_buf_validate(struct sk_buff *buf)
1394{
1395 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001396 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001397 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1398 };
1399
1400 struct tipc_msg *msg;
1401 u32 tipc_hdr[2];
1402 u32 size;
1403 u32 hdr_size;
1404 u32 min_hdr_size;
1405
1406 if (unlikely(buf->len < MIN_H_SIZE))
1407 return 0;
1408
1409 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1410 if (msg == NULL)
1411 return 0;
1412
1413 if (unlikely(msg_version(msg) != TIPC_VERSION))
1414 return 0;
1415
1416 size = msg_size(msg);
1417 hdr_size = msg_hdr_sz(msg);
1418 min_hdr_size = msg_isdata(msg) ?
1419 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1420
1421 if (unlikely((hdr_size < min_hdr_size) ||
1422 (size < hdr_size) ||
1423 (buf->len < size) ||
1424 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1425 return 0;
1426
1427 return pskb_may_pull(buf, hdr_size);
1428}
1429
Allan Stephensb02b69c2010-08-17 11:00:07 +00001430/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001431 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001432 * @head: pointer to message buffer chain
1433 * @tb_ptr: pointer to bearer message arrived on
1434 *
1435 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1436 * structure (i.e. cannot be NULL), but bearer can be inactive.
1437 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001438void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439{
Per Liden4323add2006-01-18 00:38:21 +01001440 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001442 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001443 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 struct sk_buff *crs;
1445 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001446 struct tipc_msg *msg;
1447 u32 seq_no;
1448 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 u32 released = 0;
1450 int type;
1451
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001453 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001454
Allan Stephensb02b69c2010-08-17 11:00:07 +00001455 /* Ensure bearer is still enabled */
Allan Stephensb02b69c2010-08-17 11:00:07 +00001456 if (unlikely(!b_ptr->active))
Ying Xue3af390e2013-10-30 11:26:57 +08001457 goto discard;
Allan Stephensb02b69c2010-08-17 11:00:07 +00001458
Allan Stephens85035562008-04-15 19:04:54 -07001459 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001460 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001461 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462
Allan Stephensfe13dda2008-04-15 19:03:23 -07001463 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001464 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001465 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001466
Allan Stephens85035562008-04-15 19:04:54 -07001467 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001468 msg = buf_msg(buf);
1469
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001471 if (msg_user(msg) == LINK_CONFIG)
1472 tipc_disc_recv_msg(buf, b_ptr);
1473 else
1474 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 continue;
1476 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001477
Allan Stephensed33a9c2011-04-05 15:15:04 -04001478 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001479 if (unlikely(!msg_short(msg) &&
1480 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001481 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001482
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001483 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001484 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001486 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001487 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001488
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001489 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001491 if (unlikely(!l_ptr))
1492 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001493
Allan Stephensb4b56102011-05-27 11:00:51 -04001494 /* Verify that communication with node is currently allowed */
Allan Stephensb4b56102011-05-27 11:00:51 -04001495 if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
1496 msg_user(msg) == LINK_PROTOCOL &&
1497 (msg_type(msg) == RESET_MSG ||
1498 msg_type(msg) == ACTIVATE_MSG) &&
1499 !msg_redundant_link(msg))
1500 n_ptr->block_setup &= ~WAIT_PEER_DOWN;
1501
Ying Xue3af390e2013-10-30 11:26:57 +08001502 if (n_ptr->block_setup)
1503 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001504
Allan Stephens85035562008-04-15 19:04:54 -07001505 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001506 seq_no = msg_seqno(msg);
1507 ackd = msg_ack(msg);
1508
1509 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001510 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001511 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512
1513 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001514 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001515 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 struct sk_buff *next = crs->next;
1517
Allan Stephens5f6d9122011-11-04 13:24:29 -04001518 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519 crs = next;
1520 released++;
1521 }
1522 if (released) {
1523 l_ptr->first_out = crs;
1524 l_ptr->out_queue_size -= released;
1525 }
Allan Stephens85035562008-04-15 19:04:54 -07001526
1527 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001529 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001531 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1533 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001534 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 }
1536
Allan Stephens85035562008-04-15 19:04:54 -07001537 /* Now (finally!) process the incoming message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538protocol_check:
Ying Xue3af390e2013-10-30 11:26:57 +08001539 if (unlikely(!link_working_working(l_ptr))) {
1540 if (msg_user(msg) == LINK_PROTOCOL) {
1541 link_recv_proto_msg(l_ptr, buf);
1542 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001543 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 continue;
1545 }
Ying Xue3af390e2013-10-30 11:26:57 +08001546
1547 /* Traffic message. Conditionally activate link */
1548 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1549
1550 if (link_working_working(l_ptr)) {
1551 /* Re-insert buffer in front of queue */
1552 buf->next = head;
1553 head = buf;
1554 tipc_node_unlock(n_ptr);
1555 continue;
1556 }
1557 goto unlock_discard;
1558 }
1559
1560 /* Link is now in state WORKING_WORKING */
1561 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 link_handle_out_of_seq_msg(l_ptr, buf);
1563 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001564 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 continue;
1566 }
Ying Xue3af390e2013-10-30 11:26:57 +08001567 l_ptr->next_in_no++;
1568 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 head = link_insert_deferred_queue(l_ptr, head);
Ying Xue3af390e2013-10-30 11:26:57 +08001570deliver:
1571 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001572 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001573 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 continue;
1575 }
Ying Xue3af390e2013-10-30 11:26:57 +08001576 switch (msg_user(msg)) {
1577 int ret;
1578 case MSG_BUNDLER:
1579 l_ptr->stats.recv_bundles++;
1580 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1581 tipc_node_unlock(n_ptr);
1582 tipc_link_recv_bundle(buf);
1583 continue;
1584 case NAME_DISTRIBUTOR:
1585 n_ptr->bclink.recv_permitted = true;
1586 tipc_node_unlock(n_ptr);
1587 tipc_named_recv(buf);
1588 continue;
1589 case BCAST_PROTOCOL:
1590 tipc_link_recv_sync(n_ptr, buf);
Per Liden4323add2006-01-18 00:38:21 +01001591 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001593 case CONN_MANAGER:
1594 tipc_node_unlock(n_ptr);
1595 tipc_port_recv_proto_msg(buf);
1596 continue;
1597 case MSG_FRAGMENTER:
1598 l_ptr->stats.recv_fragments++;
Jon Paul Maloy03b92012014-02-13 17:29:05 -05001599 ret = tipc_link_frag_rcv(&l_ptr->reasm_head,
1600 &l_ptr->reasm_tail,
1601 &buf);
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001602 if (ret == LINK_REASM_COMPLETE) {
Ying Xue3af390e2013-10-30 11:26:57 +08001603 l_ptr->stats.recv_fragmented++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001604 msg = buf_msg(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001605 goto deliver;
1606 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001607 if (ret == LINK_REASM_ERROR)
Erik Hugnea715b492013-11-06 09:28:07 +01001608 tipc_link_reset(l_ptr);
Erik Hugne528f6f42013-11-06 09:28:05 +01001609 tipc_node_unlock(n_ptr);
1610 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001611 case CHANGEOVER_PROTOCOL:
1612 type = msg_type(msg);
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001613 if (tipc_link_tunnel_rcv(&l_ptr, &buf)) {
Ying Xue3af390e2013-10-30 11:26:57 +08001614 msg = buf_msg(buf);
1615 seq_no = msg_seqno(msg);
1616 if (type == ORIGINAL_MSG)
1617 goto deliver;
1618 goto protocol_check;
1619 }
1620 break;
1621 default:
1622 kfree_skb(buf);
1623 buf = NULL;
1624 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001625 }
Per Liden4323add2006-01-18 00:38:21 +01001626 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001627 tipc_net_route_msg(buf);
1628 continue;
1629unlock_discard:
1630
1631 tipc_node_unlock(n_ptr);
1632discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001633 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634 }
Per Liden4323add2006-01-18 00:38:21 +01001635 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636}
1637
Ben Hutchings2c530402012-07-10 10:55:09 +00001638/**
Allan Stephens8809b252011-10-25 10:44:35 -04001639 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1640 *
1641 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 */
Allan Stephens8809b252011-10-25 10:44:35 -04001643u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001644 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645{
Allan Stephens8809b252011-10-25 10:44:35 -04001646 struct sk_buff *queue_buf;
1647 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001648 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001649
1650 buf->next = NULL;
1651
1652 /* Empty queue ? */
1653 if (*head == NULL) {
1654 *head = *tail = buf;
1655 return 1;
1656 }
1657
1658 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001659 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 (*tail)->next = buf;
1661 *tail = buf;
1662 return 1;
1663 }
1664
Allan Stephens8809b252011-10-25 10:44:35 -04001665 /* Locate insertion point in queue, then insert; discard if duplicate */
1666 prev = head;
1667 queue_buf = *head;
1668 for (;;) {
1669 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670
Allan Stephens8809b252011-10-25 10:44:35 -04001671 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001672 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001673 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 }
Allan Stephens8809b252011-10-25 10:44:35 -04001675
1676 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678
Allan Stephens8809b252011-10-25 10:44:35 -04001679 prev = &queue_buf->next;
1680 queue_buf = queue_buf->next;
1681 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682
Allan Stephens8809b252011-10-25 10:44:35 -04001683 buf->next = queue_buf;
1684 *prev = buf;
1685 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686}
1687
Allan Stephens8809b252011-10-25 10:44:35 -04001688/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1690 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001691static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 struct sk_buff *buf)
1693{
Allan Stephensf9057302011-10-24 16:03:12 -04001694 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695
1696 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1697 link_recv_proto_msg(l_ptr, buf);
1698 return;
1699 }
1700
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 l_ptr->checkpoint--;
1703
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001704 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 * Discard packet if a duplicate; otherwise add it to deferred queue
1706 * and notify peer of gap as per protocol specification
1707 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 if (less(seq_no, mod(l_ptr->next_in_no))) {
1709 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001710 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 return;
1712 }
1713
Per Liden4323add2006-01-18 00:38:21 +01001714 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1715 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 l_ptr->deferred_inqueue_sz++;
1717 l_ptr->stats.deferred_recv++;
1718 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01001719 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 } else
1721 l_ptr->stats.duplicates++;
1722}
1723
1724/*
1725 * Send protocol message to the other endpoint.
1726 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001727void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001728 int probe_msg, u32 gap, u32 tolerance,
1729 u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001731 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001733 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001734 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735
Allan Stephens92d2c902011-10-25 11:20:26 -04001736 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001737 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001738 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001739 l_ptr->proto_msg_queue = NULL;
1740 }
1741
Ying Xue77a7e072013-12-10 20:45:44 -08001742 /* Don't send protocol message during link changeover */
1743 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001745
1746 /* Abort non-RESET send if communication with node is prohibited */
Allan Stephensb4b56102011-05-27 11:00:51 -04001747 if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
1748 return;
1749
Allan Stephens92d2c902011-10-25 11:20:26 -04001750 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751 msg_set_type(msg, msg_typ);
1752 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001753 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001754 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755
1756 if (msg_typ == STATE_MSG) {
1757 u32 next_sent = mod(l_ptr->next_out_no);
1758
Per Liden4323add2006-01-18 00:38:21 +01001759 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 return;
1761 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001762 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763 msg_set_next_sent(msg, next_sent);
1764 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001765 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001766 gap = mod(rec - mod(l_ptr->next_in_no));
1767 }
1768 msg_set_seq_gap(msg, gap);
1769 if (gap)
1770 l_ptr->stats.sent_nacks++;
1771 msg_set_link_tolerance(msg, tolerance);
1772 msg_set_linkprio(msg, priority);
1773 msg_set_max_pkt(msg, ack_mtu);
1774 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1775 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001776 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 u32 mtu = l_ptr->max_pkt;
1778
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001779 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 link_working_working(l_ptr) &&
1781 l_ptr->fsm_msg_cnt) {
1782 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001783 if (l_ptr->max_pkt_probes == 10) {
1784 l_ptr->max_pkt_target = (msg_size - 4);
1785 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001787 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001789 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790
1791 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001792 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 l_ptr->stats.sent_states++;
1794 } else { /* RESET_MSG or ACTIVATE_MSG */
1795 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1796 msg_set_seq_gap(msg, 0);
1797 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001798 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001799 msg_set_link_tolerance(msg, l_ptr->tolerance);
1800 msg_set_linkprio(msg, l_ptr->priority);
1801 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1802 }
1803
Allan Stephens75f0aa42011-02-28 15:30:20 -05001804 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1805 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001807 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808
1809 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1810
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001811 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 if (!buf)
1813 return;
1814
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001815 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001816 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817
Ying Xue3c294cb2012-11-15 11:34:45 +08001818 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001819 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001820 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821}
1822
1823/*
1824 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001825 * Note that network plane id propagates through the network, and may
1826 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001828static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829{
1830 u32 rec_gap = 0;
1831 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001832 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 u32 msg_tol;
1834 struct tipc_msg *msg = buf_msg(buf);
1835
Ying Xue77a7e072013-12-10 20:45:44 -08001836 /* Discard protocol message during link changeover */
1837 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838 goto exit;
1839
1840 /* record unnumbered packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 l_ptr->checkpoint--;
1842
1843 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
1844 if (tipc_own_addr > msg_prevnode(msg))
1845 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
1846
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001848
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001850 if (!link_working_unknown(l_ptr) &&
1851 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001852 if (less_eq(msg_session(msg), l_ptr->peer_session))
1853 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001855
1856 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1857 link_working_unknown(l_ptr))) {
1858 /*
1859 * peer has lost contact -- don't allow peer's links
1860 * to reactivate before we recognize loss & clean up
1861 */
1862 l_ptr->owner->block_setup = WAIT_NODE_DOWN;
1863 }
1864
Allan Stephens47361c82011-10-26 10:55:16 -04001865 link_state_event(l_ptr, RESET_MSG);
1866
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 /* fall thru' */
1868 case ACTIVATE_MSG:
1869 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1871
Allan Stephens2db99832010-12-31 18:59:33 +00001872 msg_tol = msg_link_tolerance(msg);
1873 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 link_set_supervision_props(l_ptr, msg_tol);
1875
1876 if (msg_linkprio(msg) > l_ptr->priority)
1877 l_ptr->priority = msg_linkprio(msg);
1878
1879 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001880 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 if (max_pkt_info < l_ptr->max_pkt_target)
1882 l_ptr->max_pkt_target = max_pkt_info;
1883 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1884 l_ptr->max_pkt = l_ptr->max_pkt_target;
1885 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001886 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888
Allan Stephens4d753132011-10-25 12:19:05 -04001889 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001890 if (!tipc_node_is_up(l_ptr->owner)) {
1891 l_ptr->owner->bclink.last_sent =
1892 l_ptr->owner->bclink.last_in =
1893 msg_last_bcast(msg);
1894 l_ptr->owner->bclink.oos_state = 0;
1895 }
Allan Stephens4d753132011-10-25 12:19:05 -04001896
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 l_ptr->peer_session = msg_session(msg);
1898 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001899
1900 if (msg_type(msg) == ACTIVATE_MSG)
1901 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902 break;
1903 case STATE_MSG:
1904
Allan Stephens2db99832010-12-31 18:59:33 +00001905 msg_tol = msg_link_tolerance(msg);
1906 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001908
1909 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001911 pr_warn("%s<%s>, priority change %u->%u\n",
1912 link_rst_msg, l_ptr->name, l_ptr->priority,
1913 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001915 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 break;
1917 }
1918 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1919 l_ptr->stats.recv_states++;
1920 if (link_reset_unknown(l_ptr))
1921 break;
1922
1923 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001924 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 mod(l_ptr->next_in_no));
1926 }
1927
1928 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001929 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001930 l_ptr->max_pkt = max_pkt_ack;
1931 l_ptr->max_pkt_probes = 0;
1932 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933
1934 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001935 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001937 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001938 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001939 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940
1941 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001942 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001943 tipc_bclink_update_link_state(l_ptr->owner,
1944 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945
1946 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001947 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
1948 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949 }
1950 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001952 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1953 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954 }
1955 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001956 }
1957exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001958 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959}
1960
1961
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001962/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1963 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001965static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1966 struct tipc_msg *tunnel_hdr,
1967 struct tipc_msg *msg,
1968 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001969{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001970 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 struct sk_buff *buf;
1972 u32 length = msg_size(msg);
1973
1974 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001975 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001976 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001978 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001980 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001981 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001982 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001984 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001985 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1986 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01001987 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988}
1989
1990
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001991/* tipc_link_failover_send_queue(): A link has gone down, but a second
1992 * link is still active. We can do failover. Tunnel the failing link's
1993 * whole send queue via the remaining link. This way, we don't lose
1994 * any packets, and sequence order is preserved for subsequent traffic
1995 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001996 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001997void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998{
1999 u32 msgcount = l_ptr->out_queue_size;
2000 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002001 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002002 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002003 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004
2005 if (!tunnel)
2006 return;
2007
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002008 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002009 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002010 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2011 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002012
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013 if (!l_ptr->first_out) {
2014 struct sk_buff *buf;
2015
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002016 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002017 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002018 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002019 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002020 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002022 pr_warn("%sunable to send changeover msg\n",
2023 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002024 }
2025 return;
2026 }
Allan Stephensf1310722006-06-25 23:51:37 -07002027
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002028 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002029 l_ptr->owner->active_links[1]);
2030
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031 while (crs) {
2032 struct tipc_msg *msg = buf_msg(crs);
2033
2034 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002035 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00002036 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037
Florian Westphald788d802007-08-02 19:28:06 -07002038 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00002040 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002041 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
2042 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002043 pos += align(msg_size(m));
2044 m = (struct tipc_msg *)pos;
2045 }
2046 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002047 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
2048 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002049 }
2050 crs = crs->next;
2051 }
2052}
2053
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002054/* tipc_link_dup_send_queue(): A second link has become active. Tunnel a
2055 * duplicate of the first link's send queue via the new link. This way, we
2056 * are guaranteed that currently queued packets from a socket are delivered
2057 * before future traffic from the same socket, even if this is using the
2058 * new link. The last arriving copy of each duplicate packet is dropped at
2059 * the receiving end by the regular protocol check, so packet cardinality
2060 * and sequence order is preserved per sender/receiver socket pair.
2061 * Owner node is locked.
2062 */
2063void tipc_link_dup_send_queue(struct tipc_link *l_ptr,
2064 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065{
2066 struct sk_buff *iter;
2067 struct tipc_msg tunnel_hdr;
2068
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002069 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002070 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2072 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2073 iter = l_ptr->first_out;
2074 while (iter) {
2075 struct sk_buff *outbuf;
2076 struct tipc_msg *msg = buf_msg(iter);
2077 u32 length = msg_size(msg);
2078
2079 if (msg_user(msg) == MSG_BUNDLER)
2080 msg_set_type(msg, CLOSED_MSG);
2081 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002082 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002084 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002085 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002086 pr_warn("%sunable to send duplicate msg\n",
2087 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002088 return;
2089 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002090 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2091 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2092 length);
Per Liden4323add2006-01-18 00:38:21 +01002093 tipc_link_send_buf(tunnel, outbuf);
2094 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 return;
2096 iter = iter->next;
2097 }
2098}
2099
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100/**
2101 * buf_extract - extracts embedded TIPC message from another message
2102 * @skb: encapsulating message buffer
2103 * @from_pos: offset to extract from
2104 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002105 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 * encapsulating message itself is left unchanged.
2107 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2109{
2110 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2111 u32 size = msg_size(msg);
2112 struct sk_buff *eb;
2113
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002114 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002116 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 return eb;
2118}
2119
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002120/* tipc_link_tunnel_rcv(): Receive a tunneled packet, sent
2121 * via other link as result of a failover (ORIGINAL_MSG) or
2122 * a new active link (DUPLICATE_MSG). Failover packets are
2123 * returned to the active link for delivery upwards.
2124 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002126static int tipc_link_tunnel_rcv(struct tipc_link **l_ptr,
2127 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128{
2129 struct sk_buff *tunnel_buf = *buf;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002130 struct tipc_link *dest_link;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 struct tipc_msg *msg;
2132 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2133 u32 msg_typ = msg_type(tunnel_msg);
2134 u32 msg_count = msg_msgcnt(tunnel_msg);
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002135 u32 bearer_id = msg_bearer_id(tunnel_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002137 if (bearer_id >= MAX_BEARERS)
2138 goto exit;
2139 dest_link = (*l_ptr)->owner->links[bearer_id];
Allan Stephensb29f1422010-12-31 18:59:25 +00002140 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002142 if (dest_link == *l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002143 pr_err("Unexpected changeover message on link <%s>\n",
2144 (*l_ptr)->name);
Allan Stephensf1310722006-06-25 23:51:37 -07002145 goto exit;
2146 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 *l_ptr = dest_link;
2148 msg = msg_get_wrapped(tunnel_msg);
2149
2150 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002151 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 goto exit;
Allan Stephens0e659672010-12-31 18:59:32 +00002153 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002154 if (*buf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002155 pr_warn("%sduplicate msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002156 goto exit;
2157 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002158 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159 return 1;
2160 }
2161
2162 /* First original message ?: */
Per Liden4323add2006-01-18 00:38:21 +01002163 if (tipc_link_is_up(dest_link)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002164 pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
2165 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002166 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167 dest_link->exp_msg_count = msg_count;
2168 if (!msg_count)
2169 goto exit;
2170 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171 dest_link->exp_msg_count = msg_count;
2172 if (!msg_count)
2173 goto exit;
2174 }
2175
2176 /* Receive original message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002177 if (dest_link->exp_msg_count == 0) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002178 pr_warn("%sgot too many tunnelled messages\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179 goto exit;
2180 }
2181 dest_link->exp_msg_count--;
2182 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002183 goto exit;
2184 } else {
2185 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2186 if (*buf != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002187 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 return 1;
2189 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002190 pr_warn("%soriginal msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002191 }
2192 }
2193exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002194 *buf = NULL;
Allan Stephens5f6d9122011-11-04 13:24:29 -04002195 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 return 0;
2197}
2198
2199/*
2200 * Bundler functionality:
2201 */
Per Liden4323add2006-01-18 00:38:21 +01002202void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203{
2204 u32 msgcount = msg_msgcnt(buf_msg(buf));
2205 u32 pos = INT_H_SIZE;
2206 struct sk_buff *obuf;
2207
Per Lidenb97bf3f2006-01-02 19:04:38 +01002208 while (msgcount--) {
2209 obuf = buf_extract(buf, pos);
2210 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002211 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07002212 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002213 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002215 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002216 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002217 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002218}
2219
2220/*
2221 * Fragmentation/defragmentation:
2222 */
2223
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002224/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002225 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002226 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002227 * Returns user data length.
2228 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002229static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002230{
Allan Stephens77561552011-04-17 13:06:23 -04002231 struct sk_buff *buf_chain = NULL;
2232 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002233 struct tipc_msg *inmsg = buf_msg(buf);
2234 struct tipc_msg fragm_hdr;
2235 u32 insize = msg_size(inmsg);
2236 u32 dsz = msg_data_sz(inmsg);
2237 unchar *crs = buf->data;
2238 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002239 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240 u32 fragm_sz = pack_sz - INT_H_SIZE;
Allan Stephens77561552011-04-17 13:06:23 -04002241 u32 fragm_no = 0;
Allan Stephens9c396a72008-06-04 17:36:58 -07002242 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002243
2244 if (msg_short(inmsg))
2245 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002246 else
2247 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002248
Per Lidenb97bf3f2006-01-02 19:04:38 +01002249 /* Prepare reusable fragment header: */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002250 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002251 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002252
2253 /* Chop up message: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002254 while (rest > 0) {
2255 struct sk_buff *fragm;
2256
2257 if (rest <= fragm_sz) {
2258 fragm_sz = rest;
2259 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2260 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002261 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002262 if (fragm == NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002263 kfree_skb(buf);
Ying Xued77b3832013-12-10 20:45:38 -08002264 kfree_skb_list(buf_chain);
Allan Stephens77561552011-04-17 13:06:23 -04002265 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002266 }
2267 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Allan Stephens77561552011-04-17 13:06:23 -04002268 fragm_no++;
2269 msg_set_fragm_no(&fragm_hdr, fragm_no);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002270 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2271 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2272 fragm_sz);
Allan Stephens77561552011-04-17 13:06:23 -04002273 buf_chain_tail->next = fragm;
2274 buf_chain_tail = fragm;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002275
Per Lidenb97bf3f2006-01-02 19:04:38 +01002276 rest -= fragm_sz;
2277 crs += fragm_sz;
2278 msg_set_type(&fragm_hdr, FRAGMENT);
2279 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002280 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002281
2282 /* Append chain of fragments to send queue & send them */
Allan Stephens77561552011-04-17 13:06:23 -04002283 l_ptr->long_msg_seq_no++;
2284 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2285 l_ptr->stats.sent_fragments += fragm_no;
2286 l_ptr->stats.sent_fragmented++;
2287 tipc_link_push_queue(l_ptr);
2288
Per Lidenb97bf3f2006-01-02 19:04:38 +01002289 return dsz;
2290}
2291
Jon Paul Maloy03b92012014-02-13 17:29:05 -05002292/* tipc_link_frag_rcv(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002293 * the reassembled buffer if message is complete.
2294 */
Jon Paul Maloy03b92012014-02-13 17:29:05 -05002295int tipc_link_frag_rcv(struct sk_buff **head, struct sk_buff **tail,
2296 struct sk_buff **fbuf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002297{
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002298 struct sk_buff *frag = *fbuf;
2299 struct tipc_msg *msg = buf_msg(frag);
2300 u32 fragid = msg_type(msg);
2301 bool headstolen;
2302 int delta;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002303
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002304 skb_pull(frag, msg_hdr_sz(msg));
2305 if (fragid == FIRST_FRAGMENT) {
2306 if (*head || skb_unclone(frag, GFP_ATOMIC))
2307 goto out_free;
2308 *head = frag;
2309 skb_frag_list_init(*head);
Jon Paul Maloy03b92012014-02-13 17:29:05 -05002310 *fbuf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311 return 0;
Erik Hugne3db0a192013-11-13 09:35:11 +01002312 } else if (*head &&
2313 skb_try_coalesce(*head, frag, &headstolen, &delta)) {
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002314 kfree_skb_partial(frag, headstolen);
2315 } else {
2316 if (!*head)
2317 goto out_free;
2318 if (!skb_has_frag_list(*head))
2319 skb_shinfo(*head)->frag_list = frag;
2320 else
2321 (*tail)->next = frag;
2322 *tail = frag;
2323 (*head)->truesize += frag->truesize;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002324 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002325 if (fragid == LAST_FRAGMENT) {
2326 *fbuf = *head;
2327 *tail = *head = NULL;
2328 return LINK_REASM_COMPLETE;
2329 }
Jon Paul Maloy03b92012014-02-13 17:29:05 -05002330 *fbuf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002331 return 0;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002332out_free:
2333 pr_warn_ratelimited("Link unable to reassemble fragmented message\n");
2334 kfree_skb(*fbuf);
Jon Paul Maloy03b92012014-02-13 17:29:05 -05002335 *fbuf = NULL;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002336 return LINK_REASM_ERROR;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337}
2338
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002339static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340{
Allan Stephens5413b4c2011-01-18 13:24:55 -05002341 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2342 return;
2343
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344 l_ptr->tolerance = tolerance;
2345 l_ptr->continuity_interval =
2346 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2347 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2348}
2349
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002350void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002351{
2352 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002353 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2354 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2355 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2356 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002357 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002358 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2359 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2360 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2361 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2364 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2365 /* FRAGMENT and LAST_FRAGMENT packets */
2366 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2367}
2368
2369/**
2370 * link_find_link - locate link by name
Ben Hutchings2c530402012-07-10 10:55:09 +00002371 * @name: ptr to link name string
2372 * @node: ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002373 *
Per Liden4323add2006-01-18 00:38:21 +01002374 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002376 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002377 * Returns pointer to link (or 0 if invalid link name).
2378 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002379static struct tipc_link *link_find_link(const char *name,
2380 struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002382 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002383 struct tipc_node *n_ptr;
2384 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385
Erik Hugnebbfbe472013-10-18 07:23:21 +02002386 list_for_each_entry(n_ptr, &tipc_node_list, list) {
2387 for (i = 0; i < MAX_BEARERS; i++) {
2388 l_ptr = n_ptr->links[i];
2389 if (l_ptr && !strcmp(l_ptr->name, name))
2390 goto found;
2391 }
2392 }
2393 l_ptr = NULL;
2394 n_ptr = NULL;
2395found:
2396 *node = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002397 return l_ptr;
2398}
2399
Allan Stephens5c216e12011-10-18 11:34:29 -04002400/**
2401 * link_value_is_valid -- validate proposed link tolerance/priority/window
2402 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002403 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2404 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002405 *
2406 * Returns 1 if value is within range, 0 if not.
2407 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002408static int link_value_is_valid(u16 cmd, u32 new_value)
2409{
2410 switch (cmd) {
2411 case TIPC_CMD_SET_LINK_TOL:
2412 return (new_value >= TIPC_MIN_LINK_TOL) &&
2413 (new_value <= TIPC_MAX_LINK_TOL);
2414 case TIPC_CMD_SET_LINK_PRI:
2415 return (new_value <= TIPC_MAX_LINK_PRI);
2416 case TIPC_CMD_SET_LINK_WINDOW:
2417 return (new_value >= TIPC_MIN_LINK_WIN) &&
2418 (new_value <= TIPC_MAX_LINK_WIN);
2419 }
2420 return 0;
2421}
2422
Allan Stephens5c216e12011-10-18 11:34:29 -04002423/**
2424 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002425 * @name: ptr to link, bearer, or media name
2426 * @new_value: new value of link, bearer, or media setting
2427 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002428 *
2429 * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
2430 *
2431 * Returns 0 if value updated and negative value on error.
2432 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002433static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2434{
2435 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002436 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002437 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002438 struct tipc_media *m_ptr;
Ying Xue636c0372013-10-18 07:23:20 +02002439 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002440
2441 l_ptr = link_find_link(name, &node);
2442 if (l_ptr) {
2443 /*
2444 * acquire node lock for tipc_link_send_proto_msg().
2445 * see "TIPC locking policy" in net.c.
2446 */
2447 tipc_node_lock(node);
2448 switch (cmd) {
2449 case TIPC_CMD_SET_LINK_TOL:
2450 link_set_supervision_props(l_ptr, new_value);
2451 tipc_link_send_proto_msg(l_ptr,
2452 STATE_MSG, 0, 0, new_value, 0, 0);
2453 break;
2454 case TIPC_CMD_SET_LINK_PRI:
2455 l_ptr->priority = new_value;
2456 tipc_link_send_proto_msg(l_ptr,
2457 STATE_MSG, 0, 0, 0, new_value, 0);
2458 break;
2459 case TIPC_CMD_SET_LINK_WINDOW:
2460 tipc_link_set_queue_limits(l_ptr, new_value);
2461 break;
Ying Xue636c0372013-10-18 07:23:20 +02002462 default:
2463 res = -EINVAL;
2464 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002465 }
2466 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002467 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002468 }
2469
2470 b_ptr = tipc_bearer_find(name);
2471 if (b_ptr) {
2472 switch (cmd) {
2473 case TIPC_CMD_SET_LINK_TOL:
2474 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002475 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002476 case TIPC_CMD_SET_LINK_PRI:
2477 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002478 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002479 case TIPC_CMD_SET_LINK_WINDOW:
2480 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002481 break;
2482 default:
2483 res = -EINVAL;
2484 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002485 }
Ying Xue636c0372013-10-18 07:23:20 +02002486 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002487 }
2488
2489 m_ptr = tipc_media_find(name);
2490 if (!m_ptr)
2491 return -ENODEV;
2492 switch (cmd) {
2493 case TIPC_CMD_SET_LINK_TOL:
2494 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002495 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002496 case TIPC_CMD_SET_LINK_PRI:
2497 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002498 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002499 case TIPC_CMD_SET_LINK_WINDOW:
2500 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002501 break;
2502 default:
2503 res = -EINVAL;
2504 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002505 }
Ying Xue636c0372013-10-18 07:23:20 +02002506 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002507}
2508
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002509struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002510 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002511{
2512 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002513 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002514 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002515
2516 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002517 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002518
2519 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2520 new_value = ntohl(args->value);
2521
Allan Stephens5c216e12011-10-18 11:34:29 -04002522 if (!link_value_is_valid(cmd, new_value))
2523 return tipc_cfg_reply_error_string(
2524 "cannot change, value invalid");
2525
Per Liden4323add2006-01-18 00:38:21 +01002526 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002528 (tipc_bclink_set_queue_limits(new_value) == 0))
2529 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002530 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002531 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002532 }
2533
Per Liden4323add2006-01-18 00:38:21 +01002534 read_lock_bh(&tipc_net_lock);
Allan Stephens5c216e12011-10-18 11:34:29 -04002535 res = link_cmd_set_value(args->name, new_value, cmd);
Per Liden4323add2006-01-18 00:38:21 +01002536 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002537 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002538 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002539
Per Liden4323add2006-01-18 00:38:21 +01002540 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002541}
2542
2543/**
2544 * link_reset_statistics - reset link statistics
2545 * @l_ptr: pointer to link
2546 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002547static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002548{
2549 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2550 l_ptr->stats.sent_info = l_ptr->next_out_no;
2551 l_ptr->stats.recv_info = l_ptr->next_in_no;
2552}
2553
Per Liden4323add2006-01-18 00:38:21 +01002554struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002555{
2556 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002557 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002558 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559
2560 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002561 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562
2563 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002564 if (!strcmp(link_name, tipc_bclink_name)) {
2565 if (tipc_bclink_reset_stats())
2566 return tipc_cfg_reply_error_string("link not found");
2567 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002568 }
2569
Per Liden4323add2006-01-18 00:38:21 +01002570 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002571 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002573 read_unlock_bh(&tipc_net_lock);
2574 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002575 }
2576
Per Liden4323add2006-01-18 00:38:21 +01002577 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002579 tipc_node_unlock(node);
2580 read_unlock_bh(&tipc_net_lock);
2581 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002582}
2583
2584/**
2585 * percent - convert count to a percentage of total (rounding up or down)
2586 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002587static u32 percent(u32 count, u32 total)
2588{
2589 return (count * 100 + (total / 2)) / total;
2590}
2591
2592/**
Per Liden4323add2006-01-18 00:38:21 +01002593 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594 * @name: link name
2595 * @buf: print buffer area
2596 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002597 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002598 * Returns length of print buffer data string (or 0 if error)
2599 */
Per Liden4323add2006-01-18 00:38:21 +01002600static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002601{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002602 struct tipc_link *l;
2603 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002604 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 char *status;
2606 u32 profile_total = 0;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002607 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608
Per Liden4323add2006-01-18 00:38:21 +01002609 if (!strcmp(name, tipc_bclink_name))
2610 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611
Per Liden4323add2006-01-18 00:38:21 +01002612 read_lock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002613 l = link_find_link(name, &node);
2614 if (!l) {
Per Liden4323add2006-01-18 00:38:21 +01002615 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616 return 0;
2617 }
Per Liden4323add2006-01-18 00:38:21 +01002618 tipc_node_lock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002619 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002620
Erik Hugnedc1aed32012-06-29 00:50:23 -04002621 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002623 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002624 status = "STANDBY";
2625 else
2626 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002627
2628 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2629 " %s MTU:%u Priority:%u Tolerance:%u ms"
2630 " Window:%u packets\n",
2631 l->name, status, l->max_pkt, l->priority,
2632 l->tolerance, l->queue_limit[0]);
2633
2634 ret += tipc_snprintf(buf + ret, buf_size - ret,
2635 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2636 l->next_in_no - s->recv_info, s->recv_fragments,
2637 s->recv_fragmented, s->recv_bundles,
2638 s->recv_bundled);
2639
2640 ret += tipc_snprintf(buf + ret, buf_size - ret,
2641 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2642 l->next_out_no - s->sent_info, s->sent_fragments,
2643 s->sent_fragmented, s->sent_bundles,
2644 s->sent_bundled);
2645
2646 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002647 if (!profile_total)
2648 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002649
2650 ret += tipc_snprintf(buf + ret, buf_size - ret,
2651 " TX profile sample:%u packets average:%u octets\n"
2652 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2653 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2654 s->msg_length_counts,
2655 s->msg_lengths_total / profile_total,
2656 percent(s->msg_length_profile[0], profile_total),
2657 percent(s->msg_length_profile[1], profile_total),
2658 percent(s->msg_length_profile[2], profile_total),
2659 percent(s->msg_length_profile[3], profile_total),
2660 percent(s->msg_length_profile[4], profile_total),
2661 percent(s->msg_length_profile[5], profile_total),
2662 percent(s->msg_length_profile[6], profile_total));
2663
2664 ret += tipc_snprintf(buf + ret, buf_size - ret,
2665 " RX states:%u probes:%u naks:%u defs:%u"
2666 " dups:%u\n", s->recv_states, s->recv_probes,
2667 s->recv_nacks, s->deferred_recv, s->duplicates);
2668
2669 ret += tipc_snprintf(buf + ret, buf_size - ret,
2670 " TX states:%u probes:%u naks:%u acks:%u"
2671 " dups:%u\n", s->sent_states, s->sent_probes,
2672 s->sent_nacks, s->sent_acks, s->retransmitted);
2673
2674 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002675 " Congestion link:%u Send queue"
2676 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002677 s->max_queue_sz, s->queue_sz_counts ?
2678 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002679
Per Liden4323add2006-01-18 00:38:21 +01002680 tipc_node_unlock(node);
2681 read_unlock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002682 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002683}
2684
Per Liden4323add2006-01-18 00:38:21 +01002685struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002686{
2687 struct sk_buff *buf;
2688 struct tlv_desc *rep_tlv;
2689 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002690 int pb_len;
2691 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002692
2693 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002694 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002695
Erik Hugnedc1aed32012-06-29 00:50:23 -04002696 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002697 if (!buf)
2698 return NULL;
2699
2700 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002701 pb = TLV_DATA(rep_tlv);
2702 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002703 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002704 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002705 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002706 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002707 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002708 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002709 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710 skb_put(buf, TLV_SPACE(str_len));
2711 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2712
2713 return buf;
2714}
2715
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716/**
Per Liden4323add2006-01-18 00:38:21 +01002717 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002718 * @dest: network address of destination node
2719 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002720 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002721 * If no active link can be found, uses default maximum packet size.
2722 */
Per Liden4323add2006-01-18 00:38:21 +01002723u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002724{
David S. Miller6c000552008-09-02 23:38:32 -07002725 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002726 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002727 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002728
Per Lidenb97bf3f2006-01-02 19:04:38 +01002729 if (dest == tipc_own_addr)
2730 return MAX_MSG_SIZE;
2731
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002732 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002733 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002734 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002735 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002736 l_ptr = n_ptr->active_links[selector & 1];
2737 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002738 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002739 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002740 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002741 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002742 return res;
2743}
2744
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002745static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002746{
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002747 pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002748
Per Lidenb97bf3f2006-01-02 19:04:38 +01002749 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002750 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002751 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002752 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002753 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002754 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002755 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002756 pr_cont(":WW\n");
2757 else
2758 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002759}