blob: ddee498e74bc9229447e5f54e63dde1a9a6c2c5d [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"
Richard Alpe7be57fc2014-11-20 10:29:12 +010039#include "bcast.h"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040040#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "discover.h"
43#include "config.h"
Richard Alpe0655f6a2014-11-20 10:29:07 +010044#include "netlink.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010045
Ying Xue796c75d2013-06-17 10:54:48 -040046#include <linux/pkt_sched.h>
47
Erik Hugne2cf8aa12012-06-29 00:16:37 -040048/*
49 * Error message prefixes
50 */
51static const char *link_co_err = "Link changeover error, ";
52static const char *link_rst_msg = "Resetting link ";
53static const char *link_unk_evt = "Unknown link event ";
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Richard Alpe7be57fc2014-11-20 10:29:12 +010055static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
56 [TIPC_NLA_LINK_UNSPEC] = { .type = NLA_UNSPEC },
57 [TIPC_NLA_LINK_NAME] = {
58 .type = NLA_STRING,
59 .len = TIPC_MAX_LINK_NAME
60 },
61 [TIPC_NLA_LINK_MTU] = { .type = NLA_U32 },
62 [TIPC_NLA_LINK_BROADCAST] = { .type = NLA_FLAG },
63 [TIPC_NLA_LINK_UP] = { .type = NLA_FLAG },
64 [TIPC_NLA_LINK_ACTIVE] = { .type = NLA_FLAG },
65 [TIPC_NLA_LINK_PROP] = { .type = NLA_NESTED },
66 [TIPC_NLA_LINK_STATS] = { .type = NLA_NESTED },
67 [TIPC_NLA_LINK_RX] = { .type = NLA_U32 },
68 [TIPC_NLA_LINK_TX] = { .type = NLA_U32 }
69};
70
Richard Alpe0655f6a2014-11-20 10:29:07 +010071/* Properties valid for media, bearar and link */
72static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
73 [TIPC_NLA_PROP_UNSPEC] = { .type = NLA_UNSPEC },
74 [TIPC_NLA_PROP_PRIO] = { .type = NLA_U32 },
75 [TIPC_NLA_PROP_TOL] = { .type = NLA_U32 },
76 [TIPC_NLA_PROP_WIN] = { .type = NLA_U32 }
77};
78
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090079/*
Allan Stephensa686e682008-06-04 17:29:39 -070080 * Out-of-range value for link session numbers
81 */
Allan Stephensa686e682008-06-04 17:29:39 -070082#define INVALID_SESSION 0x10000
83
84/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090085 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010086 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010087#define STARTING_EVT 856384768 /* link processing trigger */
88#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
89#define TIMEOUT_EVT 560817u /* link timer expired */
90
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090091/*
92 * The following two 'message types' is really just implementation
93 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 * They must not be considered part of the protocol
95 */
96#define OPEN_MSG 0
97#define CLOSED_MSG 1
98
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090099/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100 * State value stored in 'exp_msg_count'
101 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102#define START_CHANGEOVER 100000u
103
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500104static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 struct sk_buff *buf);
Ying Xue247f0f32014-02-18 16:06:46 +0800106static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf);
Jon Paul Maloy3bb53382014-02-13 17:29:12 -0500107static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500108 struct sk_buff **buf);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500109static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500110static void link_state_event(struct tipc_link *l_ptr, u32 event);
111static void link_reset_statistics(struct tipc_link *l_ptr);
112static void link_print(struct tipc_link *l_ptr, const char *str);
Ying Xue247f0f32014-02-18 16:06:46 +0800113static void tipc_link_sync_xmit(struct tipc_link *l);
114static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
Erik Hugne7ae934b2014-07-01 10:22:40 +0200115static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf);
116static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000117
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800119 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800121static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122{
123 return (i + 3) & ~3u;
124}
125
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500126static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
Ying Xue7a2f7d12014-04-21 10:55:46 +0800128 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900130
Ying Xue7a2f7d12014-04-21 10:55:46 +0800131 rcu_read_lock();
132 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
133 if (!b_ptr) {
134 rcu_read_unlock();
135 return;
136 }
137 max_pkt = (b_ptr->mtu & ~3);
138 rcu_read_unlock();
139
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 if (max_pkt > MAX_MSG_SIZE)
141 max_pkt = MAX_MSG_SIZE;
142
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900143 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
145 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900146 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 l_ptr->max_pkt = MAX_PKT_DEFAULT;
148
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150}
151
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800153 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500155int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156{
157 if (!l_ptr)
158 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000159 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160}
161
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500162int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000164 return (l_ptr->owner->active_links[0] == l_ptr) ||
165 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166}
167
168/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 * link_timeout - handle expiration of link timer
170 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500172static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173{
Per Liden4323add2006-01-18 00:38:21 +0100174 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175
176 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
178 l_ptr->stats.queue_sz_counts++;
179
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 if (l_ptr->first_out) {
181 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
182 u32 length = msg_size(msg);
183
Joe Perchesf64f9e72009-11-29 16:55:45 -0800184 if ((msg_user(msg) == MSG_FRAGMENTER) &&
185 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 length = msg_size(msg_get_wrapped(msg));
187 }
188 if (length) {
189 l_ptr->stats.msg_lengths_total += length;
190 l_ptr->stats.msg_length_counts++;
191 if (length <= 64)
192 l_ptr->stats.msg_length_profile[0]++;
193 else if (length <= 256)
194 l_ptr->stats.msg_length_profile[1]++;
195 else if (length <= 1024)
196 l_ptr->stats.msg_length_profile[2]++;
197 else if (length <= 4096)
198 l_ptr->stats.msg_length_profile[3]++;
199 else if (length <= 16384)
200 l_ptr->stats.msg_length_profile[4]++;
201 else if (length <= 32768)
202 l_ptr->stats.msg_length_profile[5]++;
203 else
204 l_ptr->stats.msg_length_profile[6]++;
205 }
206 }
207
208 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209
210 link_state_event(l_ptr, TIMEOUT_EVT);
211
212 if (l_ptr->next_out)
Ying Xue47b4c9a82014-11-26 11:41:48 +0800213 tipc_link_push_packets(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214
Per Liden4323add2006-01-18 00:38:21 +0100215 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216}
217
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500218static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219{
220 k_start_timer(&l_ptr->timer, time);
221}
222
223/**
Per Liden4323add2006-01-18 00:38:21 +0100224 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500225 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900228 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 * Returns pointer to link.
230 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500231struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500232 struct tipc_bearer *b_ptr,
233 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500235 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 struct tipc_msg *msg;
237 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500238 char addr_string[16];
239 u32 peer = n_ptr->addr;
240
Holger Brunck0372bf52014-11-14 18:33:19 +0100241 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500242 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100243 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
244 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500245 return NULL;
246 }
247
248 if (n_ptr->links[b_ptr->identity]) {
249 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400250 pr_err("Attempt to establish second link on <%s> to %s\n",
251 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500252 return NULL;
253 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700255 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400257 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 return NULL;
259 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260
261 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500262 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400263 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900265 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 if_name,
267 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400268 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500270 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400272 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800273 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400274 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 l_ptr->state = RESET_UNKNOWN;
276
277 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
278 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000279 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700281 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 msg_set_bearer_id(msg, b_ptr->identity);
283 strcpy((char *)msg_data(msg), if_name);
284
285 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400286 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287
Ying Xue7a2f7d12014-04-21 10:55:46 +0800288 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 link_init_max_pkt(l_ptr);
290
291 l_ptr->next_out_no = 1;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400292 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293
294 link_reset_statistics(l_ptr);
295
Allan Stephens37b9c082011-02-28 11:32:27 -0500296 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500298 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
299 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500300
301 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 return l_ptr;
304}
305
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500306void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500307{
308 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500309 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500310
Ying Xue6c7a7622014-03-27 12:54:37 +0800311 rcu_read_lock();
312 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800313 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500314 l_ptr = n_ptr->links[bearer_id];
315 if (l_ptr) {
316 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500317 if (shutting_down || !tipc_node_is_up(n_ptr)) {
318 tipc_node_detach_link(l_ptr->owner, l_ptr);
319 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800320 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500321
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500322 /* Nobody else can access this link now: */
323 del_timer_sync(&l_ptr->timer);
324 kfree(l_ptr);
325 } else {
326 /* Detach/delete when failover is finished: */
327 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800328 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500329 del_timer_sync(&l_ptr->timer);
330 }
Ying Xuec61dd612014-02-13 17:29:09 -0500331 continue;
332 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800333 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500334 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800335 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500336}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337
338/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400339 * link_schedule_user - schedule user for wakeup after congestion
340 * @link: congested link
341 * @oport: sending port
342 * @chain_sz: size of buffer chain that was attempted sent
343 * @imp: importance of message attempted sent
344 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400346static bool link_schedule_user(struct tipc_link *link, u32 oport,
347 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400349 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400351 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
352 tipc_own_addr, oport, 0, 0);
353 if (!buf)
354 return false;
355 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
356 TIPC_SKB_CB(buf)->chain_imp = imp;
357 __skb_queue_tail(&link->waiting_sks, buf);
358 link->stats.link_congs++;
359 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360}
361
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400362/**
363 * link_prepare_wakeup - prepare users for wakeup after congestion
364 * @link: congested link
365 * Move a number of waiting users, as permitted by available space in
366 * the send queue, from link wait queue to node wait queue for wakeup
367 */
368static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400370 uint pend_qsz = link->out_queue_size;
Ying Xue58d78b32014-11-26 11:41:51 +0800371 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372
Ying Xue58d78b32014-11-26 11:41:51 +0800373 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
374 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 break;
Ying Xue58d78b32014-11-26 11:41:51 +0800376 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
377 __skb_unlink(skb, &link->waiting_sks);
378 __skb_queue_tail(&link->owner->waiting_sks, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380}
381
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900382/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 * link_release_outqueue - purge link's outbound message queue
384 * @l_ptr: pointer to link
385 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500386static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387{
Ying Xued77b3832013-12-10 20:45:38 -0800388 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 l_ptr->first_out = NULL;
390 l_ptr->out_queue_size = 0;
391}
392
393/**
Per Liden4323add2006-01-18 00:38:21 +0100394 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 * @l_ptr: pointer to link
396 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500397void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400399 kfree_skb(l_ptr->reasm_buf);
400 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401}
402
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900403/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500404 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 * @l_ptr: pointer to link
406 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500407void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408{
Ying Xued77b3832013-12-10 20:45:38 -0800409 kfree_skb_list(l_ptr->oldest_deferred_in);
410 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100411 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412}
413
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500414void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 u32 prev_state = l_ptr->state;
417 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700418 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400419 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900420
Allan Stephensa686e682008-06-04 17:29:39 -0700421 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422
Allan Stephensa686e682008-06-04 17:29:39 -0700423 /* Link is down, accept any session */
424 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900426 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900428
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430
431 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
432 return;
433
Per Liden4323add2006-01-18 00:38:21 +0100434 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800435 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000436
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500437 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 l_ptr->reset_checkpoint = checkpoint;
439 l_ptr->exp_msg_count = START_CHANGEOVER;
440 }
441
442 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 link_release_outqueue(l_ptr);
Ying Xued77b3832013-12-10 20:45:38 -0800444 kfree_skb_list(l_ptr->oldest_deferred_in);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400445 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
446 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
447 owner->action_flags |= TIPC_WAKEUP_USERS;
448 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 l_ptr->last_out = NULL;
450 l_ptr->first_out = NULL;
451 l_ptr->next_out = NULL;
452 l_ptr->unacked_window = 0;
453 l_ptr->checkpoint = 1;
454 l_ptr->next_out_no = 1;
455 l_ptr->deferred_inqueue_sz = 0;
456 l_ptr->oldest_deferred_in = NULL;
457 l_ptr->newest_deferred_in = NULL;
458 l_ptr->fsm_msg_cnt = 0;
459 l_ptr->stale_count = 0;
460 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461}
462
Ying Xuec61dd612014-02-13 17:29:09 -0500463void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500464{
465 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500466 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500467
Ying Xue6c7a7622014-03-27 12:54:37 +0800468 rcu_read_lock();
469 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800470 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500471 l_ptr = n_ptr->links[bearer_id];
472 if (l_ptr)
473 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800474 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500475 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800476 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500477}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500479static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480{
Allan Stephens5392d642006-06-25 23:52:50 -0700481 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100482 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800483 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484}
485
486/**
487 * link_state_event - link finite state machine
488 * @l_ptr: pointer to link
489 * @event: state machine event to process
490 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000491static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500493 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 u32 cont_intv = l_ptr->continuity_interval;
495
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500496 if (l_ptr->flags & LINK_STOPPED)
497 return;
498
Ying Xue135daee2014-02-13 17:29:08 -0500499 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 return; /* Not yet. */
501
Ying Xue77a7e072013-12-10 20:45:44 -0800502 /* Check whether changeover is going on */
503 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000504 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800506 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508
509 switch (l_ptr->state) {
510 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 switch (event) {
512 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 break;
515 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 if (l_ptr->next_in_no != l_ptr->checkpoint) {
517 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100518 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800519 tipc_link_proto_xmit(l_ptr, STATE_MSG,
520 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 l_ptr->fsm_msg_cnt++;
522 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800523 tipc_link_proto_xmit(l_ptr, STATE_MSG,
524 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 l_ptr->fsm_msg_cnt++;
526 }
527 link_set_timer(l_ptr, cont_intv);
528 break;
529 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 l_ptr->state = WORKING_UNKNOWN;
531 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800532 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 l_ptr->fsm_msg_cnt++;
534 link_set_timer(l_ptr, cont_intv / 4);
535 break;
536 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400537 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
538 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100539 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 l_ptr->state = RESET_RESET;
541 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800542 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
543 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 l_ptr->fsm_msg_cnt++;
545 link_set_timer(l_ptr, cont_intv);
546 break;
547 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400548 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 }
550 break;
551 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 switch (event) {
553 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 l_ptr->state = WORKING_WORKING;
556 l_ptr->fsm_msg_cnt = 0;
557 link_set_timer(l_ptr, cont_intv);
558 break;
559 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400560 pr_info("%s<%s>, requested by peer while probing\n",
561 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100562 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 l_ptr->state = RESET_RESET;
564 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800565 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
566 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 l_ptr->fsm_msg_cnt++;
568 link_set_timer(l_ptr, cont_intv);
569 break;
570 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 l_ptr->state = WORKING_WORKING;
573 l_ptr->fsm_msg_cnt = 0;
574 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100575 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800576 tipc_link_proto_xmit(l_ptr, STATE_MSG,
577 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 l_ptr->fsm_msg_cnt++;
579 }
580 link_set_timer(l_ptr, cont_intv);
581 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800582 tipc_link_proto_xmit(l_ptr, STATE_MSG,
583 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 l_ptr->fsm_msg_cnt++;
585 link_set_timer(l_ptr, cont_intv / 4);
586 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400587 pr_warn("%s<%s>, peer not responding\n",
588 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100589 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 l_ptr->state = RESET_UNKNOWN;
591 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800592 tipc_link_proto_xmit(l_ptr, RESET_MSG,
593 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 l_ptr->fsm_msg_cnt++;
595 link_set_timer(l_ptr, cont_intv);
596 }
597 break;
598 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400599 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 }
601 break;
602 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 switch (event) {
604 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 break;
606 case ACTIVATE_MSG:
607 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000608 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 l_ptr->state = WORKING_WORKING;
611 l_ptr->fsm_msg_cnt = 0;
612 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800613 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800615 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800616 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 link_set_timer(l_ptr, cont_intv);
618 break;
619 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 l_ptr->state = RESET_RESET;
621 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800622 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
623 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->fsm_msg_cnt++;
625 link_set_timer(l_ptr, cont_intv);
626 break;
627 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500628 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 /* fall through */
630 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800631 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 l_ptr->fsm_msg_cnt++;
633 link_set_timer(l_ptr, cont_intv);
634 break;
635 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400636 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 }
638 break;
639 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 switch (event) {
641 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 case ACTIVATE_MSG:
643 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000644 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 l_ptr->state = WORKING_WORKING;
647 l_ptr->fsm_msg_cnt = 0;
648 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800649 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800651 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800652 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 link_set_timer(l_ptr, cont_intv);
654 break;
655 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 break;
657 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800658 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
659 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 l_ptr->fsm_msg_cnt++;
661 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 break;
663 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400664 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 }
666 break;
667 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400668 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 }
670}
671
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500672/* tipc_link_cong: determine return value and how to treat the
673 * sent buffer during link congestion.
674 * - For plain, errorless user data messages we keep the buffer and
675 * return -ELINKONG.
676 * - For all other messages we discard the buffer and return -EHOSTUNREACH
677 * - For TIPC internal messages we also reset the link
678 */
679static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
680{
681 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500682 uint imp = tipc_msg_tot_importance(msg);
683 u32 oport = msg_tot_origport(msg);
684
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400685 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500686 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
687 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400688 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500689 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400690 if (unlikely(msg_errcode(msg)))
691 goto drop;
692 if (unlikely(msg_reroute_cnt(msg)))
693 goto drop;
694 if (TIPC_SKB_CB(buf)->wakeup_pending)
695 return -ELINKCONG;
696 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
697 return -ELINKCONG;
698drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500699 kfree_skb_list(buf);
700 return -EHOSTUNREACH;
701}
702
703/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400704 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500705 * @link: link to use
706 * @buf: chain of buffers containing message
707 * Consumes the buffer chain, except when returning -ELINKCONG
708 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
709 * user data messages) or -EHOSTUNREACH (all other messages/senders)
710 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
711 * to act on the return value, since they may need to do more send attempts.
712 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400713int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *buf)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500714{
715 struct tipc_msg *msg = buf_msg(buf);
716 uint psz = msg_size(msg);
717 uint qsz = link->out_queue_size;
718 uint sndlim = link->queue_limit[0];
719 uint imp = tipc_msg_tot_importance(msg);
720 uint mtu = link->max_pkt;
721 uint ack = mod(link->next_in_no - 1);
722 uint seqno = link->next_out_no;
723 uint bc_last_in = link->owner->bclink.last_in;
724 struct tipc_media_addr *addr = &link->media_addr;
725 struct sk_buff *next = buf->next;
726
727 /* Match queue limits against msg importance: */
728 if (unlikely(qsz >= link->queue_limit[imp]))
729 return tipc_link_cong(link, buf);
730
731 /* Has valid packet limit been used ? */
732 if (unlikely(psz > mtu)) {
733 kfree_skb_list(buf);
734 return -EMSGSIZE;
735 }
736
737 /* Prepare each packet for sending, and add to outqueue: */
738 while (buf) {
739 next = buf->next;
740 msg = buf_msg(buf);
741 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
742 msg_set_bcast_ack(msg, bc_last_in);
743
744 if (!link->first_out) {
745 link->first_out = buf;
746 } else if (qsz < sndlim) {
747 link->last_out->next = buf;
748 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
749 link->stats.sent_bundled++;
750 buf = next;
751 next = buf->next;
752 continue;
753 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
754 link->stats.sent_bundled++;
755 link->stats.sent_bundles++;
756 link->last_out->next = buf;
757 if (!link->next_out)
758 link->next_out = buf;
759 } else {
760 link->last_out->next = buf;
761 if (!link->next_out)
762 link->next_out = buf;
763 }
764
765 /* Send packet if possible: */
766 if (likely(++qsz <= sndlim)) {
767 tipc_bearer_send(link->bearer_id, buf, addr);
768 link->next_out = next;
769 link->unacked_window = 0;
770 }
771 seqno++;
772 link->last_out = buf;
773 buf = next;
774 }
775 link->next_out_no = seqno;
776 link->out_queue_size = qsz;
777 return 0;
778}
779
780/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400781 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500782 * @buf: chain of buffers containing message
783 * @dsz: amount of user data to be sent
784 * @dnode: address of destination node
785 * @selector: a number used for deterministic link selection
786 * Consumes the buffer chain, except when returning -ELINKCONG
787 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
788 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400789int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500790{
791 struct tipc_link *link = NULL;
792 struct tipc_node *node;
793 int rc = -EHOSTUNREACH;
794
795 node = tipc_node_find(dnode);
796 if (node) {
797 tipc_node_lock(node);
798 link = node->active_links[selector & 1];
799 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400800 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500801 tipc_node_unlock(node);
802 }
803
804 if (link)
805 return rc;
806
807 if (likely(in_own_node(dnode)))
808 return tipc_sk_rcv(buf);
809
810 kfree_skb_list(buf);
811 return rc;
812}
813
Jon Maloyc64f7a62012-11-16 13:51:31 +0800814/*
Ying Xue247f0f32014-02-18 16:06:46 +0800815 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800816 *
817 * Give a newly added peer node the sequence number where it should
818 * start receiving and acking broadcast packets.
819 *
820 * Called with node locked
821 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400822static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800823{
824 struct sk_buff *buf;
825 struct tipc_msg *msg;
826
827 buf = tipc_buf_acquire(INT_H_SIZE);
828 if (!buf)
829 return;
830
831 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400832 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
833 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400834 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800835}
836
837/*
Ying Xue247f0f32014-02-18 16:06:46 +0800838 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800839 * Receive the sequence number where we should start receiving and
840 * acking broadcast packets from a newly added peer node, and open
841 * up for reception of such packets.
842 *
843 * Called with node locked
844 */
Ying Xue247f0f32014-02-18 16:06:46 +0800845static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800846{
847 struct tipc_msg *msg = buf_msg(buf);
848
849 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
850 n->bclink.recv_permitted = true;
851 kfree_skb(buf);
852}
853
854/*
Ying Xue47b4c9a82014-11-26 11:41:48 +0800855 * tipc_link_push_packets - push unsent packets to bearer
856 *
857 * Push out the unsent messages of a link where congestion
858 * has abated. Node is locked.
859 *
860 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 */
Ying Xue47b4c9a82014-11-26 11:41:48 +0800862void tipc_link_push_packets(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863{
Ying Xue47b4c9a82014-11-26 11:41:48 +0800864 struct sk_buff *skb;
865 struct tipc_msg *msg;
866 u32 next, first;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867
Ying Xue47b4c9a82014-11-26 11:41:48 +0800868 while (l_ptr->next_out) {
869 skb = l_ptr->next_out;
870 msg = buf_msg(skb);
871 next = msg_seqno(msg);
872 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873
874 if (mod(next - first) < l_ptr->queue_limit[0]) {
875 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900876 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +0800877 if (msg_user(msg) == MSG_BUNDLER)
Ying Xue58311d12014-11-26 11:41:49 +0800878 TIPC_SKB_CB(skb)->bundling = false;
Ying Xue47b4c9a82014-11-26 11:41:48 +0800879 tipc_bearer_send(l_ptr->bearer_id, skb,
880 &l_ptr->media_addr);
881 l_ptr->next_out = skb->next;
882 } else {
883 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884 }
885 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886}
887
Ying Xue3f5a12b2014-05-05 08:56:17 +0800888void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700889{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700890 char addr_string[16];
891 u32 i;
892
Ying Xue3f5a12b2014-05-05 08:56:17 +0800893 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700894
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400895 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800896 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700897
898 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800899 if (node->links[i]) {
900 link_print(node->links[i], "Resetting link\n");
901 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700902 }
903 }
904
Ying Xue3f5a12b2014-05-05 08:56:17 +0800905 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700906}
907
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500908static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400909 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700910{
911 struct tipc_msg *msg = buf_msg(buf);
912
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400913 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700914
915 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700916 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000917 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700918 tipc_link_reset(l_ptr);
919
920 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700921 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700922 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700923 char addr_string[16];
924
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400925 pr_info("Msg seq number: %u, ", msg_seqno(msg));
926 pr_cont("Outstanding acks: %lu\n",
927 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700928
Allan Stephens01d83ed2011-01-18 13:53:16 -0500929 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700930 tipc_node_lock(n_ptr);
931
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000932 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400933 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800934 pr_info("Reception permitted: %d, Acked: %u\n",
935 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400936 n_ptr->bclink.acked);
937 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
938 n_ptr->bclink.last_in,
939 n_ptr->bclink.oos_state,
940 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700941
Allan Stephensd356eeb2006-06-25 23:40:01 -0700942 tipc_node_unlock(n_ptr);
943
Ying Xue3f5a12b2014-05-05 08:56:17 +0800944 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700945 l_ptr->stale_count = 0;
946 }
947}
948
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500949void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +0100950 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951{
952 struct tipc_msg *msg;
953
Allan Stephensd356eeb2006-06-25 23:40:01 -0700954 if (!buf)
955 return;
956
957 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900958
Erik Hugne512137e2013-12-06 10:08:00 -0500959 /* Detect repeated retransmit failures */
960 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
961 if (++l_ptr->stale_count > 100) {
962 link_retransmit_failure(l_ptr, buf);
963 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700964 }
965 } else {
Erik Hugne512137e2013-12-06 10:08:00 -0500966 l_ptr->last_retransmitted = msg_seqno(msg);
967 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 }
Allan Stephensd356eeb2006-06-25 23:40:01 -0700969
Neil Hormanca509102010-03-15 07:58:45 +0000970 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 msg = buf_msg(buf);
972 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900973 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800974 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800975 buf = buf->next;
976 retransmits--;
977 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979}
980
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 * link_insert_deferred_queue - insert deferred messages back into receive chain
983 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500984static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 struct sk_buff *buf)
986{
987 u32 seq_no;
988
989 if (l_ptr->oldest_deferred_in == NULL)
990 return buf;
991
Allan Stephensf9057302011-10-24 16:03:12 -0400992 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 if (seq_no == mod(l_ptr->next_in_no)) {
994 l_ptr->newest_deferred_in->next = buf;
995 buf = l_ptr->oldest_deferred_in;
996 l_ptr->oldest_deferred_in = NULL;
997 l_ptr->deferred_inqueue_sz = 0;
998 }
999 return buf;
1000}
1001
Allan Stephens85035562008-04-15 19:04:54 -07001002/**
1003 * link_recv_buf_validate - validate basic format of received message
1004 *
1005 * This routine ensures a TIPC message has an acceptable header, and at least
1006 * as much data as the header indicates it should. The routine also ensures
1007 * that the entire message header is stored in the main fragment of the message
1008 * buffer, to simplify future access to message header fields.
1009 *
1010 * Note: Having extra info present in the message header or data areas is OK.
1011 * TIPC will ignore the excess, under the assumption that it is optional info
1012 * introduced by a later release of the protocol.
1013 */
Allan Stephens85035562008-04-15 19:04:54 -07001014static int link_recv_buf_validate(struct sk_buff *buf)
1015{
1016 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001017 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001018 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1019 };
1020
1021 struct tipc_msg *msg;
1022 u32 tipc_hdr[2];
1023 u32 size;
1024 u32 hdr_size;
1025 u32 min_hdr_size;
1026
Erik Hugne64380a02014-02-11 11:38:26 +01001027 /* If this packet comes from the defer queue, the skb has already
1028 * been validated
1029 */
1030 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1031 return 1;
1032
Allan Stephens85035562008-04-15 19:04:54 -07001033 if (unlikely(buf->len < MIN_H_SIZE))
1034 return 0;
1035
1036 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1037 if (msg == NULL)
1038 return 0;
1039
1040 if (unlikely(msg_version(msg) != TIPC_VERSION))
1041 return 0;
1042
1043 size = msg_size(msg);
1044 hdr_size = msg_hdr_sz(msg);
1045 min_hdr_size = msg_isdata(msg) ?
1046 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1047
1048 if (unlikely((hdr_size < min_hdr_size) ||
1049 (size < hdr_size) ||
1050 (buf->len < size) ||
1051 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1052 return 0;
1053
1054 return pskb_may_pull(buf, hdr_size);
1055}
1056
Allan Stephensb02b69c2010-08-17 11:00:07 +00001057/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001058 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001059 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001060 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001061 *
1062 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1063 * structure (i.e. cannot be NULL), but bearer can be inactive.
1064 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001065void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001068 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001069 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 struct sk_buff *crs;
1071 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001072 struct tipc_msg *msg;
1073 u32 seq_no;
1074 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 u32 released = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001078 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001079
1080 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001081 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001082 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083
Allan Stephensfe13dda2008-04-15 19:03:23 -07001084 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001085 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001086 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001087
Allan Stephens85035562008-04-15 19:04:54 -07001088 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001089 msg = buf_msg(buf);
1090
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001092 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001093 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001094 else
Ying Xue247f0f32014-02-18 16:06:46 +08001095 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 continue;
1097 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001098
Allan Stephensed33a9c2011-04-05 15:15:04 -04001099 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001100 if (unlikely(!msg_short(msg) &&
1101 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001102 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001103
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001104 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001105 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001107 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001108 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001109
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001110 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001112 if (unlikely(!l_ptr))
1113 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001114
Allan Stephensb4b56102011-05-27 11:00:51 -04001115 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001116 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001117 msg_user(msg) == LINK_PROTOCOL &&
1118 (msg_type(msg) == RESET_MSG ||
1119 msg_type(msg) == ACTIVATE_MSG) &&
1120 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001121 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001122
Ying Xue10f465c2014-05-05 08:56:11 +08001123 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001124 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001125
Allan Stephens85035562008-04-15 19:04:54 -07001126 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001127 seq_no = msg_seqno(msg);
1128 ackd = msg_ack(msg);
1129
1130 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001131 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001132 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133
1134 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001135 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001136 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 struct sk_buff *next = crs->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001138 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 crs = next;
1140 released++;
1141 }
1142 if (released) {
1143 l_ptr->first_out = crs;
1144 l_ptr->out_queue_size -= released;
1145 }
Allan Stephens85035562008-04-15 19:04:54 -07001146
1147 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 if (unlikely(l_ptr->next_out))
Ying Xue47b4c9a82014-11-26 11:41:48 +08001149 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001150
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001151 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1152 link_prepare_wakeup(l_ptr);
1153 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1154 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001155
Jon Paul Maloya5377832014-02-13 17:29:15 -05001156 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001157 if (unlikely(!link_working_working(l_ptr))) {
1158 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001159 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001160 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001161 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 continue;
1163 }
Ying Xue3af390e2013-10-30 11:26:57 +08001164
1165 /* Traffic message. Conditionally activate link */
1166 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1167
1168 if (link_working_working(l_ptr)) {
1169 /* Re-insert buffer in front of queue */
1170 buf->next = head;
1171 head = buf;
1172 tipc_node_unlock(n_ptr);
1173 continue;
1174 }
1175 goto unlock_discard;
1176 }
1177
1178 /* Link is now in state WORKING_WORKING */
1179 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 link_handle_out_of_seq_msg(l_ptr, buf);
1181 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001182 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 continue;
1184 }
Ying Xue3af390e2013-10-30 11:26:57 +08001185 l_ptr->next_in_no++;
1186 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001188
Erik Hugne3f53bd82014-07-01 10:22:41 +02001189 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1190 l_ptr->stats.sent_acks++;
1191 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1192 }
1193
Erik Hugne7ae934b2014-07-01 10:22:40 +02001194 if (tipc_link_prepare_input(l_ptr, &buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001195 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 }
Per Liden4323add2006-01-18 00:38:21 +01001198 tipc_node_unlock(n_ptr);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001199 msg = buf_msg(buf);
1200 if (tipc_link_input(l_ptr, buf) != 0)
1201 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001202 continue;
1203unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001204 tipc_node_unlock(n_ptr);
1205discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001206 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208}
1209
Ben Hutchings2c530402012-07-10 10:55:09 +00001210/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001211 * tipc_link_prepare_input - process TIPC link messages
1212 *
1213 * returns nonzero if the message was consumed
1214 *
1215 * Node lock must be held
1216 */
1217static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1218{
1219 struct tipc_node *n;
1220 struct tipc_msg *msg;
1221 int res = -EINVAL;
1222
1223 n = l->owner;
1224 msg = buf_msg(*buf);
1225 switch (msg_user(msg)) {
1226 case CHANGEOVER_PROTOCOL:
1227 if (tipc_link_tunnel_rcv(n, buf))
1228 res = 0;
1229 break;
1230 case MSG_FRAGMENTER:
1231 l->stats.recv_fragments++;
1232 if (tipc_buf_append(&l->reasm_buf, buf)) {
1233 l->stats.recv_fragmented++;
1234 res = 0;
1235 } else if (!l->reasm_buf) {
1236 tipc_link_reset(l);
1237 }
1238 break;
1239 case MSG_BUNDLER:
1240 l->stats.recv_bundles++;
1241 l->stats.recv_bundled += msg_msgcnt(msg);
1242 res = 0;
1243 break;
1244 case NAME_DISTRIBUTOR:
1245 n->bclink.recv_permitted = true;
1246 res = 0;
1247 break;
1248 case BCAST_PROTOCOL:
1249 tipc_link_sync_rcv(n, *buf);
1250 break;
1251 default:
1252 res = 0;
1253 }
1254 return res;
1255}
1256/**
1257 * tipc_link_input - Deliver message too higher layers
1258 */
1259static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1260{
1261 struct tipc_msg *msg = buf_msg(buf);
1262 int res = 0;
1263
1264 switch (msg_user(msg)) {
1265 case TIPC_LOW_IMPORTANCE:
1266 case TIPC_MEDIUM_IMPORTANCE:
1267 case TIPC_HIGH_IMPORTANCE:
1268 case TIPC_CRITICAL_IMPORTANCE:
1269 case CONN_MANAGER:
1270 tipc_sk_rcv(buf);
1271 break;
1272 case NAME_DISTRIBUTOR:
1273 tipc_named_rcv(buf);
1274 break;
1275 case MSG_BUNDLER:
1276 tipc_link_bundle_rcv(buf);
1277 break;
1278 default:
1279 res = -EINVAL;
1280 }
1281 return res;
1282}
1283
1284/**
Allan Stephens8809b252011-10-25 10:44:35 -04001285 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1286 *
1287 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 */
Allan Stephens8809b252011-10-25 10:44:35 -04001289u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001290 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291{
Allan Stephens8809b252011-10-25 10:44:35 -04001292 struct sk_buff *queue_buf;
1293 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001294 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295
1296 buf->next = NULL;
1297
1298 /* Empty queue ? */
1299 if (*head == NULL) {
1300 *head = *tail = buf;
1301 return 1;
1302 }
1303
1304 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001305 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 (*tail)->next = buf;
1307 *tail = buf;
1308 return 1;
1309 }
1310
Allan Stephens8809b252011-10-25 10:44:35 -04001311 /* Locate insertion point in queue, then insert; discard if duplicate */
1312 prev = head;
1313 queue_buf = *head;
1314 for (;;) {
1315 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316
Allan Stephens8809b252011-10-25 10:44:35 -04001317 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001318 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001319 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 }
Allan Stephens8809b252011-10-25 10:44:35 -04001321
1322 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324
Allan Stephens8809b252011-10-25 10:44:35 -04001325 prev = &queue_buf->next;
1326 queue_buf = queue_buf->next;
1327 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328
Allan Stephens8809b252011-10-25 10:44:35 -04001329 buf->next = queue_buf;
1330 *prev = buf;
1331 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332}
1333
Allan Stephens8809b252011-10-25 10:44:35 -04001334/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1336 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001337static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 struct sk_buff *buf)
1339{
Allan Stephensf9057302011-10-24 16:03:12 -04001340 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341
1342 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001343 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 return;
1345 }
1346
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 l_ptr->checkpoint--;
1349
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001350 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 * Discard packet if a duplicate; otherwise add it to deferred queue
1352 * and notify peer of gap as per protocol specification
1353 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 if (less(seq_no, mod(l_ptr->next_in_no))) {
1355 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001356 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 return;
1358 }
1359
Per Liden4323add2006-01-18 00:38:21 +01001360 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1361 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 l_ptr->deferred_inqueue_sz++;
1363 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001364 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001366 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 } else
1368 l_ptr->stats.duplicates++;
1369}
1370
1371/*
1372 * Send protocol message to the other endpoint.
1373 */
Ying Xue247f0f32014-02-18 16:06:46 +08001374void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1375 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001377 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001379 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001380 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381
Ying Xue77a7e072013-12-10 20:45:44 -08001382 /* Don't send protocol message during link changeover */
1383 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001385
1386 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001387 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001388 return;
1389
Allan Stephens92d2c902011-10-25 11:20:26 -04001390 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001392 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001393 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001394 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395
1396 if (msg_typ == STATE_MSG) {
1397 u32 next_sent = mod(l_ptr->next_out_no);
1398
Per Liden4323add2006-01-18 00:38:21 +01001399 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 return;
1401 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001402 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 msg_set_next_sent(msg, next_sent);
1404 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001405 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 gap = mod(rec - mod(l_ptr->next_in_no));
1407 }
1408 msg_set_seq_gap(msg, gap);
1409 if (gap)
1410 l_ptr->stats.sent_nacks++;
1411 msg_set_link_tolerance(msg, tolerance);
1412 msg_set_linkprio(msg, priority);
1413 msg_set_max_pkt(msg, ack_mtu);
1414 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1415 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001416 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 u32 mtu = l_ptr->max_pkt;
1418
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001419 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 link_working_working(l_ptr) &&
1421 l_ptr->fsm_msg_cnt) {
1422 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001423 if (l_ptr->max_pkt_probes == 10) {
1424 l_ptr->max_pkt_target = (msg_size - 4);
1425 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001427 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001429 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430
1431 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001432 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 l_ptr->stats.sent_states++;
1434 } else { /* RESET_MSG or ACTIVATE_MSG */
1435 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1436 msg_set_seq_gap(msg, 0);
1437 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001438 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 msg_set_link_tolerance(msg, l_ptr->tolerance);
1440 msg_set_linkprio(msg, l_ptr->priority);
1441 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1442 }
1443
Allan Stephens75f0aa42011-02-28 15:30:20 -05001444 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1445 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001447 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448
1449 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1450
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001451 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 if (!buf)
1453 return;
1454
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001455 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001456 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457
Ying Xue7a2f7d12014-04-21 10:55:46 +08001458 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001459 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001460 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461}
1462
1463/*
1464 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001465 * Note that network plane id propagates through the network, and may
1466 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 */
Ying Xue247f0f32014-02-18 16:06:46 +08001468static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469{
1470 u32 rec_gap = 0;
1471 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001472 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 u32 msg_tol;
1474 struct tipc_msg *msg = buf_msg(buf);
1475
Ying Xue77a7e072013-12-10 20:45:44 -08001476 /* Discard protocol message during link changeover */
1477 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 goto exit;
1479
Ying Xue7a2f7d12014-04-21 10:55:46 +08001480 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001482 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001485
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001487 if (!link_working_unknown(l_ptr) &&
1488 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001489 if (less_eq(msg_session(msg), l_ptr->peer_session))
1490 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001492
1493 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1494 link_working_unknown(l_ptr))) {
1495 /*
1496 * peer has lost contact -- don't allow peer's links
1497 * to reactivate before we recognize loss & clean up
1498 */
Ying Xueca9cf062014-05-08 08:54:40 +08001499 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001500 }
1501
Allan Stephens47361c82011-10-26 10:55:16 -04001502 link_state_event(l_ptr, RESET_MSG);
1503
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 /* fall thru' */
1505 case ACTIVATE_MSG:
1506 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1508
Allan Stephens2db99832010-12-31 18:59:33 +00001509 msg_tol = msg_link_tolerance(msg);
1510 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 link_set_supervision_props(l_ptr, msg_tol);
1512
1513 if (msg_linkprio(msg) > l_ptr->priority)
1514 l_ptr->priority = msg_linkprio(msg);
1515
1516 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001517 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 if (max_pkt_info < l_ptr->max_pkt_target)
1519 l_ptr->max_pkt_target = max_pkt_info;
1520 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1521 l_ptr->max_pkt = l_ptr->max_pkt_target;
1522 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001523 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525
Allan Stephens4d753132011-10-25 12:19:05 -04001526 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001527 if (!tipc_node_is_up(l_ptr->owner)) {
1528 l_ptr->owner->bclink.last_sent =
1529 l_ptr->owner->bclink.last_in =
1530 msg_last_bcast(msg);
1531 l_ptr->owner->bclink.oos_state = 0;
1532 }
Allan Stephens4d753132011-10-25 12:19:05 -04001533
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 l_ptr->peer_session = msg_session(msg);
1535 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001536
1537 if (msg_type(msg) == ACTIVATE_MSG)
1538 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539 break;
1540 case STATE_MSG:
1541
Allan Stephens2db99832010-12-31 18:59:33 +00001542 msg_tol = msg_link_tolerance(msg);
1543 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001545
1546 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001547 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001548 pr_warn("%s<%s>, priority change %u->%u\n",
1549 link_rst_msg, l_ptr->name, l_ptr->priority,
1550 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001552 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 break;
1554 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001555
1556 /* Record reception; force mismatch at next timeout: */
1557 l_ptr->checkpoint--;
1558
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1560 l_ptr->stats.recv_states++;
1561 if (link_reset_unknown(l_ptr))
1562 break;
1563
1564 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001565 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566 mod(l_ptr->next_in_no));
1567 }
1568
1569 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001570 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001571 l_ptr->max_pkt = max_pkt_ack;
1572 l_ptr->max_pkt_probes = 0;
1573 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574
1575 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001576 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001578 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001579 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001580 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581
1582 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001583 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001584 tipc_bclink_update_link_state(l_ptr->owner,
1585 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586
1587 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001588 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1589 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 }
1591 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001593 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1594 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 }
1596 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597 }
1598exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001599 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600}
1601
1602
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001603/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1604 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001606static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1607 struct tipc_msg *tunnel_hdr,
1608 struct tipc_msg *msg,
1609 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001611 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612 struct sk_buff *buf;
1613 u32 length = msg_size(msg);
1614
1615 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001616 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001617 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001619 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001621 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001622 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001623 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001625 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001626 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1627 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001628 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629}
1630
1631
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001632/* tipc_link_failover_send_queue(): A link has gone down, but a second
1633 * link is still active. We can do failover. Tunnel the failing link's
1634 * whole send queue via the remaining link. This way, we don't lose
1635 * any packets, and sequence order is preserved for subsequent traffic
1636 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001638void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639{
1640 u32 msgcount = l_ptr->out_queue_size;
1641 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001642 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001644 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645
1646 if (!tunnel)
1647 return;
1648
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001649 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001650 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1652 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001653
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 if (!l_ptr->first_out) {
1655 struct sk_buff *buf;
1656
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001657 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001659 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001661 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001663 pr_warn("%sunable to send changeover msg\n",
1664 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 }
1666 return;
1667 }
Allan Stephensf1310722006-06-25 23:51:37 -07001668
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001669 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001670 l_ptr->owner->active_links[1]);
1671
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 while (crs) {
1673 struct tipc_msg *msg = buf_msg(crs);
1674
1675 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001677 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678
Florian Westphald788d802007-08-02 19:28:06 -07001679 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001681 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001682 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1683 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 pos += align(msg_size(m));
1685 m = (struct tipc_msg *)pos;
1686 }
1687 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001688 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1689 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 }
1691 crs = crs->next;
1692 }
1693}
1694
Ying Xue247f0f32014-02-18 16:06:46 +08001695/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001696 * duplicate of the first link's send queue via the new link. This way, we
1697 * are guaranteed that currently queued packets from a socket are delivered
1698 * before future traffic from the same socket, even if this is using the
1699 * new link. The last arriving copy of each duplicate packet is dropped at
1700 * the receiving end by the regular protocol check, so packet cardinality
1701 * and sequence order is preserved per sender/receiver socket pair.
1702 * Owner node is locked.
1703 */
Ying Xue247f0f32014-02-18 16:06:46 +08001704void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001705 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706{
1707 struct sk_buff *iter;
1708 struct tipc_msg tunnel_hdr;
1709
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001710 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001711 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1713 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1714 iter = l_ptr->first_out;
1715 while (iter) {
1716 struct sk_buff *outbuf;
1717 struct tipc_msg *msg = buf_msg(iter);
1718 u32 length = msg_size(msg);
1719
1720 if (msg_user(msg) == MSG_BUNDLER)
1721 msg_set_type(msg, CLOSED_MSG);
1722 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001723 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001725 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001727 pr_warn("%sunable to send duplicate msg\n",
1728 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 return;
1730 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001731 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1732 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1733 length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001734 __tipc_link_xmit(tunnel, outbuf);
Per Liden4323add2006-01-18 00:38:21 +01001735 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736 return;
1737 iter = iter->next;
1738 }
1739}
1740
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741/**
1742 * buf_extract - extracts embedded TIPC message from another message
1743 * @skb: encapsulating message buffer
1744 * @from_pos: offset to extract from
1745 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001746 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 * encapsulating message itself is left unchanged.
1748 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1750{
1751 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1752 u32 size = msg_size(msg);
1753 struct sk_buff *eb;
1754
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001755 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001757 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 return eb;
1759}
1760
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001761
1762
1763/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1764 * Owner node is locked.
1765 */
1766static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1767 struct sk_buff *t_buf)
1768{
1769 struct sk_buff *buf;
1770
1771 if (!tipc_link_is_up(l_ptr))
1772 return;
1773
1774 buf = buf_extract(t_buf, INT_H_SIZE);
1775 if (buf == NULL) {
1776 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1777 return;
1778 }
1779
1780 /* Add buffer to deferred queue, if applicable: */
1781 link_handle_out_of_seq_msg(l_ptr, buf);
1782}
1783
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001784/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1785 * Owner node is locked.
1786 */
1787static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1788 struct sk_buff *t_buf)
1789{
1790 struct tipc_msg *t_msg = buf_msg(t_buf);
1791 struct sk_buff *buf = NULL;
1792 struct tipc_msg *msg;
1793
1794 if (tipc_link_is_up(l_ptr))
1795 tipc_link_reset(l_ptr);
1796
1797 /* First failover packet? */
1798 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1799 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1800
1801 /* Should there be an inner packet? */
1802 if (l_ptr->exp_msg_count) {
1803 l_ptr->exp_msg_count--;
1804 buf = buf_extract(t_buf, INT_H_SIZE);
1805 if (buf == NULL) {
1806 pr_warn("%sno inner failover pkt\n", link_co_err);
1807 goto exit;
1808 }
1809 msg = buf_msg(buf);
1810
1811 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1812 kfree_skb(buf);
1813 buf = NULL;
1814 goto exit;
1815 }
1816 if (msg_user(msg) == MSG_FRAGMENTER) {
1817 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001818 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001819 }
1820 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001821exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001822 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1823 tipc_node_detach_link(l_ptr->owner, l_ptr);
1824 kfree(l_ptr);
1825 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001826 return buf;
1827}
1828
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001829/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001830 * via other link as result of a failover (ORIGINAL_MSG) or
1831 * a new active link (DUPLICATE_MSG). Failover packets are
1832 * returned to the active link for delivery upwards.
1833 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001835static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001836 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001838 struct sk_buff *t_buf = *buf;
1839 struct tipc_link *l_ptr;
1840 struct tipc_msg *t_msg = buf_msg(t_buf);
1841 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001843 *buf = NULL;
1844
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001845 if (bearer_id >= MAX_BEARERS)
1846 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001847
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001848 l_ptr = n_ptr->links[bearer_id];
1849 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001852 if (msg_type(t_msg) == DUPLICATE_MSG)
1853 tipc_link_dup_rcv(l_ptr, t_buf);
1854 else if (msg_type(t_msg) == ORIGINAL_MSG)
1855 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001856 else
1857 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001859 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001860 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861}
1862
1863/*
1864 * Bundler functionality:
1865 */
Ying Xue247f0f32014-02-18 16:06:46 +08001866void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867{
1868 u32 msgcount = msg_msgcnt(buf_msg(buf));
1869 u32 pos = INT_H_SIZE;
1870 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001871 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 while (msgcount--) {
1874 obuf = buf_extract(buf, pos);
1875 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001876 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001877 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001878 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001879 omsg = buf_msg(obuf);
1880 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001881 if (msg_isdata(omsg)) {
1882 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1883 tipc_sk_mcast_rcv(obuf);
1884 else
1885 tipc_sk_rcv(obuf);
1886 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001887 tipc_sk_rcv(obuf);
1888 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1889 tipc_named_rcv(obuf);
1890 } else {
1891 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1892 kfree_skb(obuf);
1893 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001895 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896}
1897
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001898static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001900 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1901 return;
1902
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 l_ptr->tolerance = tolerance;
1904 l_ptr->continuity_interval =
1905 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1906 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1907}
1908
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001909void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910{
1911 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001912 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1913 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1914 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1915 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001917 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1918 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1919 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1920 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1923 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1924 /* FRAGMENT and LAST_FRAGMENT packets */
1925 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1926}
1927
Jon Paul Maloye099e862014-02-13 17:29:18 -05001928/* tipc_link_find_owner - locate owner node of link by link's name
1929 * @name: pointer to link name string
1930 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001931 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001932 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001934static struct tipc_node *tipc_link_find_owner(const char *link_name,
1935 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001937 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001938 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001939 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001940 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941
Jon Paul Maloye099e862014-02-13 17:29:18 -05001942 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001943 rcu_read_lock();
1944 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001945 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001946 for (i = 0; i < MAX_BEARERS; i++) {
1947 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001948 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1949 *bearer_id = i;
1950 found_node = n_ptr;
1951 break;
1952 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001953 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001954 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001955 if (found_node)
1956 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001957 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001958 rcu_read_unlock();
1959
Jon Paul Maloye099e862014-02-13 17:29:18 -05001960 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961}
1962
Allan Stephens5c216e12011-10-18 11:34:29 -04001963/**
1964 * link_value_is_valid -- validate proposed link tolerance/priority/window
1965 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001966 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1967 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001968 *
1969 * Returns 1 if value is within range, 0 if not.
1970 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001971static int link_value_is_valid(u16 cmd, u32 new_value)
1972{
1973 switch (cmd) {
1974 case TIPC_CMD_SET_LINK_TOL:
1975 return (new_value >= TIPC_MIN_LINK_TOL) &&
1976 (new_value <= TIPC_MAX_LINK_TOL);
1977 case TIPC_CMD_SET_LINK_PRI:
1978 return (new_value <= TIPC_MAX_LINK_PRI);
1979 case TIPC_CMD_SET_LINK_WINDOW:
1980 return (new_value >= TIPC_MIN_LINK_WIN) &&
1981 (new_value <= TIPC_MAX_LINK_WIN);
1982 }
1983 return 0;
1984}
1985
Allan Stephens5c216e12011-10-18 11:34:29 -04001986/**
1987 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00001988 * @name: ptr to link, bearer, or media name
1989 * @new_value: new value of link, bearer, or media setting
1990 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04001991 *
Ying Xue7216cd92014-04-21 10:55:48 +08001992 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04001993 *
1994 * Returns 0 if value updated and negative value on error.
1995 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001996static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
1997{
1998 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001999 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002000 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002001 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002002 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002003 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002004
Jon Paul Maloye099e862014-02-13 17:29:18 -05002005 node = tipc_link_find_owner(name, &bearer_id);
2006 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002007 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002008 l_ptr = node->links[bearer_id];
2009
2010 if (l_ptr) {
2011 switch (cmd) {
2012 case TIPC_CMD_SET_LINK_TOL:
2013 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002014 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2015 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002016 break;
2017 case TIPC_CMD_SET_LINK_PRI:
2018 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002019 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2020 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002021 break;
2022 case TIPC_CMD_SET_LINK_WINDOW:
2023 tipc_link_set_queue_limits(l_ptr, new_value);
2024 break;
2025 default:
2026 res = -EINVAL;
2027 break;
2028 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002029 }
2030 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002031 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002032 }
2033
2034 b_ptr = tipc_bearer_find(name);
2035 if (b_ptr) {
2036 switch (cmd) {
2037 case TIPC_CMD_SET_LINK_TOL:
2038 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002039 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002040 case TIPC_CMD_SET_LINK_PRI:
2041 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002042 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002043 case TIPC_CMD_SET_LINK_WINDOW:
2044 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002045 break;
2046 default:
2047 res = -EINVAL;
2048 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002049 }
Ying Xue636c0372013-10-18 07:23:20 +02002050 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002051 }
2052
2053 m_ptr = tipc_media_find(name);
2054 if (!m_ptr)
2055 return -ENODEV;
2056 switch (cmd) {
2057 case TIPC_CMD_SET_LINK_TOL:
2058 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002059 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002060 case TIPC_CMD_SET_LINK_PRI:
2061 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002062 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002063 case TIPC_CMD_SET_LINK_WINDOW:
2064 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002065 break;
2066 default:
2067 res = -EINVAL;
2068 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002069 }
Ying Xue636c0372013-10-18 07:23:20 +02002070 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002071}
2072
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002073struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002074 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075{
2076 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002077 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002078 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002079
2080 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002081 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002082
2083 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2084 new_value = ntohl(args->value);
2085
Allan Stephens5c216e12011-10-18 11:34:29 -04002086 if (!link_value_is_valid(cmd, new_value))
2087 return tipc_cfg_reply_error_string(
2088 "cannot change, value invalid");
2089
Per Liden4323add2006-01-18 00:38:21 +01002090 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002092 (tipc_bclink_set_queue_limits(new_value) == 0))
2093 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002094 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002095 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 }
2097
Allan Stephens5c216e12011-10-18 11:34:29 -04002098 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002100 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101
Per Liden4323add2006-01-18 00:38:21 +01002102 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103}
2104
2105/**
2106 * link_reset_statistics - reset link statistics
2107 * @l_ptr: pointer to link
2108 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002109static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002110{
2111 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2112 l_ptr->stats.sent_info = l_ptr->next_out_no;
2113 l_ptr->stats.recv_info = l_ptr->next_in_no;
2114}
2115
Per Liden4323add2006-01-18 00:38:21 +01002116struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117{
2118 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002119 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002120 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002121 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122
2123 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002124 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125
2126 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002127 if (!strcmp(link_name, tipc_bclink_name)) {
2128 if (tipc_bclink_reset_stats())
2129 return tipc_cfg_reply_error_string("link not found");
2130 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002132 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002133 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002134 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002135
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002136 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002137 l_ptr = node->links[bearer_id];
2138 if (!l_ptr) {
2139 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002140 return tipc_cfg_reply_error_string("link not found");
2141 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002143 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002144 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002145}
2146
2147/**
2148 * percent - convert count to a percentage of total (rounding up or down)
2149 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150static u32 percent(u32 count, u32 total)
2151{
2152 return (count * 100 + (total / 2)) / total;
2153}
2154
2155/**
Per Liden4323add2006-01-18 00:38:21 +01002156 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157 * @name: link name
2158 * @buf: print buffer area
2159 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002160 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161 * Returns length of print buffer data string (or 0 if error)
2162 */
Per Liden4323add2006-01-18 00:38:21 +01002163static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002165 struct tipc_link *l;
2166 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002167 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168 char *status;
2169 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002170 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002171 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002172
Per Liden4323add2006-01-18 00:38:21 +01002173 if (!strcmp(name, tipc_bclink_name))
2174 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175
Jon Paul Maloye099e862014-02-13 17:29:18 -05002176 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002177 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002178 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002179
Per Liden4323add2006-01-18 00:38:21 +01002180 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002181
2182 l = node->links[bearer_id];
2183 if (!l) {
2184 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002185 return 0;
2186 }
2187
Erik Hugnedc1aed32012-06-29 00:50:23 -04002188 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002189
Erik Hugnedc1aed32012-06-29 00:50:23 -04002190 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002191 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002192 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193 status = "STANDBY";
2194 else
2195 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002196
2197 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2198 " %s MTU:%u Priority:%u Tolerance:%u ms"
2199 " Window:%u packets\n",
2200 l->name, status, l->max_pkt, l->priority,
2201 l->tolerance, l->queue_limit[0]);
2202
2203 ret += tipc_snprintf(buf + ret, buf_size - ret,
2204 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2205 l->next_in_no - s->recv_info, s->recv_fragments,
2206 s->recv_fragmented, s->recv_bundles,
2207 s->recv_bundled);
2208
2209 ret += tipc_snprintf(buf + ret, buf_size - ret,
2210 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2211 l->next_out_no - s->sent_info, s->sent_fragments,
2212 s->sent_fragmented, s->sent_bundles,
2213 s->sent_bundled);
2214
2215 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002216 if (!profile_total)
2217 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002218
2219 ret += tipc_snprintf(buf + ret, buf_size - ret,
2220 " TX profile sample:%u packets average:%u octets\n"
2221 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2222 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2223 s->msg_length_counts,
2224 s->msg_lengths_total / profile_total,
2225 percent(s->msg_length_profile[0], profile_total),
2226 percent(s->msg_length_profile[1], profile_total),
2227 percent(s->msg_length_profile[2], profile_total),
2228 percent(s->msg_length_profile[3], profile_total),
2229 percent(s->msg_length_profile[4], profile_total),
2230 percent(s->msg_length_profile[5], profile_total),
2231 percent(s->msg_length_profile[6], profile_total));
2232
2233 ret += tipc_snprintf(buf + ret, buf_size - ret,
2234 " RX states:%u probes:%u naks:%u defs:%u"
2235 " dups:%u\n", s->recv_states, s->recv_probes,
2236 s->recv_nacks, s->deferred_recv, s->duplicates);
2237
2238 ret += tipc_snprintf(buf + ret, buf_size - ret,
2239 " TX states:%u probes:%u naks:%u acks:%u"
2240 " dups:%u\n", s->sent_states, s->sent_probes,
2241 s->sent_nacks, s->sent_acks, s->retransmitted);
2242
2243 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002244 " Congestion link:%u Send queue"
2245 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002246 s->max_queue_sz, s->queue_sz_counts ?
2247 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002248
Per Liden4323add2006-01-18 00:38:21 +01002249 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002250 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002251}
2252
Per Liden4323add2006-01-18 00:38:21 +01002253struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002254{
2255 struct sk_buff *buf;
2256 struct tlv_desc *rep_tlv;
2257 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002258 int pb_len;
2259 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002260
2261 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002262 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002263
Erik Hugnedc1aed32012-06-29 00:50:23 -04002264 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002265 if (!buf)
2266 return NULL;
2267
2268 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002269 pb = TLV_DATA(rep_tlv);
2270 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002271 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002272 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002273 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002274 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002275 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002276 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002277 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002278 skb_put(buf, TLV_SPACE(str_len));
2279 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2280
2281 return buf;
2282}
2283
Per Lidenb97bf3f2006-01-02 19:04:38 +01002284/**
Per Liden4323add2006-01-18 00:38:21 +01002285 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286 * @dest: network address of destination node
2287 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002288 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002289 * If no active link can be found, uses default maximum packet size.
2290 */
Per Liden4323add2006-01-18 00:38:21 +01002291u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002292{
David S. Miller6c000552008-09-02 23:38:32 -07002293 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002294 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002295 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002296
Per Lidenb97bf3f2006-01-02 19:04:38 +01002297 if (dest == tipc_own_addr)
2298 return MAX_MSG_SIZE;
2299
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002300 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002301 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002302 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002303 l_ptr = n_ptr->active_links[selector & 1];
2304 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002305 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002306 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002308 return res;
2309}
2310
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002311static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002312{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002313 struct tipc_bearer *b_ptr;
2314
2315 rcu_read_lock();
2316 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2317 if (b_ptr)
2318 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2319 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002320
Per Lidenb97bf3f2006-01-02 19:04:38 +01002321 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002322 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002323 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002324 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002325 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002326 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002327 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002328 pr_cont(":WW\n");
2329 else
2330 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002331}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002332
2333/* Parse and validate nested (link) properties valid for media, bearer and link
2334 */
2335int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2336{
2337 int err;
2338
2339 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2340 tipc_nl_prop_policy);
2341 if (err)
2342 return err;
2343
2344 if (props[TIPC_NLA_PROP_PRIO]) {
2345 u32 prio;
2346
2347 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2348 if (prio > TIPC_MAX_LINK_PRI)
2349 return -EINVAL;
2350 }
2351
2352 if (props[TIPC_NLA_PROP_TOL]) {
2353 u32 tol;
2354
2355 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2356 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2357 return -EINVAL;
2358 }
2359
2360 if (props[TIPC_NLA_PROP_WIN]) {
2361 u32 win;
2362
2363 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2364 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2365 return -EINVAL;
2366 }
2367
2368 return 0;
2369}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002370
Richard Alpef96ce7a2014-11-20 10:29:13 +01002371int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2372{
2373 int err;
2374 int res = 0;
2375 int bearer_id;
2376 char *name;
2377 struct tipc_link *link;
2378 struct tipc_node *node;
2379 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2380
2381 if (!info->attrs[TIPC_NLA_LINK])
2382 return -EINVAL;
2383
2384 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2385 info->attrs[TIPC_NLA_LINK],
2386 tipc_nl_link_policy);
2387 if (err)
2388 return err;
2389
2390 if (!attrs[TIPC_NLA_LINK_NAME])
2391 return -EINVAL;
2392
2393 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2394
2395 node = tipc_link_find_owner(name, &bearer_id);
2396 if (!node)
2397 return -EINVAL;
2398
2399 tipc_node_lock(node);
2400
2401 link = node->links[bearer_id];
2402 if (!link) {
2403 res = -EINVAL;
2404 goto out;
2405 }
2406
2407 if (attrs[TIPC_NLA_LINK_PROP]) {
2408 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2409
2410 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2411 props);
2412 if (err) {
2413 res = err;
2414 goto out;
2415 }
2416
2417 if (props[TIPC_NLA_PROP_TOL]) {
2418 u32 tol;
2419
2420 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2421 link_set_supervision_props(link, tol);
2422 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2423 }
2424 if (props[TIPC_NLA_PROP_PRIO]) {
2425 u32 prio;
2426
2427 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2428 link->priority = prio;
2429 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2430 }
2431 if (props[TIPC_NLA_PROP_WIN]) {
2432 u32 win;
2433
2434 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2435 tipc_link_set_queue_limits(link, win);
2436 }
2437 }
2438
2439out:
2440 tipc_node_unlock(node);
2441
2442 return res;
2443}
Richard Alped8182802014-11-24 11:10:29 +01002444
2445static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002446{
2447 int i;
2448 struct nlattr *stats;
2449
2450 struct nla_map {
2451 u32 key;
2452 u32 val;
2453 };
2454
2455 struct nla_map map[] = {
2456 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2457 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2458 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2459 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2460 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2461 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2462 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2463 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2464 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2465 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2466 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2467 s->msg_length_counts : 1},
2468 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2469 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2470 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2471 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2472 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2473 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2474 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2475 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2476 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2477 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2478 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2479 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2480 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2481 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2482 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2483 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2484 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2485 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2486 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2487 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2488 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2489 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2490 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2491 };
2492
2493 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2494 if (!stats)
2495 return -EMSGSIZE;
2496
2497 for (i = 0; i < ARRAY_SIZE(map); i++)
2498 if (nla_put_u32(skb, map[i].key, map[i].val))
2499 goto msg_full;
2500
2501 nla_nest_end(skb, stats);
2502
2503 return 0;
2504msg_full:
2505 nla_nest_cancel(skb, stats);
2506
2507 return -EMSGSIZE;
2508}
2509
2510/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002511static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002512{
2513 int err;
2514 void *hdr;
2515 struct nlattr *attrs;
2516 struct nlattr *prop;
2517
2518 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2519 NLM_F_MULTI, TIPC_NL_LINK_GET);
2520 if (!hdr)
2521 return -EMSGSIZE;
2522
2523 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2524 if (!attrs)
2525 goto msg_full;
2526
2527 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2528 goto attr_msg_full;
2529 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2530 tipc_cluster_mask(tipc_own_addr)))
2531 goto attr_msg_full;
2532 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2533 goto attr_msg_full;
2534 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2535 goto attr_msg_full;
2536 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2537 goto attr_msg_full;
2538
2539 if (tipc_link_is_up(link))
2540 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2541 goto attr_msg_full;
2542 if (tipc_link_is_active(link))
2543 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2544 goto attr_msg_full;
2545
2546 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2547 if (!prop)
2548 goto attr_msg_full;
2549 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2550 goto prop_msg_full;
2551 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2552 goto prop_msg_full;
2553 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2554 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2555 goto prop_msg_full;
2556 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2557 goto prop_msg_full;
2558 nla_nest_end(msg->skb, prop);
2559
2560 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2561 if (err)
2562 goto attr_msg_full;
2563
2564 nla_nest_end(msg->skb, attrs);
2565 genlmsg_end(msg->skb, hdr);
2566
2567 return 0;
2568
2569prop_msg_full:
2570 nla_nest_cancel(msg->skb, prop);
2571attr_msg_full:
2572 nla_nest_cancel(msg->skb, attrs);
2573msg_full:
2574 genlmsg_cancel(msg->skb, hdr);
2575
2576 return -EMSGSIZE;
2577}
2578
2579/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002580static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2581 struct tipc_node *node,
2582 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002583{
2584 u32 i;
2585 int err;
2586
2587 for (i = *prev_link; i < MAX_BEARERS; i++) {
2588 *prev_link = i;
2589
2590 if (!node->links[i])
2591 continue;
2592
2593 err = __tipc_nl_add_link(msg, node->links[i]);
2594 if (err)
2595 return err;
2596 }
2597 *prev_link = 0;
2598
2599 return 0;
2600}
2601
2602int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2603{
2604 struct tipc_node *node;
2605 struct tipc_nl_msg msg;
2606 u32 prev_node = cb->args[0];
2607 u32 prev_link = cb->args[1];
2608 int done = cb->args[2];
2609 int err;
2610
2611 if (done)
2612 return 0;
2613
2614 msg.skb = skb;
2615 msg.portid = NETLINK_CB(cb->skb).portid;
2616 msg.seq = cb->nlh->nlmsg_seq;
2617
2618 rcu_read_lock();
2619
2620 if (prev_node) {
2621 node = tipc_node_find(prev_node);
2622 if (!node) {
2623 /* We never set seq or call nl_dump_check_consistent()
2624 * this means that setting prev_seq here will cause the
2625 * consistence check to fail in the netlink callback
2626 * handler. Resulting in the last NLMSG_DONE message
2627 * having the NLM_F_DUMP_INTR flag set.
2628 */
2629 cb->prev_seq = 1;
2630 goto out;
2631 }
2632
2633 list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
2634 tipc_node_lock(node);
2635 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2636 tipc_node_unlock(node);
2637 if (err)
2638 goto out;
2639
2640 prev_node = node->addr;
2641 }
2642 } else {
2643 err = tipc_nl_add_bc_link(&msg);
2644 if (err)
2645 goto out;
2646
2647 list_for_each_entry_rcu(node, &tipc_node_list, list) {
2648 tipc_node_lock(node);
2649 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2650 tipc_node_unlock(node);
2651 if (err)
2652 goto out;
2653
2654 prev_node = node->addr;
2655 }
2656 }
2657 done = 1;
2658out:
2659 rcu_read_unlock();
2660
2661 cb->args[0] = prev_node;
2662 cb->args[1] = prev_link;
2663 cb->args[2] = done;
2664
2665 return skb->len;
2666}
2667
2668int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2669{
2670 struct sk_buff *ans_skb;
2671 struct tipc_nl_msg msg;
2672 struct tipc_link *link;
2673 struct tipc_node *node;
2674 char *name;
2675 int bearer_id;
2676 int err;
2677
2678 if (!info->attrs[TIPC_NLA_LINK_NAME])
2679 return -EINVAL;
2680
2681 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2682 node = tipc_link_find_owner(name, &bearer_id);
2683 if (!node)
2684 return -EINVAL;
2685
2686 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2687 if (!ans_skb)
2688 return -ENOMEM;
2689
2690 msg.skb = ans_skb;
2691 msg.portid = info->snd_portid;
2692 msg.seq = info->snd_seq;
2693
2694 tipc_node_lock(node);
2695 link = node->links[bearer_id];
2696 if (!link) {
2697 err = -EINVAL;
2698 goto err_out;
2699 }
2700
2701 err = __tipc_nl_add_link(&msg, link);
2702 if (err)
2703 goto err_out;
2704
2705 tipc_node_unlock(node);
2706
2707 return genlmsg_reply(ans_skb, info);
2708
2709err_out:
2710 tipc_node_unlock(node);
2711 nlmsg_free(ans_skb);
2712
2713 return err;
2714}
Richard Alpeae363422014-11-20 10:29:14 +01002715
2716int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2717{
2718 int err;
2719 char *link_name;
2720 unsigned int bearer_id;
2721 struct tipc_link *link;
2722 struct tipc_node *node;
2723 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2724
2725 if (!info->attrs[TIPC_NLA_LINK])
2726 return -EINVAL;
2727
2728 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2729 info->attrs[TIPC_NLA_LINK],
2730 tipc_nl_link_policy);
2731 if (err)
2732 return err;
2733
2734 if (!attrs[TIPC_NLA_LINK_NAME])
2735 return -EINVAL;
2736
2737 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2738
2739 if (strcmp(link_name, tipc_bclink_name) == 0) {
2740 err = tipc_bclink_reset_stats();
2741 if (err)
2742 return err;
2743 return 0;
2744 }
2745
2746 node = tipc_link_find_owner(link_name, &bearer_id);
2747 if (!node)
2748 return -EINVAL;
2749
2750 tipc_node_lock(node);
2751
2752 link = node->links[bearer_id];
2753 if (!link) {
2754 tipc_node_unlock(node);
2755 return -EINVAL;
2756 }
2757
2758 link_reset_statistics(link);
2759
2760 tipc_node_unlock(node);
2761
2762 return 0;
2763}