blob: bb48b96850201e6575474e942fbf7d503e39c662 [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);
90static void link_start(struct tipc_link *l_ptr);
91static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +080092static void tipc_link_send_sync(struct tipc_link *l);
93static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +000094
Per Lidenb97bf3f2006-01-02 19:04:38 +010095/*
Sam Ravnborg05790c62006-03-20 22:37:04 -080096 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080098static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +010099{
100 return (i + 3) & ~3u;
101}
102
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500103static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104{
105 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900106
Allan Stephens2d627b92011-01-07 13:00:11 -0500107 max_pkt = (l_ptr->b_ptr->mtu & ~3);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108 if (max_pkt > MAX_MSG_SIZE)
109 max_pkt = MAX_MSG_SIZE;
110
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900111 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
113 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900114 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 l_ptr->max_pkt = MAX_PKT_DEFAULT;
116
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900117 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118}
119
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500120static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121{
122 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400123 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 return mod(l_ptr->next_out_no);
125}
126
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500127static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128{
129 return mod(link_next_sent(l_ptr) - 1);
130}
131
132/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800133 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500135int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136{
137 if (!l_ptr)
138 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000139 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140}
141
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500142int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000144 return (l_ptr->owner->active_links[0] == l_ptr) ||
145 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146}
147
148/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 * link_timeout - handle expiration of link timer
150 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900151 *
Per Liden4323add2006-01-18 00:38:21 +0100152 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
153 * with tipc_link_delete(). (There is no risk that the node will be deleted by
154 * another thread because tipc_link_delete() always cancels the link timer before
155 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500157static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158{
Per Liden4323add2006-01-18 00:38:21 +0100159 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160
161 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
163 l_ptr->stats.queue_sz_counts++;
164
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 if (l_ptr->first_out) {
166 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
167 u32 length = msg_size(msg);
168
Joe Perchesf64f9e72009-11-29 16:55:45 -0800169 if ((msg_user(msg) == MSG_FRAGMENTER) &&
170 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 length = msg_size(msg_get_wrapped(msg));
172 }
173 if (length) {
174 l_ptr->stats.msg_lengths_total += length;
175 l_ptr->stats.msg_length_counts++;
176 if (length <= 64)
177 l_ptr->stats.msg_length_profile[0]++;
178 else if (length <= 256)
179 l_ptr->stats.msg_length_profile[1]++;
180 else if (length <= 1024)
181 l_ptr->stats.msg_length_profile[2]++;
182 else if (length <= 4096)
183 l_ptr->stats.msg_length_profile[3]++;
184 else if (length <= 16384)
185 l_ptr->stats.msg_length_profile[4]++;
186 else if (length <= 32768)
187 l_ptr->stats.msg_length_profile[5]++;
188 else
189 l_ptr->stats.msg_length_profile[6]++;
190 }
191 }
192
193 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194
195 link_state_event(l_ptr, TIMEOUT_EVT);
196
197 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100198 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199
Per Liden4323add2006-01-18 00:38:21 +0100200 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201}
202
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500203static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204{
205 k_start_timer(&l_ptr->timer, time);
206}
207
208/**
Per Liden4323add2006-01-18 00:38:21 +0100209 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500210 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900213 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 * Returns pointer to link.
215 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500216struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Allan Stephens37b9c082011-02-28 11:32:27 -0500217 struct tipc_bearer *b_ptr,
Per Liden4323add2006-01-18 00:38:21 +0100218 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500220 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 struct tipc_msg *msg;
222 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500223 char addr_string[16];
224 u32 peer = n_ptr->addr;
225
226 if (n_ptr->link_cnt >= 2) {
227 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400228 pr_err("Attempt to establish third link to %s\n", addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500229 return NULL;
230 }
231
232 if (n_ptr->links[b_ptr->identity]) {
233 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400234 pr_err("Attempt to establish second link on <%s> to %s\n",
235 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500236 return NULL;
237 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700239 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400241 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 return NULL;
243 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
245 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500246 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400247 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900249 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 if_name,
251 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400252 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500254 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 l_ptr->checkpoint = 1;
Allan Stephensf882cb72011-04-07 09:43:27 -0400256 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 l_ptr->b_ptr = b_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -0400258 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 l_ptr->state = RESET_UNKNOWN;
260
261 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
262 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000263 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700265 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 msg_set_bearer_id(msg, b_ptr->identity);
267 strcpy((char *)msg_data(msg), if_name);
268
269 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400270 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271
272 link_init_max_pkt(l_ptr);
273
274 l_ptr->next_out_no = 1;
275 INIT_LIST_HEAD(&l_ptr->waiting_ports);
276
277 link_reset_statistics(l_ptr);
278
Allan Stephens37b9c082011-02-28 11:32:27 -0500279 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500281 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
282 (unsigned long)l_ptr);
Florian Westphal945710652007-07-26 00:05:07 -0700283 list_add_tail(&l_ptr->link_list, &b_ptr->links);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000284 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
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);
309 tipc_link_stop(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
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500316static void link_start(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317{
Allan Stephens214dda42011-01-24 16:22:43 -0500318 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 link_state_event(l_ptr, STARTING_EVT);
Allan Stephens214dda42011-01-24 16:22:43 -0500320 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321}
322
323/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900324 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 * @l_ptr: pointer to link
326 * @origport: reference to sending port
327 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900328 *
329 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 * has abated.
331 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500332static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500334 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335
Per Liden4323add2006-01-18 00:38:21 +0100336 spin_lock_bh(&tipc_port_list_lock);
337 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 if (p_ptr) {
339 if (!p_ptr->wakeup)
340 goto exit;
341 if (!list_empty(&p_ptr->wait_list))
342 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500343 p_ptr->congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000344 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
346 l_ptr->stats.link_congs++;
347exit:
Per Liden4323add2006-01-18 00:38:21 +0100348 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 }
Per Liden4323add2006-01-18 00:38:21 +0100350 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 return -ELINKCONG;
352}
353
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500354void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500356 struct tipc_port *p_ptr;
357 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
359
360 if (all)
361 win = 100000;
362 if (win <= 0)
363 return;
Per Liden4323add2006-01-18 00:38:21 +0100364 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 return;
366 if (link_congested(l_ptr))
367 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900368 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 wait_list) {
370 if (win <= 0)
371 break;
372 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500373 spin_lock_bh(p_ptr->lock);
374 p_ptr->congested = 0;
375 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500377 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 }
379
380exit:
Per Liden4323add2006-01-18 00:38:21 +0100381 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382}
383
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900384/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 * link_release_outqueue - purge link's outbound message queue
386 * @l_ptr: pointer to link
387 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500388static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389{
Ying Xued77b3832013-12-10 20:45:38 -0800390 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 l_ptr->first_out = NULL;
392 l_ptr->out_queue_size = 0;
393}
394
395/**
Per Liden4323add2006-01-18 00:38:21 +0100396 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 * @l_ptr: pointer to link
398 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500399void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
Erik Hugne40ba3cd2013-11-06 09:28:06 +0100401 kfree_skb(l_ptr->reasm_head);
402 l_ptr->reasm_head = NULL;
403 l_ptr->reasm_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404}
405
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900406/**
Per Liden4323add2006-01-18 00:38:21 +0100407 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 * @l_ptr: pointer to link
409 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500410void tipc_link_stop(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411{
Ying Xued77b3832013-12-10 20:45:38 -0800412 kfree_skb_list(l_ptr->oldest_deferred_in);
413 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100414 tipc_link_reset_fragments(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400415 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 l_ptr->proto_msg_queue = NULL;
417}
418
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500419void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 u32 prev_state = l_ptr->state;
422 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700423 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424
Allan Stephensa686e682008-06-04 17:29:39 -0700425 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426
Allan Stephensa686e682008-06-04 17:29:39 -0700427 /* Link is down, accept any session */
428 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900432
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
435 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
436 return;
437
Per Liden4323add2006-01-18 00:38:21 +0100438 tipc_node_link_down(l_ptr->owner, l_ptr);
439 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000440
Paul Gortmaker8f19afb2011-02-28 11:36:21 -0400441 if (was_active_link && tipc_node_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 l_ptr->owner->permit_changeover) {
443 l_ptr->reset_checkpoint = checkpoint;
444 l_ptr->exp_msg_count = START_CHANGEOVER;
445 }
446
447 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400449 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 l_ptr->proto_msg_queue = NULL;
Ying Xued77b3832013-12-10 20:45:38 -0800451 kfree_skb_list(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100453 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454
455 l_ptr->retransm_queue_head = 0;
456 l_ptr->retransm_queue_size = 0;
457 l_ptr->last_out = NULL;
458 l_ptr->first_out = NULL;
459 l_ptr->next_out = NULL;
460 l_ptr->unacked_window = 0;
461 l_ptr->checkpoint = 1;
462 l_ptr->next_out_no = 1;
463 l_ptr->deferred_inqueue_sz = 0;
464 l_ptr->oldest_deferred_in = NULL;
465 l_ptr->newest_deferred_in = NULL;
466 l_ptr->fsm_msg_cnt = 0;
467 l_ptr->stale_count = 0;
468 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469}
470
471
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500472static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473{
Allan Stephens5392d642006-06-25 23:52:50 -0700474 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100475 tipc_node_link_up(l_ptr->owner, l_ptr);
476 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477}
478
479/**
480 * link_state_event - link finite state machine
481 * @l_ptr: pointer to link
482 * @event: state machine event to process
483 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000484static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500486 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 u32 cont_intv = l_ptr->continuity_interval;
488
489 if (!l_ptr->started && (event != STARTING_EVT))
490 return; /* Not yet. */
491
Ying Xue77a7e072013-12-10 20:45:44 -0800492 /* Check whether changeover is going on */
493 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000494 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800496 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
499 switch (l_ptr->state) {
500 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 switch (event) {
502 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 break;
505 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 if (l_ptr->next_in_no != l_ptr->checkpoint) {
507 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100508 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100510 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 l_ptr->fsm_msg_cnt++;
512 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900513 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100514 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 l_ptr->fsm_msg_cnt++;
516 }
517 link_set_timer(l_ptr, cont_intv);
518 break;
519 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 l_ptr->state = WORKING_UNKNOWN;
521 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100522 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 l_ptr->fsm_msg_cnt++;
524 link_set_timer(l_ptr, cont_intv / 4);
525 break;
526 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400527 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
528 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100529 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 l_ptr->state = RESET_RESET;
531 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100532 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 l_ptr->fsm_msg_cnt++;
534 link_set_timer(l_ptr, cont_intv);
535 break;
536 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400537 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 }
539 break;
540 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 switch (event) {
542 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 l_ptr->state = WORKING_WORKING;
545 l_ptr->fsm_msg_cnt = 0;
546 link_set_timer(l_ptr, cont_intv);
547 break;
548 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400549 pr_info("%s<%s>, requested by peer while probing\n",
550 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100551 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 l_ptr->state = RESET_RESET;
553 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100554 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 l_ptr->fsm_msg_cnt++;
556 link_set_timer(l_ptr, cont_intv);
557 break;
558 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 l_ptr->state = WORKING_WORKING;
561 l_ptr->fsm_msg_cnt = 0;
562 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100563 if (tipc_bclink_acks_missing(l_ptr->owner)) {
564 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
565 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 l_ptr->fsm_msg_cnt++;
567 }
568 link_set_timer(l_ptr, cont_intv);
569 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900570 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100571 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 l_ptr->fsm_msg_cnt++;
573 link_set_timer(l_ptr, cont_intv / 4);
574 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400575 pr_warn("%s<%s>, peer not responding\n",
576 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100577 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 l_ptr->state = RESET_UNKNOWN;
579 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100580 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
581 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 l_ptr->fsm_msg_cnt++;
583 link_set_timer(l_ptr, cont_intv);
584 }
585 break;
586 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400587 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 }
589 break;
590 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 switch (event) {
592 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 break;
594 case ACTIVATE_MSG:
595 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000596 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 l_ptr->state = WORKING_WORKING;
599 l_ptr->fsm_msg_cnt = 0;
600 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100601 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800603 if (l_ptr->owner->working_links == 1)
604 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 link_set_timer(l_ptr, cont_intv);
606 break;
607 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 l_ptr->state = RESET_RESET;
609 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100610 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 l_ptr->fsm_msg_cnt++;
612 link_set_timer(l_ptr, cont_intv);
613 break;
614 case STARTING_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 l_ptr->started = 1;
616 /* fall through */
617 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100618 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 l_ptr->fsm_msg_cnt++;
620 link_set_timer(l_ptr, cont_intv);
621 break;
622 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400623 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 }
625 break;
626 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 switch (event) {
628 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 case ACTIVATE_MSG:
630 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000631 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 l_ptr->state = WORKING_WORKING;
634 l_ptr->fsm_msg_cnt = 0;
635 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100636 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800638 if (l_ptr->owner->working_links == 1)
639 tipc_link_send_sync(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 link_set_timer(l_ptr, cont_intv);
641 break;
642 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 break;
644 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100645 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 l_ptr->fsm_msg_cnt++;
647 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 break;
649 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400650 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 }
652 break;
653 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400654 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 }
656}
657
658/*
659 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900660 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400662static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 struct sk_buff *buf)
664{
665 struct tipc_msg *bundler_msg = buf_msg(bundler);
666 struct tipc_msg *msg = buf_msg(buf);
667 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700668 u32 bundle_size = msg_size(bundler_msg);
669 u32 to_pos = align(bundle_size);
670 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671
672 if (msg_user(bundler_msg) != MSG_BUNDLER)
673 return 0;
674 if (msg_type(bundler_msg) != OPEN_MSG)
675 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700676 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000678 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700679 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680
Allan Stephense49060c2006-06-29 12:32:46 -0700681 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300682 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 msg_set_size(bundler_msg, to_pos + size);
684 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400685 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 l_ptr->stats.sent_bundled++;
687 return 1;
688}
689
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500690static void link_add_to_outqueue(struct tipc_link *l_ptr,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800691 struct sk_buff *buf,
692 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693{
694 u32 ack = mod(l_ptr->next_in_no - 1);
695 u32 seqno = mod(l_ptr->next_out_no++);
696
697 msg_set_word(msg, 2, ((ack << 16) | seqno));
698 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
699 buf->next = NULL;
700 if (l_ptr->first_out) {
701 l_ptr->last_out->next = buf;
702 l_ptr->last_out = buf;
703 } else
704 l_ptr->first_out = l_ptr->last_out = buf;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500705
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 l_ptr->out_queue_size++;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500707 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
708 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709}
710
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500711static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
Allan Stephensdc63d912011-04-21 11:50:42 -0400712 struct sk_buff *buf_chain,
713 u32 long_msgno)
714{
715 struct sk_buff *buf;
716 struct tipc_msg *msg;
717
718 if (!l_ptr->next_out)
719 l_ptr->next_out = buf_chain;
720 while (buf_chain) {
721 buf = buf_chain;
722 buf_chain = buf_chain->next;
723
724 msg = buf_msg(buf);
725 msg_set_long_msgno(msg, long_msgno);
726 link_add_to_outqueue(l_ptr, buf, msg);
727 }
728}
729
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730/*
731 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 * inside TIPC when the 'fast path' in tipc_send_buf
733 * has failed, and from link_send()
734 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500735int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736{
737 struct tipc_msg *msg = buf_msg(buf);
738 u32 size = msg_size(msg);
739 u32 dsz = msg_data_sz(msg);
740 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000741 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000743 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 /* Match msg importance against queue limits: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 if (unlikely(queue_size >= queue_limit)) {
747 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
Allan Stephensbebc55a2011-04-19 10:17:58 -0400748 link_schedule_port(l_ptr, msg_origport(msg), size);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400749 kfree_skb(buf);
Allan Stephensbebc55a2011-04-19 10:17:58 -0400750 return -ELINKCONG;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 }
Allan Stephens5f6d9122011-11-04 13:24:29 -0400752 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 if (imp > CONN_MANAGER) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400754 pr_warn("%s<%s>, send queue full", link_rst_msg,
755 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100756 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 }
758 return dsz;
759 }
760
761 /* Fragmentation needed ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000763 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400765 /* Packet can be queued or sent. */
Erik Hugne512137e2013-12-06 10:08:00 -0500766 if (likely(!link_congested(l_ptr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 link_add_to_outqueue(l_ptr, buf, msg);
768
Ying Xue3c294cb2012-11-15 11:34:45 +0800769 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
770 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 return dsz;
772 }
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400773 /* Congestion: can message be bundled ? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
775 (msg_user(msg) != MSG_FRAGMENTER)) {
776
777 /* Try adding message to an existing bundle */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900778 if (l_ptr->next_out &&
Ying Xue3c294cb2012-11-15 11:34:45 +0800779 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 return dsz;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781
782 /* Try creating a new bundle */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000784 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 struct tipc_msg bundler_hdr;
786
787 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000788 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700789 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300790 skb_copy_to_linear_data(bundler, &bundler_hdr,
791 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 skb_trim(bundler, INT_H_SIZE);
793 link_bundle_buf(l_ptr, bundler, buf);
794 buf = bundler;
795 msg = buf_msg(buf);
796 l_ptr->stats.sent_bundles++;
797 }
798 }
799 }
800 if (!l_ptr->next_out)
801 l_ptr->next_out = buf;
802 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 return dsz;
804}
805
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806/*
807 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 * not been selected yet, and the the owner node is not locked
809 * Called by TIPC internal users, e.g. the name distributor
810 */
Per Liden4323add2006-01-18 00:38:21 +0100811int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500813 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700814 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 int res = -ELINKCONG;
816
Per Liden4323add2006-01-18 00:38:21 +0100817 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000818 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100820 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000822 if (l_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100823 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000824 else
Allan Stephens5f6d9122011-11-04 13:24:29 -0400825 kfree_skb(buf);
Per Liden4323add2006-01-18 00:38:21 +0100826 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 } else {
Allan Stephens5f6d9122011-11-04 13:24:29 -0400828 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 }
Per Liden4323add2006-01-18 00:38:21 +0100830 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 return res;
832}
833
Jon Maloyc64f7a62012-11-16 13:51:31 +0800834/*
835 * tipc_link_send_sync - synchronize broadcast link endpoints.
836 *
837 * Give a newly added peer node the sequence number where it should
838 * start receiving and acking broadcast packets.
839 *
840 * Called with node locked
841 */
842static void tipc_link_send_sync(struct tipc_link *l)
843{
844 struct sk_buff *buf;
845 struct tipc_msg *msg;
846
847 buf = tipc_buf_acquire(INT_H_SIZE);
848 if (!buf)
849 return;
850
851 msg = buf_msg(buf);
852 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
853 msg_set_last_bcast(msg, l->owner->bclink.acked);
854 link_add_chain_to_outqueue(l, buf, 0);
855 tipc_link_push_queue(l);
856}
857
858/*
859 * tipc_link_recv_sync - synchronize broadcast link endpoints.
860 * Receive the sequence number where we should start receiving and
861 * acking broadcast packets from a newly added peer node, and open
862 * up for reception of such packets.
863 *
864 * Called with node locked
865 */
866static void tipc_link_recv_sync(struct tipc_node *n, struct sk_buff *buf)
867{
868 struct tipc_msg *msg = buf_msg(buf);
869
870 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
871 n->bclink.recv_permitted = true;
872 kfree_skb(buf);
873}
874
875/*
Allan Stephens9aa88c22011-05-31 13:38:02 -0400876 * tipc_link_send_names - send name table entries to new neighbor
877 *
878 * Send routine for bulk delivery of name table messages when contact
879 * with a new neighbor occurs. No link congestion checking is performed
880 * because name table messages *must* be delivered. The messages must be
881 * small enough not to require fragmentation.
882 * Called without any locks held.
883 */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400884void tipc_link_send_names(struct list_head *message_list, u32 dest)
885{
886 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500887 struct tipc_link *l_ptr;
Allan Stephens9aa88c22011-05-31 13:38:02 -0400888 struct sk_buff *buf;
889 struct sk_buff *temp_buf;
890
891 if (list_empty(message_list))
892 return;
893
894 read_lock_bh(&tipc_net_lock);
895 n_ptr = tipc_node_find(dest);
896 if (n_ptr) {
897 tipc_node_lock(n_ptr);
898 l_ptr = n_ptr->active_links[0];
899 if (l_ptr) {
900 /* convert circular list to linear list */
901 ((struct sk_buff *)message_list->prev)->next = NULL;
902 link_add_chain_to_outqueue(l_ptr,
903 (struct sk_buff *)message_list->next, 0);
904 tipc_link_push_queue(l_ptr);
905 INIT_LIST_HEAD(message_list);
906 }
907 tipc_node_unlock(n_ptr);
908 }
909 read_unlock_bh(&tipc_net_lock);
910
911 /* discard the messages if they couldn't be sent */
Allan Stephens9aa88c22011-05-31 13:38:02 -0400912 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
913 list_del((struct list_head *)buf);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400914 kfree_skb(buf);
Allan Stephens9aa88c22011-05-31 13:38:02 -0400915 }
916}
917
918/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900919 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 * destination link is known and the header is complete,
921 * inclusive total message length. Very time critical.
922 * Link is locked. Returns user data length.
923 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500924static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
Sam Ravnborg05790c62006-03-20 22:37:04 -0800925 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926{
927 struct tipc_msg *msg = buf_msg(buf);
928 int res = msg_data_sz(msg);
929
930 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +0000931 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Erik Hugne512137e2013-12-06 10:08:00 -0500932 link_add_to_outqueue(l_ptr, buf, msg);
933 tipc_bearer_send(l_ptr->b_ptr, buf,
934 &l_ptr->media_addr);
935 l_ptr->unacked_window = 0;
936 return res;
937 }
938 else
Allan Stephens15e979d2010-05-11 14:30:10 +0000939 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 }
Per Liden4323add2006-01-18 00:38:21 +0100941 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942}
943
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900944/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900945 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900947 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 * Returns user data length or errno.
949 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500950int tipc_link_send_sections_fast(struct tipc_port *sender,
Per Liden4323add2006-01-18 00:38:21 +0100951 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +0200952 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500954 struct tipc_msg *hdr = &sender->phdr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500955 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -0700957 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 int res;
959 u32 selector = msg_origport(hdr) & 1;
960
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961again:
962 /*
963 * Try building message using port's max_pkt hint.
964 * (Must not hold any locks while building message.)
965 */
Ying Xue9446b872013-10-18 07:23:15 +0200966 res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
Ying Xue7410f962013-06-17 10:54:49 -0400967 /* Exit if build request was invalid */
968 if (unlikely(res < 0))
969 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970
Per Liden4323add2006-01-18 00:38:21 +0100971 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000972 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +0100974 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 l_ptr = node->active_links[selector];
976 if (likely(l_ptr)) {
977 if (likely(buf)) {
978 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500979 &sender->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980exit:
Per Liden4323add2006-01-18 00:38:21 +0100981 tipc_node_unlock(node);
982 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 return res;
984 }
985
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986 /* Exit if link (or bearer) is congested */
Erik Hugne512137e2013-12-06 10:08:00 -0500987 if (link_congested(l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 res = link_schedule_port(l_ptr,
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500989 sender->ref, res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 goto exit;
991 }
992
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900993 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 * Message size exceeds max_pkt hint; update hint,
995 * then re-try fast path or fragment the message
996 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500997 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +0100998 tipc_node_unlock(node);
999 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000
1001
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001002 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 goto again;
1004
Ying Xue9446b872013-10-18 07:23:15 +02001005 return link_send_sections_long(sender, msg_sect, len,
Allan Stephens26896902011-04-21 10:42:07 -05001006 destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 }
Per Liden4323add2006-01-18 00:38:21 +01001008 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 }
Per Liden4323add2006-01-18 00:38:21 +01001010 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011
1012 /* Couldn't find a link to the destination node */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 if (buf)
1014 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1015 if (res >= 0)
Ying Xue9446b872013-10-18 07:23:15 +02001016 return tipc_port_reject_sections(sender, hdr, msg_sect,
1017 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 return res;
1019}
1020
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021/*
1022 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 * Link and bearer congestion status have been checked to be ok,
1026 * and are ignored if they change.
1027 *
1028 * Note that fragments do not use the full link MTU so that they won't have
1029 * to undergo refragmentation if link changeover causes them to be sent
1030 * over another link with an additional tunnel header added as prefix.
1031 * (Refragmentation will still occur if the other link has a smaller MTU.)
1032 *
1033 * Returns user data length or errno.
1034 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001035static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 struct iovec const *msg_sect,
Ying Xue9446b872013-10-18 07:23:15 +02001037 unsigned int len, u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001039 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001040 struct tipc_node *node;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001041 struct tipc_msg *hdr = &sender->phdr;
Ying Xue9446b872013-10-18 07:23:15 +02001042 u32 dsz = len;
Allan Stephens0e659672010-12-31 18:59:32 +00001043 u32 max_pkt, fragm_sz, rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 struct tipc_msg fragm_hdr;
Allan Stephens0e659672010-12-31 18:59:32 +00001045 struct sk_buff *buf, *buf_chain, *prev;
1046 u32 fragm_crs, fragm_rest, hsz, sect_rest;
Ying Xue40682432013-10-18 07:23:16 +02001047 const unchar __user *sect_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 int curr_sect;
1049 u32 fragm_no;
Ying Xue126c0522013-06-17 10:54:50 -04001050 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051
1052again:
1053 fragm_no = 1;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001054 max_pkt = sender->max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001056 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 /* leave room for fragmentation header in each fragment */
1058 rest = dsz;
1059 fragm_crs = 0;
1060 fragm_rest = 0;
1061 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001062 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 curr_sect = -1;
1064
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001065 /* Prepare reusable fragment header */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001066 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001067 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 msg_set_size(&fragm_hdr, max_pkt);
1069 msg_set_fragm_no(&fragm_hdr, 1);
1070
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001071 /* Prepare header of first fragment */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001072 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 if (!buf)
1074 return -ENOMEM;
1075 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001076 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001078 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001080 /* Chop up message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 fragm_crs = INT_H_SIZE + hsz;
1082 fragm_rest = fragm_sz - hsz;
1083
1084 do { /* For all sections */
1085 u32 sz;
1086
1087 if (!sect_rest) {
1088 sect_rest = msg_sect[++curr_sect].iov_len;
Ying Xue40682432013-10-18 07:23:16 +02001089 sect_crs = msg_sect[curr_sect].iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 }
1091
1092 if (sect_rest < fragm_rest)
1093 sz = sect_rest;
1094 else
1095 sz = fragm_rest;
1096
Ying Xuef1733d72013-06-17 10:54:43 -04001097 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
Ying Xue126c0522013-06-17 10:54:50 -04001098 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099error:
Ying Xued77b3832013-12-10 20:45:38 -08001100 kfree_skb_list(buf_chain);
Ying Xue126c0522013-06-17 10:54:50 -04001101 return res;
Ying Xuef1733d72013-06-17 10:54:43 -04001102 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 sect_crs += sz;
1104 sect_rest -= sz;
1105 fragm_crs += sz;
1106 fragm_rest -= sz;
1107 rest -= sz;
1108
1109 if (!fragm_rest && rest) {
1110
1111 /* Initiate new fragment: */
1112 if (rest <= fragm_sz) {
1113 fragm_sz = rest;
Allan Stephens0e659672010-12-31 18:59:32 +00001114 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 } else {
1116 msg_set_type(&fragm_hdr, FRAGMENT);
1117 }
1118 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1119 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1120 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001121 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Ying Xue126c0522013-06-17 10:54:50 -04001122 if (!buf) {
1123 res = -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 goto error;
Ying Xue126c0522013-06-17 10:54:50 -04001125 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001127 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001129 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 fragm_crs = INT_H_SIZE;
1131 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 }
Allan Stephens0e659672010-12-31 18:59:32 +00001133 } while (rest > 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001135 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 * Now we have a buffer chain. Select a link and check
1137 * that packet size is still OK
1138 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001139 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001141 tipc_node_lock(node);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001142 l_ptr = node->active_links[sender->ref & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001144 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 goto reject;
1146 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001147 if (l_ptr->max_pkt < max_pkt) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001148 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001149 tipc_node_unlock(node);
Ying Xued77b3832013-12-10 20:45:38 -08001150 kfree_skb_list(buf_chain);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 goto again;
1152 }
1153 } else {
1154reject:
Ying Xued77b3832013-12-10 20:45:38 -08001155 kfree_skb_list(buf_chain);
Ying Xue9446b872013-10-18 07:23:15 +02001156 return tipc_port_reject_sections(sender, hdr, msg_sect,
1157 len, TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 }
1159
Allan Stephensdc63d912011-04-21 11:50:42 -04001160 /* Append chain of fragments to send queue & send them */
Allan Stephense0f08592011-04-17 11:44:24 -04001161 l_ptr->long_msg_seq_no++;
Allan Stephensdc63d912011-04-21 11:50:42 -04001162 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
1163 l_ptr->stats.sent_fragments += fragm_no;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 l_ptr->stats.sent_fragmented++;
Per Liden4323add2006-01-18 00:38:21 +01001165 tipc_link_push_queue(l_ptr);
1166 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 return dsz;
1168}
1169
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001170/*
Per Liden4323add2006-01-18 00:38:21 +01001171 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 */
stephen hemminger98056962014-01-04 13:47:48 -08001173static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174{
1175 struct sk_buff *buf = l_ptr->first_out;
1176 u32 r_q_size = l_ptr->retransm_queue_size;
1177 u32 r_q_head = l_ptr->retransm_queue_head;
1178
1179 /* Step to position where retransmission failed, if any, */
1180 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001182 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -04001184 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185
1186 while (buf && less(first, r_q_head)) {
1187 first = mod(first + 1);
1188 buf = buf->next;
1189 }
1190 l_ptr->retransm_queue_head = r_q_head = first;
1191 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1192 }
1193
1194 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +00001195 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001197 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001198 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1199 l_ptr->retransm_queue_head = mod(++r_q_head);
1200 l_ptr->retransm_queue_size = --r_q_size;
1201 l_ptr->stats.retransmitted++;
1202 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 }
1204
1205 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 buf = l_ptr->proto_msg_queue;
1207 if (buf) {
1208 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001209 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001210 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1211 l_ptr->unacked_window = 0;
1212 kfree_skb(buf);
1213 l_ptr->proto_msg_queue = NULL;
1214 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 }
1216
1217 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 buf = l_ptr->next_out;
1219 if (buf) {
1220 struct tipc_msg *msg = buf_msg(buf);
1221 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -04001222 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223
1224 if (mod(next - first) < l_ptr->queue_limit[0]) {
1225 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001226 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001227 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1228 if (msg_user(msg) == MSG_BUNDLER)
1229 msg_set_type(msg, CLOSED_MSG);
1230 l_ptr->next_out = buf->next;
1231 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 }
1233 }
Ying Xue3c294cb2012-11-15 11:34:45 +08001234 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235}
1236
1237/*
1238 * push_queue(): push out the unsent messages of a link where
1239 * congestion has abated. Node is locked
1240 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001241void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242{
1243 u32 res;
1244
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 do {
Per Liden4323add2006-01-18 00:38:21 +01001246 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001247 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248}
1249
Allan Stephensd356eeb2006-06-25 23:40:01 -07001250static void link_reset_all(unsigned long addr)
1251{
David S. Miller6c000552008-09-02 23:38:32 -07001252 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001253 char addr_string[16];
1254 u32 i;
1255
1256 read_lock_bh(&tipc_net_lock);
1257 n_ptr = tipc_node_find((u32)addr);
1258 if (!n_ptr) {
1259 read_unlock_bh(&tipc_net_lock);
1260 return; /* node no longer exists */
1261 }
1262
1263 tipc_node_lock(n_ptr);
1264
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001265 pr_warn("Resetting all links to %s\n",
1266 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001267
1268 for (i = 0; i < MAX_BEARERS; i++) {
1269 if (n_ptr->links[i]) {
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001270 link_print(n_ptr->links[i], "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001271 tipc_link_reset(n_ptr->links[i]);
1272 }
1273 }
1274
1275 tipc_node_unlock(n_ptr);
1276 read_unlock_bh(&tipc_net_lock);
1277}
1278
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001279static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001280 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -07001281{
1282 struct tipc_msg *msg = buf_msg(buf);
1283
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001284 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001285
1286 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001287 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001288 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001289 tipc_link_reset(l_ptr);
1290
1291 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001292 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -07001293 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001294 char addr_string[16];
1295
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001296 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1297 pr_cont("Outstanding acks: %lu\n",
1298 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001299
Allan Stephens01d83ed2011-01-18 13:53:16 -05001300 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -07001301 tipc_node_lock(n_ptr);
1302
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001303 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001304 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +08001305 pr_info("Reception permitted: %d, Acked: %u\n",
1306 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001307 n_ptr->bclink.acked);
1308 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1309 n_ptr->bclink.last_in,
1310 n_ptr->bclink.oos_state,
1311 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001312
1313 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1314
1315 tipc_node_unlock(n_ptr);
1316
1317 l_ptr->stale_count = 0;
1318 }
1319}
1320
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001321void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001322 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323{
1324 struct tipc_msg *msg;
1325
Allan Stephensd356eeb2006-06-25 23:40:01 -07001326 if (!buf)
1327 return;
1328
1329 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001330
Erik Hugne512137e2013-12-06 10:08:00 -05001331 /* Detect repeated retransmit failures */
1332 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1333 if (++l_ptr->stale_count > 100) {
1334 link_retransmit_failure(l_ptr, buf);
1335 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001336 }
1337 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001338 l_ptr->last_retransmitted = msg_seqno(msg);
1339 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001341
Neil Hormanca509102010-03-15 07:58:45 +00001342 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 msg = buf_msg(buf);
1344 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001345 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +08001346 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
1347 buf = buf->next;
1348 retransmits--;
1349 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001351
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1353}
1354
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001355/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 * link_insert_deferred_queue - insert deferred messages back into receive chain
1357 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001358static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 struct sk_buff *buf)
1360{
1361 u32 seq_no;
1362
1363 if (l_ptr->oldest_deferred_in == NULL)
1364 return buf;
1365
Allan Stephensf9057302011-10-24 16:03:12 -04001366 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 if (seq_no == mod(l_ptr->next_in_no)) {
1368 l_ptr->newest_deferred_in->next = buf;
1369 buf = l_ptr->oldest_deferred_in;
1370 l_ptr->oldest_deferred_in = NULL;
1371 l_ptr->deferred_inqueue_sz = 0;
1372 }
1373 return buf;
1374}
1375
Allan Stephens85035562008-04-15 19:04:54 -07001376/**
1377 * link_recv_buf_validate - validate basic format of received message
1378 *
1379 * This routine ensures a TIPC message has an acceptable header, and at least
1380 * as much data as the header indicates it should. The routine also ensures
1381 * that the entire message header is stored in the main fragment of the message
1382 * buffer, to simplify future access to message header fields.
1383 *
1384 * Note: Having extra info present in the message header or data areas is OK.
1385 * TIPC will ignore the excess, under the assumption that it is optional info
1386 * introduced by a later release of the protocol.
1387 */
Allan Stephens85035562008-04-15 19:04:54 -07001388static int link_recv_buf_validate(struct sk_buff *buf)
1389{
1390 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001391 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001392 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1393 };
1394
1395 struct tipc_msg *msg;
1396 u32 tipc_hdr[2];
1397 u32 size;
1398 u32 hdr_size;
1399 u32 min_hdr_size;
1400
1401 if (unlikely(buf->len < MIN_H_SIZE))
1402 return 0;
1403
1404 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1405 if (msg == NULL)
1406 return 0;
1407
1408 if (unlikely(msg_version(msg) != TIPC_VERSION))
1409 return 0;
1410
1411 size = msg_size(msg);
1412 hdr_size = msg_hdr_sz(msg);
1413 min_hdr_size = msg_isdata(msg) ?
1414 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1415
1416 if (unlikely((hdr_size < min_hdr_size) ||
1417 (size < hdr_size) ||
1418 (buf->len < size) ||
1419 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1420 return 0;
1421
1422 return pskb_may_pull(buf, hdr_size);
1423}
1424
Allan Stephensb02b69c2010-08-17 11:00:07 +00001425/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001426 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001427 * @head: pointer to message buffer chain
1428 * @tb_ptr: pointer to bearer message arrived on
1429 *
1430 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1431 * structure (i.e. cannot be NULL), but bearer can be inactive.
1432 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001433void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434{
Per Liden4323add2006-01-18 00:38:21 +01001435 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001437 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001438 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 struct sk_buff *crs;
1440 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001441 struct tipc_msg *msg;
1442 u32 seq_no;
1443 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 u32 released = 0;
1445 int type;
1446
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 head = head->next;
Allan Stephens85035562008-04-15 19:04:54 -07001448
Allan Stephensb02b69c2010-08-17 11:00:07 +00001449 /* Ensure bearer is still enabled */
Allan Stephensb02b69c2010-08-17 11:00:07 +00001450 if (unlikely(!b_ptr->active))
Ying Xue3af390e2013-10-30 11:26:57 +08001451 goto discard;
Allan Stephensb02b69c2010-08-17 11:00:07 +00001452
Allan Stephens85035562008-04-15 19:04:54 -07001453 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001454 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001455 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456
Allan Stephensfe13dda2008-04-15 19:03:23 -07001457 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001458 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001459 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001460
Allan Stephens85035562008-04-15 19:04:54 -07001461 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001462 msg = buf_msg(buf);
1463
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001465 if (msg_user(msg) == LINK_CONFIG)
1466 tipc_disc_recv_msg(buf, b_ptr);
1467 else
1468 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 continue;
1470 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001471
Allan Stephensed33a9c2011-04-05 15:15:04 -04001472 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001473 if (unlikely(!msg_short(msg) &&
1474 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001475 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001476
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001477 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001478 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001480 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001481 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001482
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001483 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001485 if (unlikely(!l_ptr))
1486 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001487
Allan Stephensb4b56102011-05-27 11:00:51 -04001488 /* Verify that communication with node is currently allowed */
Allan Stephensb4b56102011-05-27 11:00:51 -04001489 if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
1490 msg_user(msg) == LINK_PROTOCOL &&
1491 (msg_type(msg) == RESET_MSG ||
1492 msg_type(msg) == ACTIVATE_MSG) &&
1493 !msg_redundant_link(msg))
1494 n_ptr->block_setup &= ~WAIT_PEER_DOWN;
1495
Ying Xue3af390e2013-10-30 11:26:57 +08001496 if (n_ptr->block_setup)
1497 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001498
Allan Stephens85035562008-04-15 19:04:54 -07001499 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001500 seq_no = msg_seqno(msg);
1501 ackd = msg_ack(msg);
1502
1503 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001504 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001505 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506
1507 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001508 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001509 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 struct sk_buff *next = crs->next;
1511
Allan Stephens5f6d9122011-11-04 13:24:29 -04001512 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513 crs = next;
1514 released++;
1515 }
1516 if (released) {
1517 l_ptr->first_out = crs;
1518 l_ptr->out_queue_size -= released;
1519 }
Allan Stephens85035562008-04-15 19:04:54 -07001520
1521 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001523 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001525 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1527 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001528 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529 }
1530
Allan Stephens85035562008-04-15 19:04:54 -07001531 /* Now (finally!) process the incoming message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532protocol_check:
Ying Xue3af390e2013-10-30 11:26:57 +08001533 if (unlikely(!link_working_working(l_ptr))) {
1534 if (msg_user(msg) == LINK_PROTOCOL) {
1535 link_recv_proto_msg(l_ptr, buf);
1536 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001537 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538 continue;
1539 }
Ying Xue3af390e2013-10-30 11:26:57 +08001540
1541 /* Traffic message. Conditionally activate link */
1542 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1543
1544 if (link_working_working(l_ptr)) {
1545 /* Re-insert buffer in front of queue */
1546 buf->next = head;
1547 head = buf;
1548 tipc_node_unlock(n_ptr);
1549 continue;
1550 }
1551 goto unlock_discard;
1552 }
1553
1554 /* Link is now in state WORKING_WORKING */
1555 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 link_handle_out_of_seq_msg(l_ptr, buf);
1557 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001558 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 continue;
1560 }
Ying Xue3af390e2013-10-30 11:26:57 +08001561 l_ptr->next_in_no++;
1562 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 head = link_insert_deferred_queue(l_ptr, head);
Ying Xue3af390e2013-10-30 11:26:57 +08001564deliver:
1565 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001566 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001567 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568 continue;
1569 }
Ying Xue3af390e2013-10-30 11:26:57 +08001570 switch (msg_user(msg)) {
1571 int ret;
1572 case MSG_BUNDLER:
1573 l_ptr->stats.recv_bundles++;
1574 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1575 tipc_node_unlock(n_ptr);
1576 tipc_link_recv_bundle(buf);
1577 continue;
1578 case NAME_DISTRIBUTOR:
1579 n_ptr->bclink.recv_permitted = true;
1580 tipc_node_unlock(n_ptr);
1581 tipc_named_recv(buf);
1582 continue;
1583 case BCAST_PROTOCOL:
1584 tipc_link_recv_sync(n_ptr, buf);
Per Liden4323add2006-01-18 00:38:21 +01001585 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001587 case CONN_MANAGER:
1588 tipc_node_unlock(n_ptr);
1589 tipc_port_recv_proto_msg(buf);
1590 continue;
1591 case MSG_FRAGMENTER:
1592 l_ptr->stats.recv_fragments++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001593 ret = tipc_link_recv_fragment(&l_ptr->reasm_head,
1594 &l_ptr->reasm_tail,
1595 &buf);
1596 if (ret == LINK_REASM_COMPLETE) {
Ying Xue3af390e2013-10-30 11:26:57 +08001597 l_ptr->stats.recv_fragmented++;
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001598 msg = buf_msg(buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001599 goto deliver;
1600 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01001601 if (ret == LINK_REASM_ERROR)
Erik Hugnea715b492013-11-06 09:28:07 +01001602 tipc_link_reset(l_ptr);
Erik Hugne528f6f42013-11-06 09:28:05 +01001603 tipc_node_unlock(n_ptr);
1604 continue;
Ying Xue3af390e2013-10-30 11:26:57 +08001605 case CHANGEOVER_PROTOCOL:
1606 type = msg_type(msg);
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001607 if (tipc_link_tunnel_rcv(&l_ptr, &buf)) {
Ying Xue3af390e2013-10-30 11:26:57 +08001608 msg = buf_msg(buf);
1609 seq_no = msg_seqno(msg);
1610 if (type == ORIGINAL_MSG)
1611 goto deliver;
1612 goto protocol_check;
1613 }
1614 break;
1615 default:
1616 kfree_skb(buf);
1617 buf = NULL;
1618 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 }
Per Liden4323add2006-01-18 00:38:21 +01001620 tipc_node_unlock(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001621 tipc_net_route_msg(buf);
1622 continue;
1623unlock_discard:
1624
1625 tipc_node_unlock(n_ptr);
1626discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001627 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 }
Per Liden4323add2006-01-18 00:38:21 +01001629 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630}
1631
Ben Hutchings2c530402012-07-10 10:55:09 +00001632/**
Allan Stephens8809b252011-10-25 10:44:35 -04001633 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1634 *
1635 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636 */
Allan Stephens8809b252011-10-25 10:44:35 -04001637u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001638 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639{
Allan Stephens8809b252011-10-25 10:44:35 -04001640 struct sk_buff *queue_buf;
1641 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001642 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643
1644 buf->next = NULL;
1645
1646 /* Empty queue ? */
1647 if (*head == NULL) {
1648 *head = *tail = buf;
1649 return 1;
1650 }
1651
1652 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001653 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 (*tail)->next = buf;
1655 *tail = buf;
1656 return 1;
1657 }
1658
Allan Stephens8809b252011-10-25 10:44:35 -04001659 /* Locate insertion point in queue, then insert; discard if duplicate */
1660 prev = head;
1661 queue_buf = *head;
1662 for (;;) {
1663 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664
Allan Stephens8809b252011-10-25 10:44:35 -04001665 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001666 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001667 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 }
Allan Stephens8809b252011-10-25 10:44:35 -04001669
1670 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672
Allan Stephens8809b252011-10-25 10:44:35 -04001673 prev = &queue_buf->next;
1674 queue_buf = queue_buf->next;
1675 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676
Allan Stephens8809b252011-10-25 10:44:35 -04001677 buf->next = queue_buf;
1678 *prev = buf;
1679 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680}
1681
Allan Stephens8809b252011-10-25 10:44:35 -04001682/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1684 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001685static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 struct sk_buff *buf)
1687{
Allan Stephensf9057302011-10-24 16:03:12 -04001688 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689
1690 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1691 link_recv_proto_msg(l_ptr, buf);
1692 return;
1693 }
1694
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 l_ptr->checkpoint--;
1697
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001698 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 * Discard packet if a duplicate; otherwise add it to deferred queue
1700 * and notify peer of gap as per protocol specification
1701 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 if (less(seq_no, mod(l_ptr->next_in_no))) {
1703 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001704 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 return;
1706 }
1707
Per Liden4323add2006-01-18 00:38:21 +01001708 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1709 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 l_ptr->deferred_inqueue_sz++;
1711 l_ptr->stats.deferred_recv++;
1712 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01001713 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714 } else
1715 l_ptr->stats.duplicates++;
1716}
1717
1718/*
1719 * Send protocol message to the other endpoint.
1720 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001721void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001722 int probe_msg, u32 gap, u32 tolerance,
1723 u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001725 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001727 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001728 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729
Allan Stephens92d2c902011-10-25 11:20:26 -04001730 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001731 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001732 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001733 l_ptr->proto_msg_queue = NULL;
1734 }
1735
Ying Xue77a7e072013-12-10 20:45:44 -08001736 /* Don't send protocol message during link changeover */
1737 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001739
1740 /* Abort non-RESET send if communication with node is prohibited */
Allan Stephensb4b56102011-05-27 11:00:51 -04001741 if ((l_ptr->owner->block_setup) && (msg_typ != RESET_MSG))
1742 return;
1743
Allan Stephens92d2c902011-10-25 11:20:26 -04001744 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 msg_set_type(msg, msg_typ);
1746 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001747 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001748 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749
1750 if (msg_typ == STATE_MSG) {
1751 u32 next_sent = mod(l_ptr->next_out_no);
1752
Per Liden4323add2006-01-18 00:38:21 +01001753 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 return;
1755 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001756 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757 msg_set_next_sent(msg, next_sent);
1758 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001759 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 gap = mod(rec - mod(l_ptr->next_in_no));
1761 }
1762 msg_set_seq_gap(msg, gap);
1763 if (gap)
1764 l_ptr->stats.sent_nacks++;
1765 msg_set_link_tolerance(msg, tolerance);
1766 msg_set_linkprio(msg, priority);
1767 msg_set_max_pkt(msg, ack_mtu);
1768 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1769 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001770 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 u32 mtu = l_ptr->max_pkt;
1772
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001773 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 link_working_working(l_ptr) &&
1775 l_ptr->fsm_msg_cnt) {
1776 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001777 if (l_ptr->max_pkt_probes == 10) {
1778 l_ptr->max_pkt_target = (msg_size - 4);
1779 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001781 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001783 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784
1785 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001786 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 l_ptr->stats.sent_states++;
1788 } else { /* RESET_MSG or ACTIVATE_MSG */
1789 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1790 msg_set_seq_gap(msg, 0);
1791 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001792 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 msg_set_link_tolerance(msg, l_ptr->tolerance);
1794 msg_set_linkprio(msg, l_ptr->priority);
1795 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1796 }
1797
Allan Stephens75f0aa42011-02-28 15:30:20 -05001798 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1799 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001801 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001802
1803 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1804
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001805 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 if (!buf)
1807 return;
1808
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001809 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001810 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811
Ying Xue3c294cb2012-11-15 11:34:45 +08001812 tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001813 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001814 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815}
1816
1817/*
1818 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001819 * Note that network plane id propagates through the network, and may
1820 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001822static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823{
1824 u32 rec_gap = 0;
1825 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001826 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 u32 msg_tol;
1828 struct tipc_msg *msg = buf_msg(buf);
1829
Ying Xue77a7e072013-12-10 20:45:44 -08001830 /* Discard protocol message during link changeover */
1831 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 goto exit;
1833
1834 /* record unnumbered packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 l_ptr->checkpoint--;
1836
1837 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
1838 if (tipc_own_addr > msg_prevnode(msg))
1839 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
1840
1841 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
1842
1843 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001844
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001846 if (!link_working_unknown(l_ptr) &&
1847 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001848 if (less_eq(msg_session(msg), l_ptr->peer_session))
1849 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001851
1852 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1853 link_working_unknown(l_ptr))) {
1854 /*
1855 * peer has lost contact -- don't allow peer's links
1856 * to reactivate before we recognize loss & clean up
1857 */
1858 l_ptr->owner->block_setup = WAIT_NODE_DOWN;
1859 }
1860
Allan Stephens47361c82011-10-26 10:55:16 -04001861 link_state_event(l_ptr, RESET_MSG);
1862
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 /* fall thru' */
1864 case ACTIVATE_MSG:
1865 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1867
Allan Stephens2db99832010-12-31 18:59:33 +00001868 msg_tol = msg_link_tolerance(msg);
1869 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 link_set_supervision_props(l_ptr, msg_tol);
1871
1872 if (msg_linkprio(msg) > l_ptr->priority)
1873 l_ptr->priority = msg_linkprio(msg);
1874
1875 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001876 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001877 if (max_pkt_info < l_ptr->max_pkt_target)
1878 l_ptr->max_pkt_target = max_pkt_info;
1879 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1880 l_ptr->max_pkt = l_ptr->max_pkt_target;
1881 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001882 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884
Allan Stephens4d753132011-10-25 12:19:05 -04001885 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001886 if (!tipc_node_is_up(l_ptr->owner)) {
1887 l_ptr->owner->bclink.last_sent =
1888 l_ptr->owner->bclink.last_in =
1889 msg_last_bcast(msg);
1890 l_ptr->owner->bclink.oos_state = 0;
1891 }
Allan Stephens4d753132011-10-25 12:19:05 -04001892
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 l_ptr->peer_session = msg_session(msg);
1894 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001895
1896 if (msg_type(msg) == ACTIVATE_MSG)
1897 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898 break;
1899 case STATE_MSG:
1900
Allan Stephens2db99832010-12-31 18:59:33 +00001901 msg_tol = msg_link_tolerance(msg);
1902 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001904
1905 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001907 pr_warn("%s<%s>, priority change %u->%u\n",
1908 link_rst_msg, l_ptr->name, l_ptr->priority,
1909 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001911 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912 break;
1913 }
1914 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1915 l_ptr->stats.recv_states++;
1916 if (link_reset_unknown(l_ptr))
1917 break;
1918
1919 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001920 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921 mod(l_ptr->next_in_no));
1922 }
1923
1924 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001925 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001926 l_ptr->max_pkt = max_pkt_ack;
1927 l_ptr->max_pkt_probes = 0;
1928 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929
1930 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001931 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001933 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001934 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001935 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936
1937 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001938 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001939 tipc_bclink_update_link_state(l_ptr->owner,
1940 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941
1942 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001943 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
1944 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945 }
1946 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001948 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1949 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001950 }
1951 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952 }
1953exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001954 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955}
1956
1957
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001958/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1959 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001961static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1962 struct tipc_msg *tunnel_hdr,
1963 struct tipc_msg *msg,
1964 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001966 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967 struct sk_buff *buf;
1968 u32 length = msg_size(msg);
1969
1970 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001971 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001972 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001974 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001976 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001977 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001978 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001980 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001981 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1982 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01001983 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984}
1985
1986
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001987/* tipc_link_failover_send_queue(): A link has gone down, but a second
1988 * link is still active. We can do failover. Tunnel the failing link's
1989 * whole send queue via the remaining link. This way, we don't lose
1990 * any packets, and sequence order is preserved for subsequent traffic
1991 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001993void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994{
1995 u32 msgcount = l_ptr->out_queue_size;
1996 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001997 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001999 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000
2001 if (!tunnel)
2002 return;
2003
Allan Stephens5392d642006-06-25 23:52:50 -07002004 if (!l_ptr->owner->permit_changeover) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002005 pr_warn("%speer did not permit changeover\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002006 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002007 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002008
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002009 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002010 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2012 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002013
Per Lidenb97bf3f2006-01-02 19:04:38 +01002014 if (!l_ptr->first_out) {
2015 struct sk_buff *buf;
2016
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002017 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002019 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002021 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002023 pr_warn("%sunable to send changeover msg\n",
2024 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025 }
2026 return;
2027 }
Allan Stephensf1310722006-06-25 23:51:37 -07002028
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002029 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002030 l_ptr->owner->active_links[1]);
2031
Per Lidenb97bf3f2006-01-02 19:04:38 +01002032 while (crs) {
2033 struct tipc_msg *msg = buf_msg(crs);
2034
2035 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00002037 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038
Florian Westphald788d802007-08-02 19:28:06 -07002039 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00002041 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002042 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
2043 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 pos += align(msg_size(m));
2045 m = (struct tipc_msg *)pos;
2046 }
2047 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002048 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
2049 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002050 }
2051 crs = crs->next;
2052 }
2053}
2054
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002055/* tipc_link_dup_send_queue(): A second link has become active. Tunnel a
2056 * duplicate of the first link's send queue via the new link. This way, we
2057 * are guaranteed that currently queued packets from a socket are delivered
2058 * before future traffic from the same socket, even if this is using the
2059 * new link. The last arriving copy of each duplicate packet is dropped at
2060 * the receiving end by the regular protocol check, so packet cardinality
2061 * and sequence order is preserved per sender/receiver socket pair.
2062 * Owner node is locked.
2063 */
2064void tipc_link_dup_send_queue(struct tipc_link *l_ptr,
2065 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066{
2067 struct sk_buff *iter;
2068 struct tipc_msg tunnel_hdr;
2069
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002070 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002071 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2073 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2074 iter = l_ptr->first_out;
2075 while (iter) {
2076 struct sk_buff *outbuf;
2077 struct tipc_msg *msg = buf_msg(iter);
2078 u32 length = msg_size(msg);
2079
2080 if (msg_user(msg) == MSG_BUNDLER)
2081 msg_set_type(msg, CLOSED_MSG);
2082 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002083 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002085 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002087 pr_warn("%sunable to send duplicate msg\n",
2088 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 return;
2090 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002091 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2092 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2093 length);
Per Liden4323add2006-01-18 00:38:21 +01002094 tipc_link_send_buf(tunnel, outbuf);
2095 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 return;
2097 iter = iter->next;
2098 }
2099}
2100
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101/**
2102 * buf_extract - extracts embedded TIPC message from another message
2103 * @skb: encapsulating message buffer
2104 * @from_pos: offset to extract from
2105 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002106 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107 * encapsulating message itself is left unchanged.
2108 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002109static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2110{
2111 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2112 u32 size = msg_size(msg);
2113 struct sk_buff *eb;
2114
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002115 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002117 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002118 return eb;
2119}
2120
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002121/* tipc_link_tunnel_rcv(): Receive a tunneled packet, sent
2122 * via other link as result of a failover (ORIGINAL_MSG) or
2123 * a new active link (DUPLICATE_MSG). Failover packets are
2124 * returned to the active link for delivery upwards.
2125 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05002127static int tipc_link_tunnel_rcv(struct tipc_link **l_ptr,
2128 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002129{
2130 struct sk_buff *tunnel_buf = *buf;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002131 struct tipc_link *dest_link;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002132 struct tipc_msg *msg;
2133 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2134 u32 msg_typ = msg_type(tunnel_msg);
2135 u32 msg_count = msg_msgcnt(tunnel_msg);
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002136 u32 bearer_id = msg_bearer_id(tunnel_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002137
Dan Carpentercb4b102f2013-05-06 08:28:41 +00002138 if (bearer_id >= MAX_BEARERS)
2139 goto exit;
2140 dest_link = (*l_ptr)->owner->links[bearer_id];
Allan Stephensb29f1422010-12-31 18:59:25 +00002141 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002143 if (dest_link == *l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002144 pr_err("Unexpected changeover message on link <%s>\n",
2145 (*l_ptr)->name);
Allan Stephensf1310722006-06-25 23:51:37 -07002146 goto exit;
2147 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148 *l_ptr = dest_link;
2149 msg = msg_get_wrapped(tunnel_msg);
2150
2151 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002152 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002153 goto exit;
Allan Stephens0e659672010-12-31 18:59:32 +00002154 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155 if (*buf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002156 pr_warn("%sduplicate msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157 goto exit;
2158 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002159 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002160 return 1;
2161 }
2162
2163 /* First original message ?: */
Per Liden4323add2006-01-18 00:38:21 +01002164 if (tipc_link_is_up(dest_link)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002165 pr_info("%s<%s>, changeover initiated by peer\n", link_rst_msg,
2166 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002167 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168 dest_link->exp_msg_count = msg_count;
2169 if (!msg_count)
2170 goto exit;
2171 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002172 dest_link->exp_msg_count = msg_count;
2173 if (!msg_count)
2174 goto exit;
2175 }
2176
2177 /* Receive original message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002178 if (dest_link->exp_msg_count == 0) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002179 pr_warn("%sgot too many tunnelled messages\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002180 goto exit;
2181 }
2182 dest_link->exp_msg_count--;
2183 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 goto exit;
2185 } else {
2186 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2187 if (*buf != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002188 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002189 return 1;
2190 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002191 pr_warn("%soriginal msg dropped\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192 }
2193 }
2194exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002195 *buf = NULL;
Allan Stephens5f6d9122011-11-04 13:24:29 -04002196 kfree_skb(tunnel_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 return 0;
2198}
2199
2200/*
2201 * Bundler functionality:
2202 */
Per Liden4323add2006-01-18 00:38:21 +01002203void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002204{
2205 u32 msgcount = msg_msgcnt(buf_msg(buf));
2206 u32 pos = INT_H_SIZE;
2207 struct sk_buff *obuf;
2208
Per Lidenb97bf3f2006-01-02 19:04:38 +01002209 while (msgcount--) {
2210 obuf = buf_extract(buf, pos);
2211 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002212 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07002213 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002214 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002216 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002218 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002219}
2220
2221/*
2222 * Fragmentation/defragmentation:
2223 */
2224
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002225/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002226 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002227 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002228 * Returns user data length.
2229 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002230static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231{
Allan Stephens77561552011-04-17 13:06:23 -04002232 struct sk_buff *buf_chain = NULL;
2233 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002234 struct tipc_msg *inmsg = buf_msg(buf);
2235 struct tipc_msg fragm_hdr;
2236 u32 insize = msg_size(inmsg);
2237 u32 dsz = msg_data_sz(inmsg);
2238 unchar *crs = buf->data;
2239 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002240 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002241 u32 fragm_sz = pack_sz - INT_H_SIZE;
Allan Stephens77561552011-04-17 13:06:23 -04002242 u32 fragm_no = 0;
Allan Stephens9c396a72008-06-04 17:36:58 -07002243 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002244
2245 if (msg_short(inmsg))
2246 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002247 else
2248 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002249
Per Lidenb97bf3f2006-01-02 19:04:38 +01002250 /* Prepare reusable fragment header: */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002251 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002252 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002253
2254 /* Chop up message: */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002255 while (rest > 0) {
2256 struct sk_buff *fragm;
2257
2258 if (rest <= fragm_sz) {
2259 fragm_sz = rest;
2260 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2261 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002262 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002263 if (fragm == NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002264 kfree_skb(buf);
Ying Xued77b3832013-12-10 20:45:38 -08002265 kfree_skb_list(buf_chain);
Allan Stephens77561552011-04-17 13:06:23 -04002266 return -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 }
2268 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Allan Stephens77561552011-04-17 13:06:23 -04002269 fragm_no++;
2270 msg_set_fragm_no(&fragm_hdr, fragm_no);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002271 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2272 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2273 fragm_sz);
Allan Stephens77561552011-04-17 13:06:23 -04002274 buf_chain_tail->next = fragm;
2275 buf_chain_tail = fragm;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002276
Per Lidenb97bf3f2006-01-02 19:04:38 +01002277 rest -= fragm_sz;
2278 crs += fragm_sz;
2279 msg_set_type(&fragm_hdr, FRAGMENT);
2280 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04002281 kfree_skb(buf);
Allan Stephens77561552011-04-17 13:06:23 -04002282
2283 /* Append chain of fragments to send queue & send them */
Allan Stephens77561552011-04-17 13:06:23 -04002284 l_ptr->long_msg_seq_no++;
2285 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2286 l_ptr->stats.sent_fragments += fragm_no;
2287 l_ptr->stats.sent_fragmented++;
2288 tipc_link_push_queue(l_ptr);
2289
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290 return dsz;
2291}
2292
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002293/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002294 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002295 * the reassembled buffer if message is complete.
2296 */
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002297int tipc_link_recv_fragment(struct sk_buff **head, struct sk_buff **tail,
2298 struct sk_buff **fbuf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002299{
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002300 struct sk_buff *frag = *fbuf;
2301 struct tipc_msg *msg = buf_msg(frag);
2302 u32 fragid = msg_type(msg);
2303 bool headstolen;
2304 int delta;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002305
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002306 skb_pull(frag, msg_hdr_sz(msg));
2307 if (fragid == FIRST_FRAGMENT) {
2308 if (*head || skb_unclone(frag, GFP_ATOMIC))
2309 goto out_free;
2310 *head = frag;
2311 skb_frag_list_init(*head);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002312 return 0;
Erik Hugne3db0a192013-11-13 09:35:11 +01002313 } else if (*head &&
2314 skb_try_coalesce(*head, frag, &headstolen, &delta)) {
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002315 kfree_skb_partial(frag, headstolen);
2316 } else {
2317 if (!*head)
2318 goto out_free;
2319 if (!skb_has_frag_list(*head))
2320 skb_shinfo(*head)->frag_list = frag;
2321 else
2322 (*tail)->next = frag;
2323 *tail = frag;
2324 (*head)->truesize += frag->truesize;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002325 }
Erik Hugne40ba3cd2013-11-06 09:28:06 +01002326 if (fragid == LAST_FRAGMENT) {
2327 *fbuf = *head;
2328 *tail = *head = NULL;
2329 return LINK_REASM_COMPLETE;
2330 }
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);
2335 return LINK_REASM_ERROR;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336}
2337
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002338static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002339{
Allan Stephens5413b4c2011-01-18 13:24:55 -05002340 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2341 return;
2342
Per Lidenb97bf3f2006-01-02 19:04:38 +01002343 l_ptr->tolerance = tolerance;
2344 l_ptr->continuity_interval =
2345 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2346 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2347}
2348
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002349void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350{
2351 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002352 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2353 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2354 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2355 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002356 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002357 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2358 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2359 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2360 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002361 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2363 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2364 /* FRAGMENT and LAST_FRAGMENT packets */
2365 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2366}
2367
2368/**
2369 * link_find_link - locate link by name
Ben Hutchings2c530402012-07-10 10:55:09 +00002370 * @name: ptr to link name string
2371 * @node: ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002372 *
Per Liden4323add2006-01-18 00:38:21 +01002373 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002374 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002375 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002376 * Returns pointer to link (or 0 if invalid link name).
2377 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002378static struct tipc_link *link_find_link(const char *name,
2379 struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002381 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002382 struct tipc_node *n_ptr;
2383 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002384
Erik Hugnebbfbe472013-10-18 07:23:21 +02002385 list_for_each_entry(n_ptr, &tipc_node_list, list) {
2386 for (i = 0; i < MAX_BEARERS; i++) {
2387 l_ptr = n_ptr->links[i];
2388 if (l_ptr && !strcmp(l_ptr->name, name))
2389 goto found;
2390 }
2391 }
2392 l_ptr = NULL;
2393 n_ptr = NULL;
2394found:
2395 *node = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 return l_ptr;
2397}
2398
Allan Stephens5c216e12011-10-18 11:34:29 -04002399/**
2400 * link_value_is_valid -- validate proposed link tolerance/priority/window
2401 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002402 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2403 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002404 *
2405 * Returns 1 if value is within range, 0 if not.
2406 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002407static int link_value_is_valid(u16 cmd, u32 new_value)
2408{
2409 switch (cmd) {
2410 case TIPC_CMD_SET_LINK_TOL:
2411 return (new_value >= TIPC_MIN_LINK_TOL) &&
2412 (new_value <= TIPC_MAX_LINK_TOL);
2413 case TIPC_CMD_SET_LINK_PRI:
2414 return (new_value <= TIPC_MAX_LINK_PRI);
2415 case TIPC_CMD_SET_LINK_WINDOW:
2416 return (new_value >= TIPC_MIN_LINK_WIN) &&
2417 (new_value <= TIPC_MAX_LINK_WIN);
2418 }
2419 return 0;
2420}
2421
Allan Stephens5c216e12011-10-18 11:34:29 -04002422/**
2423 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002424 * @name: ptr to link, bearer, or media name
2425 * @new_value: new value of link, bearer, or media setting
2426 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002427 *
2428 * Caller must hold 'tipc_net_lock' to ensure link/bearer/media is not deleted.
2429 *
2430 * Returns 0 if value updated and negative value on error.
2431 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002432static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2433{
2434 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002435 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002436 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002437 struct tipc_media *m_ptr;
Ying Xue636c0372013-10-18 07:23:20 +02002438 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002439
2440 l_ptr = link_find_link(name, &node);
2441 if (l_ptr) {
2442 /*
2443 * acquire node lock for tipc_link_send_proto_msg().
2444 * see "TIPC locking policy" in net.c.
2445 */
2446 tipc_node_lock(node);
2447 switch (cmd) {
2448 case TIPC_CMD_SET_LINK_TOL:
2449 link_set_supervision_props(l_ptr, new_value);
2450 tipc_link_send_proto_msg(l_ptr,
2451 STATE_MSG, 0, 0, new_value, 0, 0);
2452 break;
2453 case TIPC_CMD_SET_LINK_PRI:
2454 l_ptr->priority = new_value;
2455 tipc_link_send_proto_msg(l_ptr,
2456 STATE_MSG, 0, 0, 0, new_value, 0);
2457 break;
2458 case TIPC_CMD_SET_LINK_WINDOW:
2459 tipc_link_set_queue_limits(l_ptr, new_value);
2460 break;
Ying Xue636c0372013-10-18 07:23:20 +02002461 default:
2462 res = -EINVAL;
2463 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002464 }
2465 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002466 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002467 }
2468
2469 b_ptr = tipc_bearer_find(name);
2470 if (b_ptr) {
2471 switch (cmd) {
2472 case TIPC_CMD_SET_LINK_TOL:
2473 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002474 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002475 case TIPC_CMD_SET_LINK_PRI:
2476 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002477 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002478 case TIPC_CMD_SET_LINK_WINDOW:
2479 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002480 break;
2481 default:
2482 res = -EINVAL;
2483 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002484 }
Ying Xue636c0372013-10-18 07:23:20 +02002485 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002486 }
2487
2488 m_ptr = tipc_media_find(name);
2489 if (!m_ptr)
2490 return -ENODEV;
2491 switch (cmd) {
2492 case TIPC_CMD_SET_LINK_TOL:
2493 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002494 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002495 case TIPC_CMD_SET_LINK_PRI:
2496 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002497 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002498 case TIPC_CMD_SET_LINK_WINDOW:
2499 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002500 break;
2501 default:
2502 res = -EINVAL;
2503 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002504 }
Ying Xue636c0372013-10-18 07:23:20 +02002505 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002506}
2507
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002508struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002509 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002510{
2511 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002512 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002513 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002514
2515 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002516 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002517
2518 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2519 new_value = ntohl(args->value);
2520
Allan Stephens5c216e12011-10-18 11:34:29 -04002521 if (!link_value_is_valid(cmd, new_value))
2522 return tipc_cfg_reply_error_string(
2523 "cannot change, value invalid");
2524
Per Liden4323add2006-01-18 00:38:21 +01002525 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002526 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002527 (tipc_bclink_set_queue_limits(new_value) == 0))
2528 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002529 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002530 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002531 }
2532
Per Liden4323add2006-01-18 00:38:21 +01002533 read_lock_bh(&tipc_net_lock);
Allan Stephens5c216e12011-10-18 11:34:29 -04002534 res = link_cmd_set_value(args->name, new_value, cmd);
Per Liden4323add2006-01-18 00:38:21 +01002535 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002536 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002537 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002538
Per Liden4323add2006-01-18 00:38:21 +01002539 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002540}
2541
2542/**
2543 * link_reset_statistics - reset link statistics
2544 * @l_ptr: pointer to link
2545 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002546static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002547{
2548 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2549 l_ptr->stats.sent_info = l_ptr->next_out_no;
2550 l_ptr->stats.recv_info = l_ptr->next_in_no;
2551}
2552
Per Liden4323add2006-01-18 00:38:21 +01002553struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554{
2555 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002556 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002557 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002558
2559 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002560 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561
2562 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002563 if (!strcmp(link_name, tipc_bclink_name)) {
2564 if (tipc_bclink_reset_stats())
2565 return tipc_cfg_reply_error_string("link not found");
2566 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567 }
2568
Per Liden4323add2006-01-18 00:38:21 +01002569 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002570 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002571 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002572 read_unlock_bh(&tipc_net_lock);
2573 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002574 }
2575
Per Liden4323add2006-01-18 00:38:21 +01002576 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002577 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002578 tipc_node_unlock(node);
2579 read_unlock_bh(&tipc_net_lock);
2580 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002581}
2582
2583/**
2584 * percent - convert count to a percentage of total (rounding up or down)
2585 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586static u32 percent(u32 count, u32 total)
2587{
2588 return (count * 100 + (total / 2)) / total;
2589}
2590
2591/**
Per Liden4323add2006-01-18 00:38:21 +01002592 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593 * @name: link name
2594 * @buf: print buffer area
2595 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002596 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002597 * Returns length of print buffer data string (or 0 if error)
2598 */
Per Liden4323add2006-01-18 00:38:21 +01002599static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002600{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002601 struct tipc_link *l;
2602 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002603 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002604 char *status;
2605 u32 profile_total = 0;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002606 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002607
Per Liden4323add2006-01-18 00:38:21 +01002608 if (!strcmp(name, tipc_bclink_name))
2609 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610
Per Liden4323add2006-01-18 00:38:21 +01002611 read_lock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002612 l = link_find_link(name, &node);
2613 if (!l) {
Per Liden4323add2006-01-18 00:38:21 +01002614 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002615 return 0;
2616 }
Per Liden4323add2006-01-18 00:38:21 +01002617 tipc_node_lock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002618 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002619
Erik Hugnedc1aed32012-06-29 00:50:23 -04002620 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002621 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002622 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002623 status = "STANDBY";
2624 else
2625 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002626
2627 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2628 " %s MTU:%u Priority:%u Tolerance:%u ms"
2629 " Window:%u packets\n",
2630 l->name, status, l->max_pkt, l->priority,
2631 l->tolerance, l->queue_limit[0]);
2632
2633 ret += tipc_snprintf(buf + ret, buf_size - ret,
2634 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2635 l->next_in_no - s->recv_info, s->recv_fragments,
2636 s->recv_fragmented, s->recv_bundles,
2637 s->recv_bundled);
2638
2639 ret += tipc_snprintf(buf + ret, buf_size - ret,
2640 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2641 l->next_out_no - s->sent_info, s->sent_fragments,
2642 s->sent_fragmented, s->sent_bundles,
2643 s->sent_bundled);
2644
2645 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002646 if (!profile_total)
2647 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002648
2649 ret += tipc_snprintf(buf + ret, buf_size - ret,
2650 " TX profile sample:%u packets average:%u octets\n"
2651 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2652 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2653 s->msg_length_counts,
2654 s->msg_lengths_total / profile_total,
2655 percent(s->msg_length_profile[0], profile_total),
2656 percent(s->msg_length_profile[1], profile_total),
2657 percent(s->msg_length_profile[2], profile_total),
2658 percent(s->msg_length_profile[3], profile_total),
2659 percent(s->msg_length_profile[4], profile_total),
2660 percent(s->msg_length_profile[5], profile_total),
2661 percent(s->msg_length_profile[6], profile_total));
2662
2663 ret += tipc_snprintf(buf + ret, buf_size - ret,
2664 " RX states:%u probes:%u naks:%u defs:%u"
2665 " dups:%u\n", s->recv_states, s->recv_probes,
2666 s->recv_nacks, s->deferred_recv, s->duplicates);
2667
2668 ret += tipc_snprintf(buf + ret, buf_size - ret,
2669 " TX states:%u probes:%u naks:%u acks:%u"
2670 " dups:%u\n", s->sent_states, s->sent_probes,
2671 s->sent_nacks, s->sent_acks, s->retransmitted);
2672
2673 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002674 " Congestion link:%u Send queue"
2675 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002676 s->max_queue_sz, s->queue_sz_counts ?
2677 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002678
Per Liden4323add2006-01-18 00:38:21 +01002679 tipc_node_unlock(node);
2680 read_unlock_bh(&tipc_net_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002681 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002682}
2683
Per Liden4323add2006-01-18 00:38:21 +01002684struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002685{
2686 struct sk_buff *buf;
2687 struct tlv_desc *rep_tlv;
2688 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002689 int pb_len;
2690 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002691
2692 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002693 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002694
Erik Hugnedc1aed32012-06-29 00:50:23 -04002695 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002696 if (!buf)
2697 return NULL;
2698
2699 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002700 pb = TLV_DATA(rep_tlv);
2701 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002702 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002703 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002704 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002705 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002706 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002707 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002708 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002709 skb_put(buf, TLV_SPACE(str_len));
2710 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2711
2712 return buf;
2713}
2714
Per Lidenb97bf3f2006-01-02 19:04:38 +01002715/**
Per Liden4323add2006-01-18 00:38:21 +01002716 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002717 * @dest: network address of destination node
2718 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002719 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002720 * If no active link can be found, uses default maximum packet size.
2721 */
Per Liden4323add2006-01-18 00:38:21 +01002722u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002723{
David S. Miller6c000552008-09-02 23:38:32 -07002724 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002725 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002726 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002727
Per Lidenb97bf3f2006-01-02 19:04:38 +01002728 if (dest == tipc_own_addr)
2729 return MAX_MSG_SIZE;
2730
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002731 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002732 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002733 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002734 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002735 l_ptr = n_ptr->active_links[selector & 1];
2736 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002737 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002738 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002739 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002740 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002741 return res;
2742}
2743
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002744static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002745{
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002746 pr_info("%s Link %x<%s>:", str, l_ptr->addr, l_ptr->b_ptr->name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002747
Per Lidenb97bf3f2006-01-02 19:04:38 +01002748 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002749 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002750 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002751 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002752 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002753 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002754 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002755 pr_cont(":WW\n");
2756 else
2757 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002758}