blob: e7f365012bd1be94f78ae3185e8f195c6f909378 [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"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040039#include "socket.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"
Richard Alpe0655f6a2014-11-20 10:29:07 +010043#include "netlink.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010044
Ying Xue796c75d2013-06-17 10:54:48 -040045#include <linux/pkt_sched.h>
46
Erik Hugne2cf8aa12012-06-29 00:16:37 -040047/*
48 * Error message prefixes
49 */
50static const char *link_co_err = "Link changeover error, ";
51static const char *link_rst_msg = "Resetting link ";
52static const char *link_unk_evt = "Unknown link event ";
Per Lidenb97bf3f2006-01-02 19:04:38 +010053
Richard Alpe0655f6a2014-11-20 10:29:07 +010054/* Properties valid for media, bearar and link */
55static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
56 [TIPC_NLA_PROP_UNSPEC] = { .type = NLA_UNSPEC },
57 [TIPC_NLA_PROP_PRIO] = { .type = NLA_U32 },
58 [TIPC_NLA_PROP_TOL] = { .type = NLA_U32 },
59 [TIPC_NLA_PROP_WIN] = { .type = NLA_U32 }
60};
61
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090062/*
Allan Stephensa686e682008-06-04 17:29:39 -070063 * Out-of-range value for link session numbers
64 */
Allan Stephensa686e682008-06-04 17:29:39 -070065#define INVALID_SESSION 0x10000
66
67/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090068 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010069 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010070#define STARTING_EVT 856384768 /* link processing trigger */
71#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
72#define TIMEOUT_EVT 560817u /* link timer expired */
73
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090074/*
75 * The following two 'message types' is really just implementation
76 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010077 * They must not be considered part of the protocol
78 */
79#define OPEN_MSG 0
80#define CLOSED_MSG 1
81
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090082/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010083 * State value stored in 'exp_msg_count'
84 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010085#define START_CHANGEOVER 100000u
86
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050087static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +010088 struct sk_buff *buf);
Ying Xue247f0f32014-02-18 16:06:46 +080089static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Paul Maloy3bb53382014-02-13 17:29:12 -050090static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -050091 struct sk_buff **buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050092static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -050093static void link_state_event(struct tipc_link *l_ptr, u32 event);
94static void link_reset_statistics(struct tipc_link *l_ptr);
95static void link_print(struct tipc_link *l_ptr, const char *str);
Ying Xue247f0f32014-02-18 16:06:46 +080096static void tipc_link_sync_xmit(struct tipc_link *l);
97static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +020098static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf);
99static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000100
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800102 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800104static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105{
106 return (i + 3) & ~3u;
107}
108
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500109static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800111 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900113
Ying Xue7a2f7d12014-04-21 10:55:46 +0800114 rcu_read_lock();
115 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
116 if (!b_ptr) {
117 rcu_read_unlock();
118 return;
119 }
120 max_pkt = (b_ptr->mtu & ~3);
121 rcu_read_unlock();
122
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 if (max_pkt > MAX_MSG_SIZE)
124 max_pkt = MAX_MSG_SIZE;
125
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900126 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
128 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900129 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130 l_ptr->max_pkt = MAX_PKT_DEFAULT;
131
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900132 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133}
134
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500135static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136{
137 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400138 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 return mod(l_ptr->next_out_no);
140}
141
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500142static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143{
144 return mod(link_next_sent(l_ptr) - 1);
145}
146
147/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800148 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500150int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151{
152 if (!l_ptr)
153 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000154 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155}
156
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500157int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000159 return (l_ptr->owner->active_links[0] == l_ptr) ||
160 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161}
162
163/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 * link_timeout - handle expiration of link timer
165 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500167static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168{
Per Liden4323add2006-01-18 00:38:21 +0100169 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170
171 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
173 l_ptr->stats.queue_sz_counts++;
174
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 if (l_ptr->first_out) {
176 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
177 u32 length = msg_size(msg);
178
Joe Perchesf64f9e72009-11-29 16:55:45 -0800179 if ((msg_user(msg) == MSG_FRAGMENTER) &&
180 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 length = msg_size(msg_get_wrapped(msg));
182 }
183 if (length) {
184 l_ptr->stats.msg_lengths_total += length;
185 l_ptr->stats.msg_length_counts++;
186 if (length <= 64)
187 l_ptr->stats.msg_length_profile[0]++;
188 else if (length <= 256)
189 l_ptr->stats.msg_length_profile[1]++;
190 else if (length <= 1024)
191 l_ptr->stats.msg_length_profile[2]++;
192 else if (length <= 4096)
193 l_ptr->stats.msg_length_profile[3]++;
194 else if (length <= 16384)
195 l_ptr->stats.msg_length_profile[4]++;
196 else if (length <= 32768)
197 l_ptr->stats.msg_length_profile[5]++;
198 else
199 l_ptr->stats.msg_length_profile[6]++;
200 }
201 }
202
203 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204
205 link_state_event(l_ptr, TIMEOUT_EVT);
206
207 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100208 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209
Per Liden4323add2006-01-18 00:38:21 +0100210 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211}
212
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500213static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214{
215 k_start_timer(&l_ptr->timer, time);
216}
217
218/**
Per Liden4323add2006-01-18 00:38:21 +0100219 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500220 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 * Returns pointer to link.
225 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500226struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500227 struct tipc_bearer *b_ptr,
228 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500230 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 struct tipc_msg *msg;
232 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500233 char addr_string[16];
234 u32 peer = n_ptr->addr;
235
Holger Brunck0372bf52014-11-14 18:33:19 +0100236 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500237 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100238 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
239 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500240 return NULL;
241 }
242
243 if (n_ptr->links[b_ptr->identity]) {
244 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400245 pr_err("Attempt to establish second link on <%s> to %s\n",
246 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500247 return NULL;
248 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700250 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400252 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 return NULL;
254 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255
256 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500257 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400258 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900260 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 if_name,
262 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400263 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500265 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 l_ptr->checkpoint = 1;
Allan Stephensf882cb72011-04-07 09:43:27 -0400267 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800268 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400269 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 l_ptr->state = RESET_UNKNOWN;
271
272 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
273 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000274 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700276 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 msg_set_bearer_id(msg, b_ptr->identity);
278 strcpy((char *)msg_data(msg), if_name);
279
280 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400281 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
Ying Xue7a2f7d12014-04-21 10:55:46 +0800283 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 link_init_max_pkt(l_ptr);
285
286 l_ptr->next_out_no = 1;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400287 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288
289 link_reset_statistics(l_ptr);
290
Allan Stephens37b9c082011-02-28 11:32:27 -0500291 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500293 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
294 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500295
296 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 return l_ptr;
299}
300
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500301void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500302{
303 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500304 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500305
Ying Xue6c7a7622014-03-27 12:54:37 +0800306 rcu_read_lock();
307 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800308 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500309 l_ptr = n_ptr->links[bearer_id];
310 if (l_ptr) {
311 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500312 if (shutting_down || !tipc_node_is_up(n_ptr)) {
313 tipc_node_detach_link(l_ptr->owner, l_ptr);
314 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800315 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500316
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500317 /* Nobody else can access this link now: */
318 del_timer_sync(&l_ptr->timer);
319 kfree(l_ptr);
320 } else {
321 /* Detach/delete when failover is finished: */
322 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800323 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500324 del_timer_sync(&l_ptr->timer);
325 }
Ying Xuec61dd612014-02-13 17:29:09 -0500326 continue;
327 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800328 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500329 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800330 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500331}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332
333/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400334 * link_schedule_user - schedule user for wakeup after congestion
335 * @link: congested link
336 * @oport: sending port
337 * @chain_sz: size of buffer chain that was attempted sent
338 * @imp: importance of message attempted sent
339 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400341static bool link_schedule_user(struct tipc_link *link, u32 oport,
342 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400344 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400346 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
347 tipc_own_addr, oport, 0, 0);
348 if (!buf)
349 return false;
350 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
351 TIPC_SKB_CB(buf)->chain_imp = imp;
352 __skb_queue_tail(&link->waiting_sks, buf);
353 link->stats.link_congs++;
354 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355}
356
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400357/**
358 * link_prepare_wakeup - prepare users for wakeup after congestion
359 * @link: congested link
360 * Move a number of waiting users, as permitted by available space in
361 * the send queue, from link wait queue to node wait queue for wakeup
362 */
363static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400365 struct sk_buff_head *wq = &link->waiting_sks;
366 struct sk_buff *buf;
367 uint pend_qsz = link->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400369 for (buf = skb_peek(wq); buf; buf = skb_peek(wq)) {
370 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(buf)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400372 pend_qsz += TIPC_SKB_CB(buf)->chain_sz;
373 __skb_queue_tail(&link->owner->waiting_sks, __skb_dequeue(wq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375}
376
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900377/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 * link_release_outqueue - purge link's outbound message queue
379 * @l_ptr: pointer to link
380 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500381static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382{
Ying Xued77b3832013-12-10 20:45:38 -0800383 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 l_ptr->first_out = NULL;
385 l_ptr->out_queue_size = 0;
386}
387
388/**
Per Liden4323add2006-01-18 00:38:21 +0100389 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 * @l_ptr: pointer to link
391 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500392void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400394 kfree_skb(l_ptr->reasm_buf);
395 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396}
397
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500399 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 * @l_ptr: pointer to link
401 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500402void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403{
Ying Xued77b3832013-12-10 20:45:38 -0800404 kfree_skb_list(l_ptr->oldest_deferred_in);
405 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100406 tipc_link_reset_fragments(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400407 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 l_ptr->proto_msg_queue = NULL;
409}
410
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500411void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 u32 prev_state = l_ptr->state;
414 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700415 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400416 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900417
Allan Stephensa686e682008-06-04 17:29:39 -0700418 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419
Allan Stephensa686e682008-06-04 17:29:39 -0700420 /* Link is down, accept any session */
421 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900423 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427
428 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
429 return;
430
Per Liden4323add2006-01-18 00:38:21 +0100431 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800432 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000433
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500434 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 l_ptr->reset_checkpoint = checkpoint;
436 l_ptr->exp_msg_count = START_CHANGEOVER;
437 }
438
439 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 link_release_outqueue(l_ptr);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400441 kfree_skb(l_ptr->proto_msg_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 l_ptr->proto_msg_queue = NULL;
Ying Xued77b3832013-12-10 20:45:38 -0800443 kfree_skb_list(l_ptr->oldest_deferred_in);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400444 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
445 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
446 owner->action_flags |= TIPC_WAKEUP_USERS;
447 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 l_ptr->retransm_queue_head = 0;
449 l_ptr->retransm_queue_size = 0;
450 l_ptr->last_out = NULL;
451 l_ptr->first_out = NULL;
452 l_ptr->next_out = NULL;
453 l_ptr->unacked_window = 0;
454 l_ptr->checkpoint = 1;
455 l_ptr->next_out_no = 1;
456 l_ptr->deferred_inqueue_sz = 0;
457 l_ptr->oldest_deferred_in = NULL;
458 l_ptr->newest_deferred_in = NULL;
459 l_ptr->fsm_msg_cnt = 0;
460 l_ptr->stale_count = 0;
461 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462}
463
Ying Xuec61dd612014-02-13 17:29:09 -0500464void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500465{
466 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500467 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500468
Ying Xue6c7a7622014-03-27 12:54:37 +0800469 rcu_read_lock();
470 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800471 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500472 l_ptr = n_ptr->links[bearer_id];
473 if (l_ptr)
474 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800475 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500476 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800477 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500478}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500480static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481{
Allan Stephens5392d642006-06-25 23:52:50 -0700482 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100483 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800484 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485}
486
487/**
488 * link_state_event - link finite state machine
489 * @l_ptr: pointer to link
490 * @event: state machine event to process
491 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000492static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500494 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 u32 cont_intv = l_ptr->continuity_interval;
496
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500497 if (l_ptr->flags & LINK_STOPPED)
498 return;
499
Ying Xue135daee2014-02-13 17:29:08 -0500500 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 return; /* Not yet. */
502
Ying Xue77a7e072013-12-10 20:45:44 -0800503 /* Check whether changeover is going on */
504 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000505 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800507 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509
510 switch (l_ptr->state) {
511 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 switch (event) {
513 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 break;
516 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 if (l_ptr->next_in_no != l_ptr->checkpoint) {
518 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100519 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800520 tipc_link_proto_xmit(l_ptr, STATE_MSG,
521 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 l_ptr->fsm_msg_cnt++;
523 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800524 tipc_link_proto_xmit(l_ptr, STATE_MSG,
525 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 l_ptr->fsm_msg_cnt++;
527 }
528 link_set_timer(l_ptr, cont_intv);
529 break;
530 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 l_ptr->state = WORKING_UNKNOWN;
532 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800533 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 l_ptr->fsm_msg_cnt++;
535 link_set_timer(l_ptr, cont_intv / 4);
536 break;
537 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400538 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
539 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100540 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 l_ptr->state = RESET_RESET;
542 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800543 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
544 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 l_ptr->fsm_msg_cnt++;
546 link_set_timer(l_ptr, cont_intv);
547 break;
548 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400549 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 }
551 break;
552 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 switch (event) {
554 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 l_ptr->state = WORKING_WORKING;
557 l_ptr->fsm_msg_cnt = 0;
558 link_set_timer(l_ptr, cont_intv);
559 break;
560 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400561 pr_info("%s<%s>, requested by peer while probing\n",
562 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100563 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 l_ptr->state = RESET_RESET;
565 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800566 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
567 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 l_ptr->fsm_msg_cnt++;
569 link_set_timer(l_ptr, cont_intv);
570 break;
571 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 l_ptr->state = WORKING_WORKING;
574 l_ptr->fsm_msg_cnt = 0;
575 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100576 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800577 tipc_link_proto_xmit(l_ptr, STATE_MSG,
578 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 l_ptr->fsm_msg_cnt++;
580 }
581 link_set_timer(l_ptr, cont_intv);
582 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800583 tipc_link_proto_xmit(l_ptr, STATE_MSG,
584 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 l_ptr->fsm_msg_cnt++;
586 link_set_timer(l_ptr, cont_intv / 4);
587 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400588 pr_warn("%s<%s>, peer not responding\n",
589 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100590 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 l_ptr->state = RESET_UNKNOWN;
592 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800593 tipc_link_proto_xmit(l_ptr, RESET_MSG,
594 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 l_ptr->fsm_msg_cnt++;
596 link_set_timer(l_ptr, cont_intv);
597 }
598 break;
599 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400600 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 }
602 break;
603 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 switch (event) {
605 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 break;
607 case ACTIVATE_MSG:
608 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000609 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 l_ptr->state = WORKING_WORKING;
612 l_ptr->fsm_msg_cnt = 0;
613 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800614 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800616 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800617 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 link_set_timer(l_ptr, cont_intv);
619 break;
620 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 l_ptr->state = RESET_RESET;
622 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800623 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
624 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 l_ptr->fsm_msg_cnt++;
626 link_set_timer(l_ptr, cont_intv);
627 break;
628 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500629 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 /* fall through */
631 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800632 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 l_ptr->fsm_msg_cnt++;
634 link_set_timer(l_ptr, cont_intv);
635 break;
636 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400637 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 }
639 break;
640 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 switch (event) {
642 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 case ACTIVATE_MSG:
644 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000645 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 l_ptr->state = WORKING_WORKING;
648 l_ptr->fsm_msg_cnt = 0;
649 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800650 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800652 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800653 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 link_set_timer(l_ptr, cont_intv);
655 break;
656 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 break;
658 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800659 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
660 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 l_ptr->fsm_msg_cnt++;
662 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 break;
664 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400665 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 }
667 break;
668 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400669 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 }
671}
672
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500673/* tipc_link_cong: determine return value and how to treat the
674 * sent buffer during link congestion.
675 * - For plain, errorless user data messages we keep the buffer and
676 * return -ELINKONG.
677 * - For all other messages we discard the buffer and return -EHOSTUNREACH
678 * - For TIPC internal messages we also reset the link
679 */
680static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
681{
682 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500683 uint imp = tipc_msg_tot_importance(msg);
684 u32 oport = msg_tot_origport(msg);
685
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400686 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500687 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
688 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400689 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500690 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400691 if (unlikely(msg_errcode(msg)))
692 goto drop;
693 if (unlikely(msg_reroute_cnt(msg)))
694 goto drop;
695 if (TIPC_SKB_CB(buf)->wakeup_pending)
696 return -ELINKCONG;
697 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
698 return -ELINKCONG;
699drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500700 kfree_skb_list(buf);
701 return -EHOSTUNREACH;
702}
703
704/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400705 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500706 * @link: link to use
707 * @buf: chain of buffers containing message
708 * Consumes the buffer chain, except when returning -ELINKCONG
709 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
710 * user data messages) or -EHOSTUNREACH (all other messages/senders)
711 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
712 * to act on the return value, since they may need to do more send attempts.
713 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400714int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *buf)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500715{
716 struct tipc_msg *msg = buf_msg(buf);
717 uint psz = msg_size(msg);
718 uint qsz = link->out_queue_size;
719 uint sndlim = link->queue_limit[0];
720 uint imp = tipc_msg_tot_importance(msg);
721 uint mtu = link->max_pkt;
722 uint ack = mod(link->next_in_no - 1);
723 uint seqno = link->next_out_no;
724 uint bc_last_in = link->owner->bclink.last_in;
725 struct tipc_media_addr *addr = &link->media_addr;
726 struct sk_buff *next = buf->next;
727
728 /* Match queue limits against msg importance: */
729 if (unlikely(qsz >= link->queue_limit[imp]))
730 return tipc_link_cong(link, buf);
731
732 /* Has valid packet limit been used ? */
733 if (unlikely(psz > mtu)) {
734 kfree_skb_list(buf);
735 return -EMSGSIZE;
736 }
737
738 /* Prepare each packet for sending, and add to outqueue: */
739 while (buf) {
740 next = buf->next;
741 msg = buf_msg(buf);
742 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
743 msg_set_bcast_ack(msg, bc_last_in);
744
745 if (!link->first_out) {
746 link->first_out = buf;
747 } else if (qsz < sndlim) {
748 link->last_out->next = buf;
749 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
750 link->stats.sent_bundled++;
751 buf = next;
752 next = buf->next;
753 continue;
754 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
755 link->stats.sent_bundled++;
756 link->stats.sent_bundles++;
757 link->last_out->next = buf;
758 if (!link->next_out)
759 link->next_out = buf;
760 } else {
761 link->last_out->next = buf;
762 if (!link->next_out)
763 link->next_out = buf;
764 }
765
766 /* Send packet if possible: */
767 if (likely(++qsz <= sndlim)) {
768 tipc_bearer_send(link->bearer_id, buf, addr);
769 link->next_out = next;
770 link->unacked_window = 0;
771 }
772 seqno++;
773 link->last_out = buf;
774 buf = next;
775 }
776 link->next_out_no = seqno;
777 link->out_queue_size = qsz;
778 return 0;
779}
780
781/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400782 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500783 * @buf: chain of buffers containing message
784 * @dsz: amount of user data to be sent
785 * @dnode: address of destination node
786 * @selector: a number used for deterministic link selection
787 * Consumes the buffer chain, except when returning -ELINKCONG
788 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
789 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400790int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500791{
792 struct tipc_link *link = NULL;
793 struct tipc_node *node;
794 int rc = -EHOSTUNREACH;
795
796 node = tipc_node_find(dnode);
797 if (node) {
798 tipc_node_lock(node);
799 link = node->active_links[selector & 1];
800 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400801 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500802 tipc_node_unlock(node);
803 }
804
805 if (link)
806 return rc;
807
808 if (likely(in_own_node(dnode)))
809 return tipc_sk_rcv(buf);
810
811 kfree_skb_list(buf);
812 return rc;
813}
814
Jon Maloyc64f7a62012-11-16 13:51:31 +0800815/*
Ying Xue247f0f32014-02-18 16:06:46 +0800816 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800817 *
818 * Give a newly added peer node the sequence number where it should
819 * start receiving and acking broadcast packets.
820 *
821 * Called with node locked
822 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400823static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800824{
825 struct sk_buff *buf;
826 struct tipc_msg *msg;
827
828 buf = tipc_buf_acquire(INT_H_SIZE);
829 if (!buf)
830 return;
831
832 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400833 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
834 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400835 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800836}
837
838/*
Ying Xue247f0f32014-02-18 16:06:46 +0800839 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800840 * Receive the sequence number where we should start receiving and
841 * acking broadcast packets from a newly added peer node, and open
842 * up for reception of such packets.
843 *
844 * Called with node locked
845 */
Ying Xue247f0f32014-02-18 16:06:46 +0800846static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800847{
848 struct tipc_msg *msg = buf_msg(buf);
849
850 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
851 n->bclink.recv_permitted = true;
852 kfree_skb(buf);
853}
854
855/*
Per Liden4323add2006-01-18 00:38:21 +0100856 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 */
stephen hemminger98056962014-01-04 13:47:48 -0800858static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859{
860 struct sk_buff *buf = l_ptr->first_out;
861 u32 r_q_size = l_ptr->retransm_queue_size;
862 u32 r_q_head = l_ptr->retransm_queue_head;
863
864 /* Step to position where retransmission failed, if any, */
865 /* consider that buffers may have been released in meantime */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900867 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 link_last_sent(l_ptr));
Allan Stephensf9057302011-10-24 16:03:12 -0400869 u32 first = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870
871 while (buf && less(first, r_q_head)) {
872 first = mod(first + 1);
873 buf = buf->next;
874 }
875 l_ptr->retransm_queue_head = r_q_head = first;
876 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
877 }
878
879 /* Continue retransmission now, if there is anything: */
Neil Hormanca509102010-03-15 07:58:45 +0000880 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900882 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800883 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800884 l_ptr->retransm_queue_head = mod(++r_q_head);
885 l_ptr->retransm_queue_size = --r_q_size;
886 l_ptr->stats.retransmitted++;
887 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 }
889
890 /* Send deferred protocol message, if any: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 buf = l_ptr->proto_msg_queue;
892 if (buf) {
893 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +0000894 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800895 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800896 l_ptr->unacked_window = 0;
897 kfree_skb(buf);
898 l_ptr->proto_msg_queue = NULL;
899 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 }
901
902 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 buf = l_ptr->next_out;
904 if (buf) {
905 struct tipc_msg *msg = buf_msg(buf);
906 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -0400907 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908
909 if (mod(next - first) < l_ptr->queue_limit[0]) {
910 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900911 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800912 tipc_bearer_send(l_ptr->bearer_id, buf,
913 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800914 if (msg_user(msg) == MSG_BUNDLER)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500915 msg_set_type(msg, BUNDLE_CLOSED);
Ying Xue3c294cb2012-11-15 11:34:45 +0800916 l_ptr->next_out = buf->next;
917 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 }
919 }
Ying Xue3c294cb2012-11-15 11:34:45 +0800920 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921}
922
923/*
924 * push_queue(): push out the unsent messages of a link where
925 * congestion has abated. Node is locked
926 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500927void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928{
929 u32 res;
930
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 do {
Per Liden4323add2006-01-18 00:38:21 +0100932 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700933 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934}
935
Ying Xue3f5a12b2014-05-05 08:56:17 +0800936void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700937{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700938 char addr_string[16];
939 u32 i;
940
Ying Xue3f5a12b2014-05-05 08:56:17 +0800941 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700942
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400943 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800944 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700945
946 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800947 if (node->links[i]) {
948 link_print(node->links[i], "Resetting link\n");
949 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700950 }
951 }
952
Ying Xue3f5a12b2014-05-05 08:56:17 +0800953 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700954}
955
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500956static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400957 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700958{
959 struct tipc_msg *msg = buf_msg(buf);
960
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400961 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700962
963 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700964 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000965 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700966 tipc_link_reset(l_ptr);
967
968 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700969 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700970 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700971 char addr_string[16];
972
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400973 pr_info("Msg seq number: %u, ", msg_seqno(msg));
974 pr_cont("Outstanding acks: %lu\n",
975 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700976
Allan Stephens01d83ed2011-01-18 13:53:16 -0500977 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700978 tipc_node_lock(n_ptr);
979
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000980 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400981 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800982 pr_info("Reception permitted: %d, Acked: %u\n",
983 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400984 n_ptr->bclink.acked);
985 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
986 n_ptr->bclink.last_in,
987 n_ptr->bclink.oos_state,
988 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700989
Allan Stephensd356eeb2006-06-25 23:40:01 -0700990 tipc_node_unlock(n_ptr);
991
Ying Xue3f5a12b2014-05-05 08:56:17 +0800992 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700993 l_ptr->stale_count = 0;
994 }
995}
996
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500997void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +0100998 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999{
1000 struct tipc_msg *msg;
1001
Allan Stephensd356eeb2006-06-25 23:40:01 -07001002 if (!buf)
1003 return;
1004
1005 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001006
Erik Hugne512137e2013-12-06 10:08:00 -05001007 /* Detect repeated retransmit failures */
1008 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1009 if (++l_ptr->stale_count > 100) {
1010 link_retransmit_failure(l_ptr, buf);
1011 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001012 }
1013 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001014 l_ptr->last_retransmitted = msg_seqno(msg);
1015 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001017
Neil Hormanca509102010-03-15 07:58:45 +00001018 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 msg = buf_msg(buf);
1020 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001022 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001023 buf = buf->next;
1024 retransmits--;
1025 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001027
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1029}
1030
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 * link_insert_deferred_queue - insert deferred messages back into receive chain
1033 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001034static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 struct sk_buff *buf)
1036{
1037 u32 seq_no;
1038
1039 if (l_ptr->oldest_deferred_in == NULL)
1040 return buf;
1041
Allan Stephensf9057302011-10-24 16:03:12 -04001042 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 if (seq_no == mod(l_ptr->next_in_no)) {
1044 l_ptr->newest_deferred_in->next = buf;
1045 buf = l_ptr->oldest_deferred_in;
1046 l_ptr->oldest_deferred_in = NULL;
1047 l_ptr->deferred_inqueue_sz = 0;
1048 }
1049 return buf;
1050}
1051
Allan Stephens85035562008-04-15 19:04:54 -07001052/**
1053 * link_recv_buf_validate - validate basic format of received message
1054 *
1055 * This routine ensures a TIPC message has an acceptable header, and at least
1056 * as much data as the header indicates it should. The routine also ensures
1057 * that the entire message header is stored in the main fragment of the message
1058 * buffer, to simplify future access to message header fields.
1059 *
1060 * Note: Having extra info present in the message header or data areas is OK.
1061 * TIPC will ignore the excess, under the assumption that it is optional info
1062 * introduced by a later release of the protocol.
1063 */
Allan Stephens85035562008-04-15 19:04:54 -07001064static int link_recv_buf_validate(struct sk_buff *buf)
1065{
1066 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001067 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001068 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1069 };
1070
1071 struct tipc_msg *msg;
1072 u32 tipc_hdr[2];
1073 u32 size;
1074 u32 hdr_size;
1075 u32 min_hdr_size;
1076
Erik Hugne64380a02014-02-11 11:38:26 +01001077 /* If this packet comes from the defer queue, the skb has already
1078 * been validated
1079 */
1080 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1081 return 1;
1082
Allan Stephens85035562008-04-15 19:04:54 -07001083 if (unlikely(buf->len < MIN_H_SIZE))
1084 return 0;
1085
1086 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1087 if (msg == NULL)
1088 return 0;
1089
1090 if (unlikely(msg_version(msg) != TIPC_VERSION))
1091 return 0;
1092
1093 size = msg_size(msg);
1094 hdr_size = msg_hdr_sz(msg);
1095 min_hdr_size = msg_isdata(msg) ?
1096 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1097
1098 if (unlikely((hdr_size < min_hdr_size) ||
1099 (size < hdr_size) ||
1100 (buf->len < size) ||
1101 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1102 return 0;
1103
1104 return pskb_may_pull(buf, hdr_size);
1105}
1106
Allan Stephensb02b69c2010-08-17 11:00:07 +00001107/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001108 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001109 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001110 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001111 *
1112 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1113 * structure (i.e. cannot be NULL), but bearer can be inactive.
1114 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001115void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001118 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001119 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 struct sk_buff *crs;
1121 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001122 struct tipc_msg *msg;
1123 u32 seq_no;
1124 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125 u32 released = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001128 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001129
1130 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001131 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001132 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133
Allan Stephensfe13dda2008-04-15 19:03:23 -07001134 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001135 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001136 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001137
Allan Stephens85035562008-04-15 19:04:54 -07001138 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001139 msg = buf_msg(buf);
1140
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001142 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001143 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001144 else
Ying Xue247f0f32014-02-18 16:06:46 +08001145 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 continue;
1147 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001148
Allan Stephensed33a9c2011-04-05 15:15:04 -04001149 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001150 if (unlikely(!msg_short(msg) &&
1151 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001152 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001153
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001154 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001155 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001157 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001158 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001159
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001160 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001162 if (unlikely(!l_ptr))
1163 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001164
Allan Stephensb4b56102011-05-27 11:00:51 -04001165 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001166 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001167 msg_user(msg) == LINK_PROTOCOL &&
1168 (msg_type(msg) == RESET_MSG ||
1169 msg_type(msg) == ACTIVATE_MSG) &&
1170 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001171 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001172
Ying Xue10f465c2014-05-05 08:56:11 +08001173 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001174 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001175
Allan Stephens85035562008-04-15 19:04:54 -07001176 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001177 seq_no = msg_seqno(msg);
1178 ackd = msg_ack(msg);
1179
1180 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001181 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001182 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183
1184 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001186 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 struct sk_buff *next = crs->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001188 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 crs = next;
1190 released++;
1191 }
1192 if (released) {
1193 l_ptr->first_out = crs;
1194 l_ptr->out_queue_size -= released;
1195 }
Allan Stephens85035562008-04-15 19:04:54 -07001196
1197 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001199 tipc_link_push_queue(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001200
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001201 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1202 link_prepare_wakeup(l_ptr);
1203 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1204 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001205
Jon Paul Maloya5377832014-02-13 17:29:15 -05001206 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001207 if (unlikely(!link_working_working(l_ptr))) {
1208 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001209 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001210 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001211 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 continue;
1213 }
Ying Xue3af390e2013-10-30 11:26:57 +08001214
1215 /* Traffic message. Conditionally activate link */
1216 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1217
1218 if (link_working_working(l_ptr)) {
1219 /* Re-insert buffer in front of queue */
1220 buf->next = head;
1221 head = buf;
1222 tipc_node_unlock(n_ptr);
1223 continue;
1224 }
1225 goto unlock_discard;
1226 }
1227
1228 /* Link is now in state WORKING_WORKING */
1229 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 link_handle_out_of_seq_msg(l_ptr, buf);
1231 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001232 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 continue;
1234 }
Ying Xue3af390e2013-10-30 11:26:57 +08001235 l_ptr->next_in_no++;
1236 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001238
Erik Hugne3f53bd82014-07-01 10:22:41 +02001239 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1240 l_ptr->stats.sent_acks++;
1241 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1242 }
1243
Erik Hugne7ae934b2014-07-01 10:22:40 +02001244 if (tipc_link_prepare_input(l_ptr, &buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001245 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 }
Per Liden4323add2006-01-18 00:38:21 +01001248 tipc_node_unlock(n_ptr);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001249 msg = buf_msg(buf);
1250 if (tipc_link_input(l_ptr, buf) != 0)
1251 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001252 continue;
1253unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001254 tipc_node_unlock(n_ptr);
1255discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001256 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258}
1259
Ben Hutchings2c530402012-07-10 10:55:09 +00001260/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001261 * tipc_link_prepare_input - process TIPC link messages
1262 *
1263 * returns nonzero if the message was consumed
1264 *
1265 * Node lock must be held
1266 */
1267static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1268{
1269 struct tipc_node *n;
1270 struct tipc_msg *msg;
1271 int res = -EINVAL;
1272
1273 n = l->owner;
1274 msg = buf_msg(*buf);
1275 switch (msg_user(msg)) {
1276 case CHANGEOVER_PROTOCOL:
1277 if (tipc_link_tunnel_rcv(n, buf))
1278 res = 0;
1279 break;
1280 case MSG_FRAGMENTER:
1281 l->stats.recv_fragments++;
1282 if (tipc_buf_append(&l->reasm_buf, buf)) {
1283 l->stats.recv_fragmented++;
1284 res = 0;
1285 } else if (!l->reasm_buf) {
1286 tipc_link_reset(l);
1287 }
1288 break;
1289 case MSG_BUNDLER:
1290 l->stats.recv_bundles++;
1291 l->stats.recv_bundled += msg_msgcnt(msg);
1292 res = 0;
1293 break;
1294 case NAME_DISTRIBUTOR:
1295 n->bclink.recv_permitted = true;
1296 res = 0;
1297 break;
1298 case BCAST_PROTOCOL:
1299 tipc_link_sync_rcv(n, *buf);
1300 break;
1301 default:
1302 res = 0;
1303 }
1304 return res;
1305}
1306/**
1307 * tipc_link_input - Deliver message too higher layers
1308 */
1309static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1310{
1311 struct tipc_msg *msg = buf_msg(buf);
1312 int res = 0;
1313
1314 switch (msg_user(msg)) {
1315 case TIPC_LOW_IMPORTANCE:
1316 case TIPC_MEDIUM_IMPORTANCE:
1317 case TIPC_HIGH_IMPORTANCE:
1318 case TIPC_CRITICAL_IMPORTANCE:
1319 case CONN_MANAGER:
1320 tipc_sk_rcv(buf);
1321 break;
1322 case NAME_DISTRIBUTOR:
1323 tipc_named_rcv(buf);
1324 break;
1325 case MSG_BUNDLER:
1326 tipc_link_bundle_rcv(buf);
1327 break;
1328 default:
1329 res = -EINVAL;
1330 }
1331 return res;
1332}
1333
1334/**
Allan Stephens8809b252011-10-25 10:44:35 -04001335 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1336 *
1337 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 */
Allan Stephens8809b252011-10-25 10:44:35 -04001339u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001340 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341{
Allan Stephens8809b252011-10-25 10:44:35 -04001342 struct sk_buff *queue_buf;
1343 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001344 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345
1346 buf->next = NULL;
1347
1348 /* Empty queue ? */
1349 if (*head == NULL) {
1350 *head = *tail = buf;
1351 return 1;
1352 }
1353
1354 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001355 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 (*tail)->next = buf;
1357 *tail = buf;
1358 return 1;
1359 }
1360
Allan Stephens8809b252011-10-25 10:44:35 -04001361 /* Locate insertion point in queue, then insert; discard if duplicate */
1362 prev = head;
1363 queue_buf = *head;
1364 for (;;) {
1365 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366
Allan Stephens8809b252011-10-25 10:44:35 -04001367 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001368 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001369 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 }
Allan Stephens8809b252011-10-25 10:44:35 -04001371
1372 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374
Allan Stephens8809b252011-10-25 10:44:35 -04001375 prev = &queue_buf->next;
1376 queue_buf = queue_buf->next;
1377 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378
Allan Stephens8809b252011-10-25 10:44:35 -04001379 buf->next = queue_buf;
1380 *prev = buf;
1381 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382}
1383
Allan Stephens8809b252011-10-25 10:44:35 -04001384/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1386 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001387static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 struct sk_buff *buf)
1389{
Allan Stephensf9057302011-10-24 16:03:12 -04001390 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391
1392 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001393 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 return;
1395 }
1396
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 l_ptr->checkpoint--;
1399
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001400 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401 * Discard packet if a duplicate; otherwise add it to deferred queue
1402 * and notify peer of gap as per protocol specification
1403 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 if (less(seq_no, mod(l_ptr->next_in_no))) {
1405 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001406 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 return;
1408 }
1409
Per Liden4323add2006-01-18 00:38:21 +01001410 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1411 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412 l_ptr->deferred_inqueue_sz++;
1413 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001414 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001416 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 } else
1418 l_ptr->stats.duplicates++;
1419}
1420
1421/*
1422 * Send protocol message to the other endpoint.
1423 */
Ying Xue247f0f32014-02-18 16:06:46 +08001424void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1425 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001427 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001429 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001430 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431
Allan Stephens92d2c902011-10-25 11:20:26 -04001432 /* Discard any previous message that was deferred due to congestion */
Allan Stephens92d2c902011-10-25 11:20:26 -04001433 if (l_ptr->proto_msg_queue) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001434 kfree_skb(l_ptr->proto_msg_queue);
Allan Stephens92d2c902011-10-25 11:20:26 -04001435 l_ptr->proto_msg_queue = NULL;
1436 }
1437
Ying Xue77a7e072013-12-10 20:45:44 -08001438 /* Don't send protocol message during link changeover */
1439 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001441
1442 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001443 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001444 return;
1445
Allan Stephens92d2c902011-10-25 11:20:26 -04001446 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001448 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001449 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001450 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451
1452 if (msg_typ == STATE_MSG) {
1453 u32 next_sent = mod(l_ptr->next_out_no);
1454
Per Liden4323add2006-01-18 00:38:21 +01001455 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 return;
1457 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001458 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 msg_set_next_sent(msg, next_sent);
1460 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001461 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462 gap = mod(rec - mod(l_ptr->next_in_no));
1463 }
1464 msg_set_seq_gap(msg, gap);
1465 if (gap)
1466 l_ptr->stats.sent_nacks++;
1467 msg_set_link_tolerance(msg, tolerance);
1468 msg_set_linkprio(msg, priority);
1469 msg_set_max_pkt(msg, ack_mtu);
1470 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1471 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001472 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 u32 mtu = l_ptr->max_pkt;
1474
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001475 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 link_working_working(l_ptr) &&
1477 l_ptr->fsm_msg_cnt) {
1478 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001479 if (l_ptr->max_pkt_probes == 10) {
1480 l_ptr->max_pkt_target = (msg_size - 4);
1481 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001483 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001485 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486
1487 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001488 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489 l_ptr->stats.sent_states++;
1490 } else { /* RESET_MSG or ACTIVATE_MSG */
1491 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1492 msg_set_seq_gap(msg, 0);
1493 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001494 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 msg_set_link_tolerance(msg, l_ptr->tolerance);
1496 msg_set_linkprio(msg, l_ptr->priority);
1497 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1498 }
1499
Allan Stephens75f0aa42011-02-28 15:30:20 -05001500 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1501 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001503 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504
1505 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1506
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001507 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 if (!buf)
1509 return;
1510
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001511 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001512 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513
Ying Xue7a2f7d12014-04-21 10:55:46 +08001514 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001515 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001516 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517}
1518
1519/*
1520 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001521 * Note that network plane id propagates through the network, and may
1522 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001523 */
Ying Xue247f0f32014-02-18 16:06:46 +08001524static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525{
1526 u32 rec_gap = 0;
1527 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001528 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529 u32 msg_tol;
1530 struct tipc_msg *msg = buf_msg(buf);
1531
Ying Xue77a7e072013-12-10 20:45:44 -08001532 /* Discard protocol message during link changeover */
1533 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 goto exit;
1535
Ying Xue7a2f7d12014-04-21 10:55:46 +08001536 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001538 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001541
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001543 if (!link_working_unknown(l_ptr) &&
1544 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001545 if (less_eq(msg_session(msg), l_ptr->peer_session))
1546 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001547 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001548
1549 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1550 link_working_unknown(l_ptr))) {
1551 /*
1552 * peer has lost contact -- don't allow peer's links
1553 * to reactivate before we recognize loss & clean up
1554 */
Ying Xueca9cf062014-05-08 08:54:40 +08001555 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001556 }
1557
Allan Stephens47361c82011-10-26 10:55:16 -04001558 link_state_event(l_ptr, RESET_MSG);
1559
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560 /* fall thru' */
1561 case ACTIVATE_MSG:
1562 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1564
Allan Stephens2db99832010-12-31 18:59:33 +00001565 msg_tol = msg_link_tolerance(msg);
1566 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 link_set_supervision_props(l_ptr, msg_tol);
1568
1569 if (msg_linkprio(msg) > l_ptr->priority)
1570 l_ptr->priority = msg_linkprio(msg);
1571
1572 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001573 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 if (max_pkt_info < l_ptr->max_pkt_target)
1575 l_ptr->max_pkt_target = max_pkt_info;
1576 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1577 l_ptr->max_pkt = l_ptr->max_pkt_target;
1578 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001579 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581
Allan Stephens4d753132011-10-25 12:19:05 -04001582 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001583 if (!tipc_node_is_up(l_ptr->owner)) {
1584 l_ptr->owner->bclink.last_sent =
1585 l_ptr->owner->bclink.last_in =
1586 msg_last_bcast(msg);
1587 l_ptr->owner->bclink.oos_state = 0;
1588 }
Allan Stephens4d753132011-10-25 12:19:05 -04001589
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 l_ptr->peer_session = msg_session(msg);
1591 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001592
1593 if (msg_type(msg) == ACTIVATE_MSG)
1594 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 break;
1596 case STATE_MSG:
1597
Allan Stephens2db99832010-12-31 18:59:33 +00001598 msg_tol = msg_link_tolerance(msg);
1599 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001601
1602 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001604 pr_warn("%s<%s>, priority change %u->%u\n",
1605 link_rst_msg, l_ptr->name, l_ptr->priority,
1606 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001608 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 break;
1610 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001611
1612 /* Record reception; force mismatch at next timeout: */
1613 l_ptr->checkpoint--;
1614
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1616 l_ptr->stats.recv_states++;
1617 if (link_reset_unknown(l_ptr))
1618 break;
1619
1620 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001621 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 mod(l_ptr->next_in_no));
1623 }
1624
1625 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001626 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001627 l_ptr->max_pkt = max_pkt_ack;
1628 l_ptr->max_pkt_probes = 0;
1629 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630
1631 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001632 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001634 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001635 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001636 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637
1638 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001639 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001640 tipc_bclink_update_link_state(l_ptr->owner,
1641 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642
1643 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001644 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1645 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 }
1647 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001649 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1650 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 }
1652 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 }
1654exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001655 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656}
1657
1658
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001659/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1660 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001662static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1663 struct tipc_msg *tunnel_hdr,
1664 struct tipc_msg *msg,
1665 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001667 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 struct sk_buff *buf;
1669 u32 length = msg_size(msg);
1670
1671 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001672 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001673 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001675 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001677 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001678 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001679 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001681 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001682 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1683 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001684 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685}
1686
1687
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001688/* tipc_link_failover_send_queue(): A link has gone down, but a second
1689 * link is still active. We can do failover. Tunnel the failing link's
1690 * whole send queue via the remaining link. This way, we don't lose
1691 * any packets, and sequence order is preserved for subsequent traffic
1692 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001694void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695{
1696 u32 msgcount = l_ptr->out_queue_size;
1697 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001698 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001700 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701
1702 if (!tunnel)
1703 return;
1704
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001705 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001706 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1708 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001709
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 if (!l_ptr->first_out) {
1711 struct sk_buff *buf;
1712
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001713 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001715 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001717 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001719 pr_warn("%sunable to send changeover msg\n",
1720 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 }
1722 return;
1723 }
Allan Stephensf1310722006-06-25 23:51:37 -07001724
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001725 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001726 l_ptr->owner->active_links[1]);
1727
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 while (crs) {
1729 struct tipc_msg *msg = buf_msg(crs);
1730
1731 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001733 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734
Florian Westphald788d802007-08-02 19:28:06 -07001735 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001737 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001738 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1739 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 pos += align(msg_size(m));
1741 m = (struct tipc_msg *)pos;
1742 }
1743 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001744 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1745 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 }
1747 crs = crs->next;
1748 }
1749}
1750
Ying Xue247f0f32014-02-18 16:06:46 +08001751/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001752 * duplicate of the first link's send queue via the new link. This way, we
1753 * are guaranteed that currently queued packets from a socket are delivered
1754 * before future traffic from the same socket, even if this is using the
1755 * new link. The last arriving copy of each duplicate packet is dropped at
1756 * the receiving end by the regular protocol check, so packet cardinality
1757 * and sequence order is preserved per sender/receiver socket pair.
1758 * Owner node is locked.
1759 */
Ying Xue247f0f32014-02-18 16:06:46 +08001760void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001761 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762{
1763 struct sk_buff *iter;
1764 struct tipc_msg tunnel_hdr;
1765
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001766 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001767 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1769 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1770 iter = l_ptr->first_out;
1771 while (iter) {
1772 struct sk_buff *outbuf;
1773 struct tipc_msg *msg = buf_msg(iter);
1774 u32 length = msg_size(msg);
1775
1776 if (msg_user(msg) == MSG_BUNDLER)
1777 msg_set_type(msg, CLOSED_MSG);
1778 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001779 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001781 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001783 pr_warn("%sunable to send duplicate msg\n",
1784 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001785 return;
1786 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001787 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1788 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1789 length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001790 __tipc_link_xmit(tunnel, outbuf);
Per Liden4323add2006-01-18 00:38:21 +01001791 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792 return;
1793 iter = iter->next;
1794 }
1795}
1796
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797/**
1798 * buf_extract - extracts embedded TIPC message from another message
1799 * @skb: encapsulating message buffer
1800 * @from_pos: offset to extract from
1801 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001802 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803 * encapsulating message itself is left unchanged.
1804 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1806{
1807 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1808 u32 size = msg_size(msg);
1809 struct sk_buff *eb;
1810
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001811 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001813 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 return eb;
1815}
1816
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001817
1818
1819/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1820 * Owner node is locked.
1821 */
1822static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1823 struct sk_buff *t_buf)
1824{
1825 struct sk_buff *buf;
1826
1827 if (!tipc_link_is_up(l_ptr))
1828 return;
1829
1830 buf = buf_extract(t_buf, INT_H_SIZE);
1831 if (buf == NULL) {
1832 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1833 return;
1834 }
1835
1836 /* Add buffer to deferred queue, if applicable: */
1837 link_handle_out_of_seq_msg(l_ptr, buf);
1838}
1839
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001840/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1841 * Owner node is locked.
1842 */
1843static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1844 struct sk_buff *t_buf)
1845{
1846 struct tipc_msg *t_msg = buf_msg(t_buf);
1847 struct sk_buff *buf = NULL;
1848 struct tipc_msg *msg;
1849
1850 if (tipc_link_is_up(l_ptr))
1851 tipc_link_reset(l_ptr);
1852
1853 /* First failover packet? */
1854 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1855 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1856
1857 /* Should there be an inner packet? */
1858 if (l_ptr->exp_msg_count) {
1859 l_ptr->exp_msg_count--;
1860 buf = buf_extract(t_buf, INT_H_SIZE);
1861 if (buf == NULL) {
1862 pr_warn("%sno inner failover pkt\n", link_co_err);
1863 goto exit;
1864 }
1865 msg = buf_msg(buf);
1866
1867 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1868 kfree_skb(buf);
1869 buf = NULL;
1870 goto exit;
1871 }
1872 if (msg_user(msg) == MSG_FRAGMENTER) {
1873 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001874 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001875 }
1876 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001877exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001878 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1879 tipc_node_detach_link(l_ptr->owner, l_ptr);
1880 kfree(l_ptr);
1881 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001882 return buf;
1883}
1884
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001885/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001886 * via other link as result of a failover (ORIGINAL_MSG) or
1887 * a new active link (DUPLICATE_MSG). Failover packets are
1888 * returned to the active link for delivery upwards.
1889 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001891static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001892 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001894 struct sk_buff *t_buf = *buf;
1895 struct tipc_link *l_ptr;
1896 struct tipc_msg *t_msg = buf_msg(t_buf);
1897 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001899 *buf = NULL;
1900
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001901 if (bearer_id >= MAX_BEARERS)
1902 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001903
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001904 l_ptr = n_ptr->links[bearer_id];
1905 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001908 if (msg_type(t_msg) == DUPLICATE_MSG)
1909 tipc_link_dup_rcv(l_ptr, t_buf);
1910 else if (msg_type(t_msg) == ORIGINAL_MSG)
1911 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001912 else
1913 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001915 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001916 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917}
1918
1919/*
1920 * Bundler functionality:
1921 */
Ying Xue247f0f32014-02-18 16:06:46 +08001922void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923{
1924 u32 msgcount = msg_msgcnt(buf_msg(buf));
1925 u32 pos = INT_H_SIZE;
1926 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001927 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 while (msgcount--) {
1930 obuf = buf_extract(buf, pos);
1931 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001932 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001933 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001934 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001935 omsg = buf_msg(obuf);
1936 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001937 if (msg_isdata(omsg)) {
1938 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1939 tipc_sk_mcast_rcv(obuf);
1940 else
1941 tipc_sk_rcv(obuf);
1942 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001943 tipc_sk_rcv(obuf);
1944 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1945 tipc_named_rcv(obuf);
1946 } else {
1947 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1948 kfree_skb(obuf);
1949 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001950 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001951 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952}
1953
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001954static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001956 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1957 return;
1958
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959 l_ptr->tolerance = tolerance;
1960 l_ptr->continuity_interval =
1961 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1962 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1963}
1964
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001965void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966{
1967 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001968 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1969 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1970 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1971 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001973 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1974 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1975 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1976 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1979 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1980 /* FRAGMENT and LAST_FRAGMENT packets */
1981 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1982}
1983
Jon Paul Maloye099e862014-02-13 17:29:18 -05001984/* tipc_link_find_owner - locate owner node of link by link's name
1985 * @name: pointer to link name string
1986 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001987 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001988 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001990static struct tipc_node *tipc_link_find_owner(const char *link_name,
1991 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001993 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001994 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001995 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001996 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997
Jon Paul Maloye099e862014-02-13 17:29:18 -05001998 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001999 rcu_read_lock();
2000 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002001 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02002002 for (i = 0; i < MAX_BEARERS; i++) {
2003 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05002004 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
2005 *bearer_id = i;
2006 found_node = n_ptr;
2007 break;
2008 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02002009 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002010 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002011 if (found_node)
2012 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02002013 }
Ying Xue6c7a7622014-03-27 12:54:37 +08002014 rcu_read_unlock();
2015
Jon Paul Maloye099e862014-02-13 17:29:18 -05002016 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002017}
2018
Allan Stephens5c216e12011-10-18 11:34:29 -04002019/**
2020 * link_value_is_valid -- validate proposed link tolerance/priority/window
2021 *
Ben Hutchings2c530402012-07-10 10:55:09 +00002022 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2023 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04002024 *
2025 * Returns 1 if value is within range, 0 if not.
2026 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002027static int link_value_is_valid(u16 cmd, u32 new_value)
2028{
2029 switch (cmd) {
2030 case TIPC_CMD_SET_LINK_TOL:
2031 return (new_value >= TIPC_MIN_LINK_TOL) &&
2032 (new_value <= TIPC_MAX_LINK_TOL);
2033 case TIPC_CMD_SET_LINK_PRI:
2034 return (new_value <= TIPC_MAX_LINK_PRI);
2035 case TIPC_CMD_SET_LINK_WINDOW:
2036 return (new_value >= TIPC_MIN_LINK_WIN) &&
2037 (new_value <= TIPC_MAX_LINK_WIN);
2038 }
2039 return 0;
2040}
2041
Allan Stephens5c216e12011-10-18 11:34:29 -04002042/**
2043 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002044 * @name: ptr to link, bearer, or media name
2045 * @new_value: new value of link, bearer, or media setting
2046 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002047 *
Ying Xue7216cd92014-04-21 10:55:48 +08002048 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002049 *
2050 * Returns 0 if value updated and negative value on error.
2051 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002052static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2053{
2054 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002055 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002056 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002057 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002058 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002059 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002060
Jon Paul Maloye099e862014-02-13 17:29:18 -05002061 node = tipc_link_find_owner(name, &bearer_id);
2062 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002063 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002064 l_ptr = node->links[bearer_id];
2065
2066 if (l_ptr) {
2067 switch (cmd) {
2068 case TIPC_CMD_SET_LINK_TOL:
2069 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002070 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2071 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002072 break;
2073 case TIPC_CMD_SET_LINK_PRI:
2074 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002075 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2076 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002077 break;
2078 case TIPC_CMD_SET_LINK_WINDOW:
2079 tipc_link_set_queue_limits(l_ptr, new_value);
2080 break;
2081 default:
2082 res = -EINVAL;
2083 break;
2084 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002085 }
2086 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002087 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002088 }
2089
2090 b_ptr = tipc_bearer_find(name);
2091 if (b_ptr) {
2092 switch (cmd) {
2093 case TIPC_CMD_SET_LINK_TOL:
2094 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002095 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002096 case TIPC_CMD_SET_LINK_PRI:
2097 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002098 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002099 case TIPC_CMD_SET_LINK_WINDOW:
2100 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002101 break;
2102 default:
2103 res = -EINVAL;
2104 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002105 }
Ying Xue636c0372013-10-18 07:23:20 +02002106 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002107 }
2108
2109 m_ptr = tipc_media_find(name);
2110 if (!m_ptr)
2111 return -ENODEV;
2112 switch (cmd) {
2113 case TIPC_CMD_SET_LINK_TOL:
2114 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002115 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002116 case TIPC_CMD_SET_LINK_PRI:
2117 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002118 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002119 case TIPC_CMD_SET_LINK_WINDOW:
2120 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002121 break;
2122 default:
2123 res = -EINVAL;
2124 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002125 }
Ying Xue636c0372013-10-18 07:23:20 +02002126 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002127}
2128
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002129struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002130 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131{
2132 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002133 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002134 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135
2136 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002137 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138
2139 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2140 new_value = ntohl(args->value);
2141
Allan Stephens5c216e12011-10-18 11:34:29 -04002142 if (!link_value_is_valid(cmd, new_value))
2143 return tipc_cfg_reply_error_string(
2144 "cannot change, value invalid");
2145
Per Liden4323add2006-01-18 00:38:21 +01002146 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002148 (tipc_bclink_set_queue_limits(new_value) == 0))
2149 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002150 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002151 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 }
2153
Allan Stephens5c216e12011-10-18 11:34:29 -04002154 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002156 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157
Per Liden4323add2006-01-18 00:38:21 +01002158 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159}
2160
2161/**
2162 * link_reset_statistics - reset link statistics
2163 * @l_ptr: pointer to link
2164 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002165static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002166{
2167 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2168 l_ptr->stats.sent_info = l_ptr->next_out_no;
2169 l_ptr->stats.recv_info = l_ptr->next_in_no;
2170}
2171
Per Liden4323add2006-01-18 00:38:21 +01002172struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173{
2174 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002175 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002176 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002177 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002178
2179 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002180 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002181
2182 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002183 if (!strcmp(link_name, tipc_bclink_name)) {
2184 if (tipc_bclink_reset_stats())
2185 return tipc_cfg_reply_error_string("link not found");
2186 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002187 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002188 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002189 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002190 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002191
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002192 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002193 l_ptr = node->links[bearer_id];
2194 if (!l_ptr) {
2195 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002196 return tipc_cfg_reply_error_string("link not found");
2197 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002198 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002199 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002200 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002201}
2202
2203/**
2204 * percent - convert count to a percentage of total (rounding up or down)
2205 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206static u32 percent(u32 count, u32 total)
2207{
2208 return (count * 100 + (total / 2)) / total;
2209}
2210
2211/**
Per Liden4323add2006-01-18 00:38:21 +01002212 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002213 * @name: link name
2214 * @buf: print buffer area
2215 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002216 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217 * Returns length of print buffer data string (or 0 if error)
2218 */
Per Liden4323add2006-01-18 00:38:21 +01002219static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002220{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002221 struct tipc_link *l;
2222 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002223 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002224 char *status;
2225 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002226 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002227 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002228
Per Liden4323add2006-01-18 00:38:21 +01002229 if (!strcmp(name, tipc_bclink_name))
2230 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231
Jon Paul Maloye099e862014-02-13 17:29:18 -05002232 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002233 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002234 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002235
Per Liden4323add2006-01-18 00:38:21 +01002236 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002237
2238 l = node->links[bearer_id];
2239 if (!l) {
2240 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002241 return 0;
2242 }
2243
Erik Hugnedc1aed32012-06-29 00:50:23 -04002244 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245
Erik Hugnedc1aed32012-06-29 00:50:23 -04002246 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002247 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002248 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002249 status = "STANDBY";
2250 else
2251 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002252
2253 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2254 " %s MTU:%u Priority:%u Tolerance:%u ms"
2255 " Window:%u packets\n",
2256 l->name, status, l->max_pkt, l->priority,
2257 l->tolerance, l->queue_limit[0]);
2258
2259 ret += tipc_snprintf(buf + ret, buf_size - ret,
2260 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2261 l->next_in_no - s->recv_info, s->recv_fragments,
2262 s->recv_fragmented, s->recv_bundles,
2263 s->recv_bundled);
2264
2265 ret += tipc_snprintf(buf + ret, buf_size - ret,
2266 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2267 l->next_out_no - s->sent_info, s->sent_fragments,
2268 s->sent_fragmented, s->sent_bundles,
2269 s->sent_bundled);
2270
2271 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002272 if (!profile_total)
2273 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002274
2275 ret += tipc_snprintf(buf + ret, buf_size - ret,
2276 " TX profile sample:%u packets average:%u octets\n"
2277 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2278 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2279 s->msg_length_counts,
2280 s->msg_lengths_total / profile_total,
2281 percent(s->msg_length_profile[0], profile_total),
2282 percent(s->msg_length_profile[1], profile_total),
2283 percent(s->msg_length_profile[2], profile_total),
2284 percent(s->msg_length_profile[3], profile_total),
2285 percent(s->msg_length_profile[4], profile_total),
2286 percent(s->msg_length_profile[5], profile_total),
2287 percent(s->msg_length_profile[6], profile_total));
2288
2289 ret += tipc_snprintf(buf + ret, buf_size - ret,
2290 " RX states:%u probes:%u naks:%u defs:%u"
2291 " dups:%u\n", s->recv_states, s->recv_probes,
2292 s->recv_nacks, s->deferred_recv, s->duplicates);
2293
2294 ret += tipc_snprintf(buf + ret, buf_size - ret,
2295 " TX states:%u probes:%u naks:%u acks:%u"
2296 " dups:%u\n", s->sent_states, s->sent_probes,
2297 s->sent_nacks, s->sent_acks, s->retransmitted);
2298
2299 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002300 " Congestion link:%u Send queue"
2301 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002302 s->max_queue_sz, s->queue_sz_counts ?
2303 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002304
Per Liden4323add2006-01-18 00:38:21 +01002305 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002306 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307}
2308
Per Liden4323add2006-01-18 00:38:21 +01002309struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002310{
2311 struct sk_buff *buf;
2312 struct tlv_desc *rep_tlv;
2313 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002314 int pb_len;
2315 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002316
2317 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002318 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002319
Erik Hugnedc1aed32012-06-29 00:50:23 -04002320 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002321 if (!buf)
2322 return NULL;
2323
2324 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002325 pb = TLV_DATA(rep_tlv);
2326 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002327 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002328 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002329 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002330 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002331 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002332 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002333 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002334 skb_put(buf, TLV_SPACE(str_len));
2335 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2336
2337 return buf;
2338}
2339
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340/**
Per Liden4323add2006-01-18 00:38:21 +01002341 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002342 * @dest: network address of destination node
2343 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002344 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002345 * If no active link can be found, uses default maximum packet size.
2346 */
Per Liden4323add2006-01-18 00:38:21 +01002347u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002348{
David S. Miller6c000552008-09-02 23:38:32 -07002349 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002350 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002351 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002352
Per Lidenb97bf3f2006-01-02 19:04:38 +01002353 if (dest == tipc_own_addr)
2354 return MAX_MSG_SIZE;
2355
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002356 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002357 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002358 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359 l_ptr = n_ptr->active_links[selector & 1];
2360 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002361 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002362 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002364 return res;
2365}
2366
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002367static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002369 struct tipc_bearer *b_ptr;
2370
2371 rcu_read_lock();
2372 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2373 if (b_ptr)
2374 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2375 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002376
Per Lidenb97bf3f2006-01-02 19:04:38 +01002377 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002378 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002379 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002380 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002381 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002382 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002383 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002384 pr_cont(":WW\n");
2385 else
2386 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002388
2389/* Parse and validate nested (link) properties valid for media, bearer and link
2390 */
2391int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2392{
2393 int err;
2394
2395 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2396 tipc_nl_prop_policy);
2397 if (err)
2398 return err;
2399
2400 if (props[TIPC_NLA_PROP_PRIO]) {
2401 u32 prio;
2402
2403 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2404 if (prio > TIPC_MAX_LINK_PRI)
2405 return -EINVAL;
2406 }
2407
2408 if (props[TIPC_NLA_PROP_TOL]) {
2409 u32 tol;
2410
2411 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2412 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2413 return -EINVAL;
2414 }
2415
2416 if (props[TIPC_NLA_PROP_WIN]) {
2417 u32 win;
2418
2419 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2420 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2421 return -EINVAL;
2422 }
2423
2424 return 0;
2425}