blob: 0e04508cdba4285be21133fac2ebb67ec86ff3c2 [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{
Ying Xue58dc55f2014-11-26 11:41:52 +0800174 struct sk_buff *skb;
175
Per Liden4323add2006-01-18 00:38:21 +0100176 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177
178 /* update counters used in statistical profiling of send traffic */
Ying Xue58dc55f2014-11-26 11:41:52 +0800179 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 l_ptr->stats.queue_sz_counts++;
181
Ying Xue58dc55f2014-11-26 11:41:52 +0800182 skb = skb_peek(&l_ptr->outqueue);
183 if (skb) {
184 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 u32 length = msg_size(msg);
186
Joe Perchesf64f9e72009-11-29 16:55:45 -0800187 if ((msg_user(msg) == MSG_FRAGMENTER) &&
188 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 length = msg_size(msg_get_wrapped(msg));
190 }
191 if (length) {
192 l_ptr->stats.msg_lengths_total += length;
193 l_ptr->stats.msg_length_counts++;
194 if (length <= 64)
195 l_ptr->stats.msg_length_profile[0]++;
196 else if (length <= 256)
197 l_ptr->stats.msg_length_profile[1]++;
198 else if (length <= 1024)
199 l_ptr->stats.msg_length_profile[2]++;
200 else if (length <= 4096)
201 l_ptr->stats.msg_length_profile[3]++;
202 else if (length <= 16384)
203 l_ptr->stats.msg_length_profile[4]++;
204 else if (length <= 32768)
205 l_ptr->stats.msg_length_profile[5]++;
206 else
207 l_ptr->stats.msg_length_profile[6]++;
208 }
209 }
210
211 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 link_state_event(l_ptr, TIMEOUT_EVT);
213
214 if (l_ptr->next_out)
Ying Xue47b4c9a82014-11-26 11:41:48 +0800215 tipc_link_push_packets(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216
Per Liden4323add2006-01-18 00:38:21 +0100217 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218}
219
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500220static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221{
222 k_start_timer(&l_ptr->timer, time);
223}
224
225/**
Per Liden4323add2006-01-18 00:38:21 +0100226 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500227 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900230 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 * Returns pointer to link.
232 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500233struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500234 struct tipc_bearer *b_ptr,
235 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500237 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 struct tipc_msg *msg;
239 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500240 char addr_string[16];
241 u32 peer = n_ptr->addr;
242
Holger Brunck0372bf52014-11-14 18:33:19 +0100243 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500244 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100245 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
246 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500247 return NULL;
248 }
249
250 if (n_ptr->links[b_ptr->identity]) {
251 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400252 pr_err("Attempt to establish second link on <%s> to %s\n",
253 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500254 return NULL;
255 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700257 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400259 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 return NULL;
261 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262
263 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500264 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400265 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900267 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 if_name,
269 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400270 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500272 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400274 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800275 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400276 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 l_ptr->state = RESET_UNKNOWN;
278
279 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
280 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000281 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700283 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 msg_set_bearer_id(msg, b_ptr->identity);
285 strcpy((char *)msg_data(msg), if_name);
286
287 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400288 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289
Ying Xue7a2f7d12014-04-21 10:55:46 +0800290 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 link_init_max_pkt(l_ptr);
292
293 l_ptr->next_out_no = 1;
Ying Xue58dc55f2014-11-26 11:41:52 +0800294 __skb_queue_head_init(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800295 __skb_queue_head_init(&l_ptr->deferred_queue);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400296 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297
298 link_reset_statistics(l_ptr);
299
Allan Stephens37b9c082011-02-28 11:32:27 -0500300 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500302 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
303 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500304
305 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 return l_ptr;
308}
309
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500310void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500311{
312 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500313 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500314
Ying Xue6c7a7622014-03-27 12:54:37 +0800315 rcu_read_lock();
316 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800317 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500318 l_ptr = n_ptr->links[bearer_id];
319 if (l_ptr) {
320 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500321 if (shutting_down || !tipc_node_is_up(n_ptr)) {
322 tipc_node_detach_link(l_ptr->owner, l_ptr);
323 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800324 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500325
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500326 /* Nobody else can access this link now: */
327 del_timer_sync(&l_ptr->timer);
328 kfree(l_ptr);
329 } else {
330 /* Detach/delete when failover is finished: */
331 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800332 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500333 del_timer_sync(&l_ptr->timer);
334 }
Ying Xuec61dd612014-02-13 17:29:09 -0500335 continue;
336 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800337 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500338 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800339 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500340}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341
342/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400343 * link_schedule_user - schedule user for wakeup after congestion
344 * @link: congested link
345 * @oport: sending port
346 * @chain_sz: size of buffer chain that was attempted sent
347 * @imp: importance of message attempted sent
348 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400350static bool link_schedule_user(struct tipc_link *link, u32 oport,
351 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400353 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400355 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
356 tipc_own_addr, oport, 0, 0);
357 if (!buf)
358 return false;
359 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
360 TIPC_SKB_CB(buf)->chain_imp = imp;
361 __skb_queue_tail(&link->waiting_sks, buf);
362 link->stats.link_congs++;
363 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364}
365
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400366/**
367 * link_prepare_wakeup - prepare users for wakeup after congestion
368 * @link: congested link
369 * Move a number of waiting users, as permitted by available space in
370 * the send queue, from link wait queue to node wait queue for wakeup
371 */
372static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373{
Ying Xue58dc55f2014-11-26 11:41:52 +0800374 uint pend_qsz = skb_queue_len(&link->outqueue);
Ying Xue58d78b32014-11-26 11:41:51 +0800375 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376
Ying Xue58d78b32014-11-26 11:41:51 +0800377 skb_queue_walk_safe(&link->waiting_sks, skb, tmp) {
378 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 break;
Ying Xue58d78b32014-11-26 11:41:51 +0800380 pend_qsz += TIPC_SKB_CB(skb)->chain_sz;
381 __skb_unlink(skb, &link->waiting_sks);
382 __skb_queue_tail(&link->owner->waiting_sks, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384}
385
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900386/**
Per Liden4323add2006-01-18 00:38:21 +0100387 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 * @l_ptr: pointer to link
389 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500390void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400392 kfree_skb(l_ptr->reasm_buf);
393 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394}
395
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900396/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500397 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 * @l_ptr: pointer to link
399 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500400void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401{
Ying Xuebc6fecd2014-11-26 11:41:53 +0800402 __skb_queue_purge(&l_ptr->deferred_queue);
Ying Xue58dc55f2014-11-26 11:41:52 +0800403 __skb_queue_purge(&l_ptr->outqueue);
Per Liden4323add2006-01-18 00:38:21 +0100404 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405}
406
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500407void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 u32 prev_state = l_ptr->state;
410 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700411 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400412 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900413
Allan Stephensa686e682008-06-04 17:29:39 -0700414 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415
Allan Stephensa686e682008-06-04 17:29:39 -0700416 /* Link is down, accept any session */
417 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900419 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423
424 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
425 return;
426
Per Liden4323add2006-01-18 00:38:21 +0100427 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800428 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000429
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500430 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 l_ptr->reset_checkpoint = checkpoint;
432 l_ptr->exp_msg_count = START_CHANGEOVER;
433 }
434
435 /* Clean up all queues: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800436 __skb_queue_purge(&l_ptr->outqueue);
Ying Xuebc6fecd2014-11-26 11:41:53 +0800437 __skb_queue_purge(&l_ptr->deferred_queue);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400438 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
439 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
440 owner->action_flags |= TIPC_WAKEUP_USERS;
441 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 l_ptr->next_out = NULL;
443 l_ptr->unacked_window = 0;
444 l_ptr->checkpoint = 1;
445 l_ptr->next_out_no = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 l_ptr->fsm_msg_cnt = 0;
447 l_ptr->stale_count = 0;
448 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449}
450
Ying Xuec61dd612014-02-13 17:29:09 -0500451void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500452{
453 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500454 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500455
Ying Xue6c7a7622014-03-27 12:54:37 +0800456 rcu_read_lock();
457 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800458 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500459 l_ptr = n_ptr->links[bearer_id];
460 if (l_ptr)
461 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800462 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500463 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800464 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500465}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500467static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468{
Allan Stephens5392d642006-06-25 23:52:50 -0700469 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100470 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800471 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472}
473
474/**
475 * link_state_event - link finite state machine
476 * @l_ptr: pointer to link
477 * @event: state machine event to process
478 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000479static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500481 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 u32 cont_intv = l_ptr->continuity_interval;
483
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500484 if (l_ptr->flags & LINK_STOPPED)
485 return;
486
Ying Xue135daee2014-02-13 17:29:08 -0500487 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 return; /* Not yet. */
489
Ying Xue77a7e072013-12-10 20:45:44 -0800490 /* Check whether changeover is going on */
491 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000492 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800494 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496
497 switch (l_ptr->state) {
498 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 switch (event) {
500 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 break;
503 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 if (l_ptr->next_in_no != l_ptr->checkpoint) {
505 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100506 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800507 tipc_link_proto_xmit(l_ptr, STATE_MSG,
508 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 l_ptr->fsm_msg_cnt++;
510 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800511 tipc_link_proto_xmit(l_ptr, STATE_MSG,
512 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 l_ptr->fsm_msg_cnt++;
514 }
515 link_set_timer(l_ptr, cont_intv);
516 break;
517 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 l_ptr->state = WORKING_UNKNOWN;
519 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800520 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 l_ptr->fsm_msg_cnt++;
522 link_set_timer(l_ptr, cont_intv / 4);
523 break;
524 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400525 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
526 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100527 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 l_ptr->state = RESET_RESET;
529 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800530 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
531 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 l_ptr->fsm_msg_cnt++;
533 link_set_timer(l_ptr, cont_intv);
534 break;
535 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400536 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 }
538 break;
539 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 switch (event) {
541 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 l_ptr->state = WORKING_WORKING;
544 l_ptr->fsm_msg_cnt = 0;
545 link_set_timer(l_ptr, cont_intv);
546 break;
547 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400548 pr_info("%s<%s>, requested by peer while probing\n",
549 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100550 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 l_ptr->state = RESET_RESET;
552 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800553 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
554 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 l_ptr->fsm_msg_cnt++;
556 link_set_timer(l_ptr, cont_intv);
557 break;
558 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 l_ptr->state = WORKING_WORKING;
561 l_ptr->fsm_msg_cnt = 0;
562 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100563 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800564 tipc_link_proto_xmit(l_ptr, STATE_MSG,
565 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 l_ptr->fsm_msg_cnt++;
567 }
568 link_set_timer(l_ptr, cont_intv);
569 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800570 tipc_link_proto_xmit(l_ptr, STATE_MSG,
571 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 l_ptr->fsm_msg_cnt++;
573 link_set_timer(l_ptr, cont_intv / 4);
574 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400575 pr_warn("%s<%s>, peer not responding\n",
576 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100577 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 l_ptr->state = RESET_UNKNOWN;
579 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800580 tipc_link_proto_xmit(l_ptr, RESET_MSG,
581 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 l_ptr->fsm_msg_cnt++;
583 link_set_timer(l_ptr, cont_intv);
584 }
585 break;
586 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400587 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 }
589 break;
590 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 switch (event) {
592 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 break;
594 case ACTIVATE_MSG:
595 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000596 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 l_ptr->state = WORKING_WORKING;
599 l_ptr->fsm_msg_cnt = 0;
600 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800601 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800603 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800604 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 link_set_timer(l_ptr, cont_intv);
606 break;
607 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 l_ptr->state = RESET_RESET;
609 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800610 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
611 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 l_ptr->fsm_msg_cnt++;
613 link_set_timer(l_ptr, cont_intv);
614 break;
615 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500616 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 /* fall through */
618 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800619 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 l_ptr->fsm_msg_cnt++;
621 link_set_timer(l_ptr, cont_intv);
622 break;
623 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400624 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 }
626 break;
627 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 switch (event) {
629 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 case ACTIVATE_MSG:
631 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000632 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 l_ptr->state = WORKING_WORKING;
635 l_ptr->fsm_msg_cnt = 0;
636 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800637 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800639 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800640 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 link_set_timer(l_ptr, cont_intv);
642 break;
643 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 break;
645 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800646 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
647 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 l_ptr->fsm_msg_cnt++;
649 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 break;
651 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400652 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 }
654 break;
655 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400656 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 }
658}
659
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500660/* tipc_link_cong: determine return value and how to treat the
661 * sent buffer during link congestion.
662 * - For plain, errorless user data messages we keep the buffer and
663 * return -ELINKONG.
664 * - For all other messages we discard the buffer and return -EHOSTUNREACH
665 * - For TIPC internal messages we also reset the link
666 */
667static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
668{
669 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500670 uint imp = tipc_msg_tot_importance(msg);
671 u32 oport = msg_tot_origport(msg);
672
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400673 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500674 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
675 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400676 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500677 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400678 if (unlikely(msg_errcode(msg)))
679 goto drop;
680 if (unlikely(msg_reroute_cnt(msg)))
681 goto drop;
682 if (TIPC_SKB_CB(buf)->wakeup_pending)
683 return -ELINKCONG;
684 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
685 return -ELINKCONG;
686drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500687 kfree_skb_list(buf);
688 return -EHOSTUNREACH;
689}
690
691/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400692 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500693 * @link: link to use
Ying Xue58dc55f2014-11-26 11:41:52 +0800694 * @skb: chain of buffers containing message
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500695 * Consumes the buffer chain, except when returning -ELINKCONG
696 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
697 * user data messages) or -EHOSTUNREACH (all other messages/senders)
698 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
699 * to act on the return value, since they may need to do more send attempts.
700 */
Ying Xue58dc55f2014-11-26 11:41:52 +0800701int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *skb)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500702{
Ying Xue58dc55f2014-11-26 11:41:52 +0800703 struct tipc_msg *msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500704 uint psz = msg_size(msg);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500705 uint sndlim = link->queue_limit[0];
706 uint imp = tipc_msg_tot_importance(msg);
707 uint mtu = link->max_pkt;
708 uint ack = mod(link->next_in_no - 1);
709 uint seqno = link->next_out_no;
710 uint bc_last_in = link->owner->bclink.last_in;
711 struct tipc_media_addr *addr = &link->media_addr;
Ying Xue58dc55f2014-11-26 11:41:52 +0800712 struct sk_buff_head *outqueue = &link->outqueue;
713 struct sk_buff *next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500714
715 /* Match queue limits against msg importance: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800716 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp]))
717 return tipc_link_cong(link, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500718
719 /* Has valid packet limit been used ? */
720 if (unlikely(psz > mtu)) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800721 kfree_skb_list(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500722 return -EMSGSIZE;
723 }
724
725 /* Prepare each packet for sending, and add to outqueue: */
Ying Xue58dc55f2014-11-26 11:41:52 +0800726 while (skb) {
727 next = skb->next;
728 msg = buf_msg(skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500729 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
730 msg_set_bcast_ack(msg, bc_last_in);
731
Ying Xue58dc55f2014-11-26 11:41:52 +0800732 if (skb_queue_len(outqueue) < sndlim) {
733 __skb_queue_tail(outqueue, skb);
734 tipc_bearer_send(link->bearer_id, skb, addr);
735 link->next_out = NULL;
736 link->unacked_window = 0;
737 } else if (tipc_msg_bundle(outqueue, skb, mtu)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500738 link->stats.sent_bundled++;
Ying Xue58dc55f2014-11-26 11:41:52 +0800739 skb = next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500740 continue;
Ying Xue58dc55f2014-11-26 11:41:52 +0800741 } else if (tipc_msg_make_bundle(outqueue, skb, mtu,
742 link->addr)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500743 link->stats.sent_bundled++;
744 link->stats.sent_bundles++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500745 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800746 link->next_out = skb_peek_tail(outqueue);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500747 } else {
Ying Xue58dc55f2014-11-26 11:41:52 +0800748 __skb_queue_tail(outqueue, skb);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500749 if (!link->next_out)
Ying Xue58dc55f2014-11-26 11:41:52 +0800750 link->next_out = skb;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500751 }
752 seqno++;
Ying Xue58dc55f2014-11-26 11:41:52 +0800753 skb = next;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500754 }
755 link->next_out_no = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500756 return 0;
757}
758
759/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400760 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500761 * @buf: chain of buffers containing message
762 * @dsz: amount of user data to be sent
763 * @dnode: address of destination node
764 * @selector: a number used for deterministic link selection
765 * Consumes the buffer chain, except when returning -ELINKCONG
766 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
767 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400768int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500769{
770 struct tipc_link *link = NULL;
771 struct tipc_node *node;
772 int rc = -EHOSTUNREACH;
773
774 node = tipc_node_find(dnode);
775 if (node) {
776 tipc_node_lock(node);
777 link = node->active_links[selector & 1];
778 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400779 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500780 tipc_node_unlock(node);
781 }
782
783 if (link)
784 return rc;
785
786 if (likely(in_own_node(dnode)))
787 return tipc_sk_rcv(buf);
788
789 kfree_skb_list(buf);
790 return rc;
791}
792
Jon Maloyc64f7a62012-11-16 13:51:31 +0800793/*
Ying Xue247f0f32014-02-18 16:06:46 +0800794 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800795 *
796 * Give a newly added peer node the sequence number where it should
797 * start receiving and acking broadcast packets.
798 *
799 * Called with node locked
800 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400801static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800802{
803 struct sk_buff *buf;
804 struct tipc_msg *msg;
805
806 buf = tipc_buf_acquire(INT_H_SIZE);
807 if (!buf)
808 return;
809
810 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400811 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
812 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400813 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800814}
815
816/*
Ying Xue247f0f32014-02-18 16:06:46 +0800817 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800818 * Receive the sequence number where we should start receiving and
819 * acking broadcast packets from a newly added peer node, and open
820 * up for reception of such packets.
821 *
822 * Called with node locked
823 */
Ying Xue247f0f32014-02-18 16:06:46 +0800824static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800825{
826 struct tipc_msg *msg = buf_msg(buf);
827
828 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
829 n->bclink.recv_permitted = true;
830 kfree_skb(buf);
831}
832
Ying Xue58dc55f2014-11-26 11:41:52 +0800833struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
834 const struct sk_buff *skb)
835{
836 if (skb_queue_is_last(list, skb))
837 return NULL;
838 return skb->next;
839}
840
Jon Maloyc64f7a62012-11-16 13:51:31 +0800841/*
Ying Xue47b4c9a82014-11-26 11:41:48 +0800842 * tipc_link_push_packets - push unsent packets to bearer
843 *
844 * Push out the unsent messages of a link where congestion
845 * has abated. Node is locked.
846 *
847 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 */
Ying Xue47b4c9a82014-11-26 11:41:48 +0800849void tipc_link_push_packets(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850{
Ying Xue58dc55f2014-11-26 11:41:52 +0800851 struct sk_buff_head *outqueue = &l_ptr->outqueue;
852 struct sk_buff *skb = l_ptr->next_out;
Ying Xue47b4c9a82014-11-26 11:41:48 +0800853 struct tipc_msg *msg;
854 u32 next, first;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855
Ying Xue58dc55f2014-11-26 11:41:52 +0800856 skb_queue_walk_from(outqueue, skb) {
Ying Xue47b4c9a82014-11-26 11:41:48 +0800857 msg = buf_msg(skb);
858 next = msg_seqno(msg);
Ying Xue58dc55f2014-11-26 11:41:52 +0800859 first = buf_seqno(skb_peek(outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860
861 if (mod(next - first) < l_ptr->queue_limit[0]) {
862 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900863 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue3c294cb2012-11-15 11:34:45 +0800864 if (msg_user(msg) == MSG_BUNDLER)
Ying Xue58311d12014-11-26 11:41:49 +0800865 TIPC_SKB_CB(skb)->bundling = false;
Ying Xue47b4c9a82014-11-26 11:41:48 +0800866 tipc_bearer_send(l_ptr->bearer_id, skb,
867 &l_ptr->media_addr);
Ying Xue58dc55f2014-11-26 11:41:52 +0800868 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
Ying Xue47b4c9a82014-11-26 11:41:48 +0800869 } else {
870 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 }
872 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873}
874
Ying Xue3f5a12b2014-05-05 08:56:17 +0800875void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700876{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700877 char addr_string[16];
878 u32 i;
879
Ying Xue3f5a12b2014-05-05 08:56:17 +0800880 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700881
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400882 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800883 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700884
885 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800886 if (node->links[i]) {
887 link_print(node->links[i], "Resetting link\n");
888 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700889 }
890 }
891
Ying Xue3f5a12b2014-05-05 08:56:17 +0800892 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700893}
894
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500895static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400896 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700897{
898 struct tipc_msg *msg = buf_msg(buf);
899
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400900 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700901
902 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700903 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000904 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700905 tipc_link_reset(l_ptr);
906
907 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700908 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700909 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700910 char addr_string[16];
911
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400912 pr_info("Msg seq number: %u, ", msg_seqno(msg));
913 pr_cont("Outstanding acks: %lu\n",
914 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700915
Allan Stephens01d83ed2011-01-18 13:53:16 -0500916 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700917 tipc_node_lock(n_ptr);
918
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000919 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400920 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800921 pr_info("Reception permitted: %d, Acked: %u\n",
922 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400923 n_ptr->bclink.acked);
924 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
925 n_ptr->bclink.last_in,
926 n_ptr->bclink.oos_state,
927 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700928
Allan Stephensd356eeb2006-06-25 23:40:01 -0700929 tipc_node_unlock(n_ptr);
930
Ying Xue3f5a12b2014-05-05 08:56:17 +0800931 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700932 l_ptr->stale_count = 0;
933 }
934}
935
Ying Xue58dc55f2014-11-26 11:41:52 +0800936void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100937 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938{
939 struct tipc_msg *msg;
940
Ying Xue58dc55f2014-11-26 11:41:52 +0800941 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700942 return;
943
Ying Xue58dc55f2014-11-26 11:41:52 +0800944 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900945
Erik Hugne512137e2013-12-06 10:08:00 -0500946 /* Detect repeated retransmit failures */
947 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
948 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +0800949 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -0500950 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700951 }
952 } else {
Erik Hugne512137e2013-12-06 10:08:00 -0500953 l_ptr->last_retransmitted = msg_seqno(msg);
954 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 }
Allan Stephensd356eeb2006-06-25 23:40:01 -0700956
Ying Xue58dc55f2014-11-26 11:41:52 +0800957 skb_queue_walk_from(&l_ptr->outqueue, skb) {
958 if (!retransmits || skb == l_ptr->next_out)
959 break;
960 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900962 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue58dc55f2014-11-26 11:41:52 +0800963 tipc_bearer_send(l_ptr->bearer_id, skb, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800964 retransmits--;
965 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967}
968
Ying Xuef03273f2014-11-26 11:41:54 +0800969static void link_retrieve_defq(struct tipc_link *link,
970 struct sk_buff_head *list)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971{
972 u32 seq_no;
973
Ying Xuef03273f2014-11-26 11:41:54 +0800974 if (skb_queue_empty(&link->deferred_queue))
975 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976
Ying Xuef03273f2014-11-26 11:41:54 +0800977 seq_no = buf_seqno(skb_peek(&link->deferred_queue));
978 if (seq_no == mod(link->next_in_no))
979 skb_queue_splice_tail_init(&link->deferred_queue, list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980}
981
Allan Stephens85035562008-04-15 19:04:54 -0700982/**
983 * link_recv_buf_validate - validate basic format of received message
984 *
985 * This routine ensures a TIPC message has an acceptable header, and at least
986 * as much data as the header indicates it should. The routine also ensures
987 * that the entire message header is stored in the main fragment of the message
988 * buffer, to simplify future access to message header fields.
989 *
990 * Note: Having extra info present in the message header or data areas is OK.
991 * TIPC will ignore the excess, under the assumption that it is optional info
992 * introduced by a later release of the protocol.
993 */
Allan Stephens85035562008-04-15 19:04:54 -0700994static int link_recv_buf_validate(struct sk_buff *buf)
995{
996 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -0400997 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -0700998 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
999 };
1000
1001 struct tipc_msg *msg;
1002 u32 tipc_hdr[2];
1003 u32 size;
1004 u32 hdr_size;
1005 u32 min_hdr_size;
1006
Erik Hugne64380a02014-02-11 11:38:26 +01001007 /* If this packet comes from the defer queue, the skb has already
1008 * been validated
1009 */
1010 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1011 return 1;
1012
Allan Stephens85035562008-04-15 19:04:54 -07001013 if (unlikely(buf->len < MIN_H_SIZE))
1014 return 0;
1015
1016 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1017 if (msg == NULL)
1018 return 0;
1019
1020 if (unlikely(msg_version(msg) != TIPC_VERSION))
1021 return 0;
1022
1023 size = msg_size(msg);
1024 hdr_size = msg_hdr_sz(msg);
1025 min_hdr_size = msg_isdata(msg) ?
1026 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1027
1028 if (unlikely((hdr_size < min_hdr_size) ||
1029 (size < hdr_size) ||
1030 (buf->len < size) ||
1031 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1032 return 0;
1033
1034 return pskb_may_pull(buf, hdr_size);
1035}
1036
Allan Stephensb02b69c2010-08-17 11:00:07 +00001037/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001038 * tipc_rcv - process TIPC packets/messages arriving from off-node
Ying Xuef03273f2014-11-26 11:41:54 +08001039 * @skb: TIPC packet
Ying Xue7a2f7d12014-04-21 10:55:46 +08001040 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001041 *
1042 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1043 * structure (i.e. cannot be NULL), but bearer can be inactive.
1044 */
Ying Xuef03273f2014-11-26 11:41:54 +08001045void tipc_rcv(struct sk_buff *skb, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046{
Ying Xuef03273f2014-11-26 11:41:54 +08001047 struct sk_buff_head head;
1048 struct tipc_node *n_ptr;
1049 struct tipc_link *l_ptr;
1050 struct sk_buff *skb1, *tmp;
1051 struct tipc_msg *msg;
1052 u32 seq_no;
1053 u32 ackd;
1054 u32 released;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055
Ying Xuef03273f2014-11-26 11:41:54 +08001056 __skb_queue_head_init(&head);
1057 __skb_queue_tail(&head, skb);
Allan Stephens85035562008-04-15 19:04:54 -07001058
Ying Xuef03273f2014-11-26 11:41:54 +08001059 while ((skb = __skb_dequeue(&head))) {
Allan Stephens85035562008-04-15 19:04:54 -07001060 /* Ensure message is well-formed */
Ying Xuef03273f2014-11-26 11:41:54 +08001061 if (unlikely(!link_recv_buf_validate(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001062 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063
Allan Stephensfe13dda2008-04-15 19:03:23 -07001064 /* Ensure message data is a single contiguous unit */
Ying Xuef03273f2014-11-26 11:41:54 +08001065 if (unlikely(skb_linearize(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001066 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001067
Allan Stephens85035562008-04-15 19:04:54 -07001068 /* Handle arrival of a non-unicast link message */
Ying Xuef03273f2014-11-26 11:41:54 +08001069 msg = buf_msg(skb);
Allan Stephens85035562008-04-15 19:04:54 -07001070
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001072 if (msg_user(msg) == LINK_CONFIG)
Ying Xuef03273f2014-11-26 11:41:54 +08001073 tipc_disc_rcv(skb, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001074 else
Ying Xuef03273f2014-11-26 11:41:54 +08001075 tipc_bclink_rcv(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 continue;
1077 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001078
Allan Stephensed33a9c2011-04-05 15:15:04 -04001079 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001080 if (unlikely(!msg_short(msg) &&
1081 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001082 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001083
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001084 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001085 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001087 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001088 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001089
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001090 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001092 if (unlikely(!l_ptr))
1093 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001094
Allan Stephensb4b56102011-05-27 11:00:51 -04001095 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001096 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001097 msg_user(msg) == LINK_PROTOCOL &&
1098 (msg_type(msg) == RESET_MSG ||
1099 msg_type(msg) == ACTIVATE_MSG) &&
1100 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001101 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001102
Ying Xue10f465c2014-05-05 08:56:11 +08001103 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001104 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001105
Allan Stephens85035562008-04-15 19:04:54 -07001106 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001107 seq_no = msg_seqno(msg);
1108 ackd = msg_ack(msg);
1109
1110 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001111 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001112 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113
Ying Xue58dc55f2014-11-26 11:41:52 +08001114 released = 0;
1115 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) {
1116 if (skb1 == l_ptr->next_out ||
1117 more(buf_seqno(skb1), ackd))
1118 break;
1119 __skb_unlink(skb1, &l_ptr->outqueue);
1120 kfree_skb(skb1);
1121 released = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 }
Allan Stephens85035562008-04-15 19:04:54 -07001123
1124 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125 if (unlikely(l_ptr->next_out))
Ying Xue47b4c9a82014-11-26 11:41:48 +08001126 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001127
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001128 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1129 link_prepare_wakeup(l_ptr);
1130 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1131 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001132
Jon Paul Maloya5377832014-02-13 17:29:15 -05001133 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001134 if (unlikely(!link_working_working(l_ptr))) {
1135 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xuef03273f2014-11-26 11:41:54 +08001136 tipc_link_proto_rcv(l_ptr, skb);
1137 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001138 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 continue;
1140 }
Ying Xue3af390e2013-10-30 11:26:57 +08001141
1142 /* Traffic message. Conditionally activate link */
1143 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1144
1145 if (link_working_working(l_ptr)) {
1146 /* Re-insert buffer in front of queue */
Ying Xuef03273f2014-11-26 11:41:54 +08001147 __skb_queue_head(&head, skb);
Ying Xue3af390e2013-10-30 11:26:57 +08001148 tipc_node_unlock(n_ptr);
1149 continue;
1150 }
1151 goto unlock_discard;
1152 }
1153
1154 /* Link is now in state WORKING_WORKING */
1155 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Ying Xuef03273f2014-11-26 11:41:54 +08001156 link_handle_out_of_seq_msg(l_ptr, skb);
1157 link_retrieve_defq(l_ptr, &head);
Per Liden4323add2006-01-18 00:38:21 +01001158 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 continue;
1160 }
Ying Xue3af390e2013-10-30 11:26:57 +08001161 l_ptr->next_in_no++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001162 if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue)))
Ying Xuef03273f2014-11-26 11:41:54 +08001163 link_retrieve_defq(l_ptr, &head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001164
Erik Hugne3f53bd82014-07-01 10:22:41 +02001165 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1166 l_ptr->stats.sent_acks++;
1167 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1168 }
1169
Ying Xuef03273f2014-11-26 11:41:54 +08001170 if (tipc_link_prepare_input(l_ptr, &skb)) {
Per Liden4323add2006-01-18 00:38:21 +01001171 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 }
Per Liden4323add2006-01-18 00:38:21 +01001174 tipc_node_unlock(n_ptr);
Ying Xuef03273f2014-11-26 11:41:54 +08001175
1176 if (tipc_link_input(l_ptr, skb) != 0)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001177 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001178 continue;
1179unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001180 tipc_node_unlock(n_ptr);
1181discard:
Ying Xuef03273f2014-11-26 11:41:54 +08001182 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184}
1185
Ben Hutchings2c530402012-07-10 10:55:09 +00001186/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001187 * tipc_link_prepare_input - process TIPC link messages
1188 *
1189 * returns nonzero if the message was consumed
1190 *
1191 * Node lock must be held
1192 */
1193static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1194{
1195 struct tipc_node *n;
1196 struct tipc_msg *msg;
1197 int res = -EINVAL;
1198
1199 n = l->owner;
1200 msg = buf_msg(*buf);
1201 switch (msg_user(msg)) {
1202 case CHANGEOVER_PROTOCOL:
1203 if (tipc_link_tunnel_rcv(n, buf))
1204 res = 0;
1205 break;
1206 case MSG_FRAGMENTER:
1207 l->stats.recv_fragments++;
1208 if (tipc_buf_append(&l->reasm_buf, buf)) {
1209 l->stats.recv_fragmented++;
1210 res = 0;
1211 } else if (!l->reasm_buf) {
1212 tipc_link_reset(l);
1213 }
1214 break;
1215 case MSG_BUNDLER:
1216 l->stats.recv_bundles++;
1217 l->stats.recv_bundled += msg_msgcnt(msg);
1218 res = 0;
1219 break;
1220 case NAME_DISTRIBUTOR:
1221 n->bclink.recv_permitted = true;
1222 res = 0;
1223 break;
1224 case BCAST_PROTOCOL:
1225 tipc_link_sync_rcv(n, *buf);
1226 break;
1227 default:
1228 res = 0;
1229 }
1230 return res;
1231}
1232/**
1233 * tipc_link_input - Deliver message too higher layers
1234 */
1235static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1236{
1237 struct tipc_msg *msg = buf_msg(buf);
1238 int res = 0;
1239
1240 switch (msg_user(msg)) {
1241 case TIPC_LOW_IMPORTANCE:
1242 case TIPC_MEDIUM_IMPORTANCE:
1243 case TIPC_HIGH_IMPORTANCE:
1244 case TIPC_CRITICAL_IMPORTANCE:
1245 case CONN_MANAGER:
1246 tipc_sk_rcv(buf);
1247 break;
1248 case NAME_DISTRIBUTOR:
1249 tipc_named_rcv(buf);
1250 break;
1251 case MSG_BUNDLER:
1252 tipc_link_bundle_rcv(buf);
1253 break;
1254 default:
1255 res = -EINVAL;
1256 }
1257 return res;
1258}
1259
1260/**
Allan Stephens8809b252011-10-25 10:44:35 -04001261 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1262 *
1263 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001265u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266{
Ying Xuebc6fecd2014-11-26 11:41:53 +08001267 struct sk_buff *skb1;
1268 u32 seq_no = buf_seqno(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269
1270 /* Empty queue ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001271 if (skb_queue_empty(list)) {
1272 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 return 1;
1274 }
1275
1276 /* Last ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001277 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1278 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 return 1;
1280 }
1281
Allan Stephens8809b252011-10-25 10:44:35 -04001282 /* Locate insertion point in queue, then insert; discard if duplicate */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001283 skb_queue_walk(list, skb1) {
1284 u32 curr_seqno = buf_seqno(skb1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285
Allan Stephens8809b252011-10-25 10:44:35 -04001286 if (seq_no == curr_seqno) {
Ying Xuebc6fecd2014-11-26 11:41:53 +08001287 kfree_skb(skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001288 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 }
Allan Stephens8809b252011-10-25 10:44:35 -04001290
1291 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 break;
Allan Stephens8809b252011-10-25 10:44:35 -04001293 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001294
Ying Xuebc6fecd2014-11-26 11:41:53 +08001295 __skb_queue_before(list, skb1, skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001296 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297}
1298
Allan Stephens8809b252011-10-25 10:44:35 -04001299/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1301 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001302static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 struct sk_buff *buf)
1304{
Allan Stephensf9057302011-10-24 16:03:12 -04001305 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306
1307 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001308 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 return;
1310 }
1311
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 l_ptr->checkpoint--;
1314
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001315 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 * Discard packet if a duplicate; otherwise add it to deferred queue
1317 * and notify peer of gap as per protocol specification
1318 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 if (less(seq_no, mod(l_ptr->next_in_no))) {
1320 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001321 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 return;
1323 }
1324
Ying Xuebc6fecd2014-11-26 11:41:53 +08001325 if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001327 TIPC_SKB_CB(buf)->deferred = true;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001328 if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001329 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001330 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 l_ptr->stats.duplicates++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001332 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333}
1334
1335/*
1336 * Send protocol message to the other endpoint.
1337 */
Ying Xue247f0f32014-02-18 16:06:46 +08001338void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1339 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001341 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001343 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001344 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345
Ying Xue77a7e072013-12-10 20:45:44 -08001346 /* Don't send protocol message during link changeover */
1347 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001349
1350 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001351 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001352 return;
1353
Allan Stephens92d2c902011-10-25 11:20:26 -04001354 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001356 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001357 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001358 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359
1360 if (msg_typ == STATE_MSG) {
1361 u32 next_sent = mod(l_ptr->next_out_no);
1362
Per Liden4323add2006-01-18 00:38:21 +01001363 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 return;
1365 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001366 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 msg_set_next_sent(msg, next_sent);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001368 if (!skb_queue_empty(&l_ptr->deferred_queue)) {
1369 u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 gap = mod(rec - mod(l_ptr->next_in_no));
1371 }
1372 msg_set_seq_gap(msg, gap);
1373 if (gap)
1374 l_ptr->stats.sent_nacks++;
1375 msg_set_link_tolerance(msg, tolerance);
1376 msg_set_linkprio(msg, priority);
1377 msg_set_max_pkt(msg, ack_mtu);
1378 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1379 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001380 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 u32 mtu = l_ptr->max_pkt;
1382
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001383 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 link_working_working(l_ptr) &&
1385 l_ptr->fsm_msg_cnt) {
1386 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001387 if (l_ptr->max_pkt_probes == 10) {
1388 l_ptr->max_pkt_target = (msg_size - 4);
1389 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001393 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394
1395 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 l_ptr->stats.sent_states++;
1398 } else { /* RESET_MSG or ACTIVATE_MSG */
1399 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1400 msg_set_seq_gap(msg, 0);
1401 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001402 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 msg_set_link_tolerance(msg, l_ptr->tolerance);
1404 msg_set_linkprio(msg, l_ptr->priority);
1405 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1406 }
1407
Allan Stephens75f0aa42011-02-28 15:30:20 -05001408 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1409 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001411 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412
1413 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1414
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001415 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 if (!buf)
1417 return;
1418
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001419 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001420 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421
Ying Xue7a2f7d12014-04-21 10:55:46 +08001422 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001423 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001424 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425}
1426
1427/*
1428 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001429 * Note that network plane id propagates through the network, and may
1430 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 */
Ying Xue247f0f32014-02-18 16:06:46 +08001432static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433{
1434 u32 rec_gap = 0;
1435 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001436 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 u32 msg_tol;
1438 struct tipc_msg *msg = buf_msg(buf);
1439
Ying Xue77a7e072013-12-10 20:45:44 -08001440 /* Discard protocol message during link changeover */
1441 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 goto exit;
1443
Ying Xue7a2f7d12014-04-21 10:55:46 +08001444 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001446 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001449
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001451 if (!link_working_unknown(l_ptr) &&
1452 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001453 if (less_eq(msg_session(msg), l_ptr->peer_session))
1454 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001456
1457 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1458 link_working_unknown(l_ptr))) {
1459 /*
1460 * peer has lost contact -- don't allow peer's links
1461 * to reactivate before we recognize loss & clean up
1462 */
Ying Xueca9cf062014-05-08 08:54:40 +08001463 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001464 }
1465
Allan Stephens47361c82011-10-26 10:55:16 -04001466 link_state_event(l_ptr, RESET_MSG);
1467
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 /* fall thru' */
1469 case ACTIVATE_MSG:
1470 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1472
Allan Stephens2db99832010-12-31 18:59:33 +00001473 msg_tol = msg_link_tolerance(msg);
1474 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 link_set_supervision_props(l_ptr, msg_tol);
1476
1477 if (msg_linkprio(msg) > l_ptr->priority)
1478 l_ptr->priority = msg_linkprio(msg);
1479
1480 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001481 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482 if (max_pkt_info < l_ptr->max_pkt_target)
1483 l_ptr->max_pkt_target = max_pkt_info;
1484 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1485 l_ptr->max_pkt = l_ptr->max_pkt_target;
1486 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001487 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489
Allan Stephens4d753132011-10-25 12:19:05 -04001490 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001491 if (!tipc_node_is_up(l_ptr->owner)) {
1492 l_ptr->owner->bclink.last_sent =
1493 l_ptr->owner->bclink.last_in =
1494 msg_last_bcast(msg);
1495 l_ptr->owner->bclink.oos_state = 0;
1496 }
Allan Stephens4d753132011-10-25 12:19:05 -04001497
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 l_ptr->peer_session = msg_session(msg);
1499 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001500
1501 if (msg_type(msg) == ACTIVATE_MSG)
1502 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 break;
1504 case STATE_MSG:
1505
Allan Stephens2db99832010-12-31 18:59:33 +00001506 msg_tol = msg_link_tolerance(msg);
1507 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509
1510 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001512 pr_warn("%s<%s>, priority change %u->%u\n",
1513 link_rst_msg, l_ptr->name, l_ptr->priority,
1514 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001516 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517 break;
1518 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001519
1520 /* Record reception; force mismatch at next timeout: */
1521 l_ptr->checkpoint--;
1522
Per Lidenb97bf3f2006-01-02 19:04:38 +01001523 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1524 l_ptr->stats.recv_states++;
1525 if (link_reset_unknown(l_ptr))
1526 break;
1527
1528 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001529 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 mod(l_ptr->next_in_no));
1531 }
1532
1533 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001534 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001535 l_ptr->max_pkt = max_pkt_ack;
1536 l_ptr->max_pkt_probes = 0;
1537 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538
1539 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001540 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001542 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001543 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001544 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545
1546 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001547 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001548 tipc_bclink_update_link_state(l_ptr->owner,
1549 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550
1551 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001552 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1553 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 }
1555 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 l_ptr->stats.recv_nacks++;
Ying Xue58dc55f2014-11-26 11:41:52 +08001557 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue),
Per Liden4323add2006-01-18 00:38:21 +01001558 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 }
1560 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 }
1562exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001563 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001564}
1565
1566
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001567/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1568 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001570static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1571 struct tipc_msg *tunnel_hdr,
1572 struct tipc_msg *msg,
1573 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001575 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576 struct sk_buff *buf;
1577 u32 length = msg_size(msg);
1578
1579 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001580 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001581 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001583 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001585 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001586 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001587 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001589 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001590 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1591 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001592 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593}
1594
1595
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001596/* tipc_link_failover_send_queue(): A link has gone down, but a second
1597 * link is still active. We can do failover. Tunnel the failing link's
1598 * whole send queue via the remaining link. This way, we don't lose
1599 * any packets, and sequence order is preserved for subsequent traffic
1600 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001601 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001602void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603{
Ying Xue58dc55f2014-11-26 11:41:52 +08001604 u32 msgcount = skb_queue_len(&l_ptr->outqueue);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001605 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 struct tipc_msg tunnel_hdr;
Ying Xue58dc55f2014-11-26 11:41:52 +08001607 struct sk_buff *skb;
Allan Stephens5392d642006-06-25 23:52:50 -07001608 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609
1610 if (!tunnel)
1611 return;
1612
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001613 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001614 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1616 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001617
Ying Xue58dc55f2014-11-26 11:41:52 +08001618 if (skb_queue_empty(&l_ptr->outqueue)) {
1619 skb = tipc_buf_acquire(INT_H_SIZE);
1620 if (skb) {
1621 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001623 __tipc_link_xmit(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001625 pr_warn("%sunable to send changeover msg\n",
1626 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 }
1628 return;
1629 }
Allan Stephensf1310722006-06-25 23:51:37 -07001630
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001631 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001632 l_ptr->owner->active_links[1]);
1633
Ying Xue58dc55f2014-11-26 11:41:52 +08001634 skb_queue_walk(&l_ptr->outqueue, skb) {
1635 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636
1637 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001639 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640
Florian Westphald788d802007-08-02 19:28:06 -07001641 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001643 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001644 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1645 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 pos += align(msg_size(m));
1647 m = (struct tipc_msg *)pos;
1648 }
1649 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001650 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1651 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 }
1654}
1655
Ying Xue247f0f32014-02-18 16:06:46 +08001656/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001657 * duplicate of the first link's send queue via the new link. This way, we
1658 * are guaranteed that currently queued packets from a socket are delivered
1659 * before future traffic from the same socket, even if this is using the
1660 * new link. The last arriving copy of each duplicate packet is dropped at
1661 * the receiving end by the regular protocol check, so packet cardinality
1662 * and sequence order is preserved per sender/receiver socket pair.
1663 * Owner node is locked.
1664 */
Ying Xue247f0f32014-02-18 16:06:46 +08001665void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001666 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667{
Ying Xue58dc55f2014-11-26 11:41:52 +08001668 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 struct tipc_msg tunnel_hdr;
1670
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001671 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001672 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Ying Xue58dc55f2014-11-26 11:41:52 +08001673 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
Ying Xue58dc55f2014-11-26 11:41:52 +08001675 skb_queue_walk(&l_ptr->outqueue, skb) {
1676 struct sk_buff *outskb;
1677 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 u32 length = msg_size(msg);
1679
1680 if (msg_user(msg) == MSG_BUNDLER)
1681 msg_set_type(msg, CLOSED_MSG);
1682 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001683 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001685 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1686 if (outskb == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001687 pr_warn("%sunable to send duplicate msg\n",
1688 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689 return;
1690 }
Ying Xue58dc55f2014-11-26 11:41:52 +08001691 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE);
1692 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data,
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001693 length);
Ying Xue58dc55f2014-11-26 11:41:52 +08001694 __tipc_link_xmit(tunnel, outskb);
Per Liden4323add2006-01-18 00:38:21 +01001695 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 }
1698}
1699
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700/**
1701 * buf_extract - extracts embedded TIPC message from another message
1702 * @skb: encapsulating message buffer
1703 * @from_pos: offset to extract from
1704 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001705 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 * encapsulating message itself is left unchanged.
1707 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1709{
1710 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1711 u32 size = msg_size(msg);
1712 struct sk_buff *eb;
1713
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001714 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001716 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 return eb;
1718}
1719
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001720
1721
1722/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1723 * Owner node is locked.
1724 */
1725static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1726 struct sk_buff *t_buf)
1727{
1728 struct sk_buff *buf;
1729
1730 if (!tipc_link_is_up(l_ptr))
1731 return;
1732
1733 buf = buf_extract(t_buf, INT_H_SIZE);
1734 if (buf == NULL) {
1735 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1736 return;
1737 }
1738
1739 /* Add buffer to deferred queue, if applicable: */
1740 link_handle_out_of_seq_msg(l_ptr, buf);
1741}
1742
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001743/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1744 * Owner node is locked.
1745 */
1746static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1747 struct sk_buff *t_buf)
1748{
1749 struct tipc_msg *t_msg = buf_msg(t_buf);
1750 struct sk_buff *buf = NULL;
1751 struct tipc_msg *msg;
1752
1753 if (tipc_link_is_up(l_ptr))
1754 tipc_link_reset(l_ptr);
1755
1756 /* First failover packet? */
1757 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1758 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1759
1760 /* Should there be an inner packet? */
1761 if (l_ptr->exp_msg_count) {
1762 l_ptr->exp_msg_count--;
1763 buf = buf_extract(t_buf, INT_H_SIZE);
1764 if (buf == NULL) {
1765 pr_warn("%sno inner failover pkt\n", link_co_err);
1766 goto exit;
1767 }
1768 msg = buf_msg(buf);
1769
1770 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1771 kfree_skb(buf);
1772 buf = NULL;
1773 goto exit;
1774 }
1775 if (msg_user(msg) == MSG_FRAGMENTER) {
1776 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001777 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001778 }
1779 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001780exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001781 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1782 tipc_node_detach_link(l_ptr->owner, l_ptr);
1783 kfree(l_ptr);
1784 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001785 return buf;
1786}
1787
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001788/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001789 * via other link as result of a failover (ORIGINAL_MSG) or
1790 * a new active link (DUPLICATE_MSG). Failover packets are
1791 * returned to the active link for delivery upwards.
1792 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001794static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001795 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001797 struct sk_buff *t_buf = *buf;
1798 struct tipc_link *l_ptr;
1799 struct tipc_msg *t_msg = buf_msg(t_buf);
1800 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001802 *buf = NULL;
1803
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001804 if (bearer_id >= MAX_BEARERS)
1805 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001806
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001807 l_ptr = n_ptr->links[bearer_id];
1808 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001810
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001811 if (msg_type(t_msg) == DUPLICATE_MSG)
1812 tipc_link_dup_rcv(l_ptr, t_buf);
1813 else if (msg_type(t_msg) == ORIGINAL_MSG)
1814 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001815 else
1816 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001818 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001819 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820}
1821
1822/*
1823 * Bundler functionality:
1824 */
Ying Xue247f0f32014-02-18 16:06:46 +08001825void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826{
1827 u32 msgcount = msg_msgcnt(buf_msg(buf));
1828 u32 pos = INT_H_SIZE;
1829 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001830 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001831
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 while (msgcount--) {
1833 obuf = buf_extract(buf, pos);
1834 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001835 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001836 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001837 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001838 omsg = buf_msg(obuf);
1839 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001840 if (msg_isdata(omsg)) {
1841 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1842 tipc_sk_mcast_rcv(obuf);
1843 else
1844 tipc_sk_rcv(obuf);
1845 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001846 tipc_sk_rcv(obuf);
1847 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1848 tipc_named_rcv(obuf);
1849 } else {
1850 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1851 kfree_skb(obuf);
1852 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001854 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855}
1856
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001857static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001859 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1860 return;
1861
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 l_ptr->tolerance = tolerance;
1863 l_ptr->continuity_interval =
1864 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1865 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1866}
1867
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001868void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869{
1870 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001871 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1872 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1873 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1874 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001876 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1877 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1878 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1879 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1882 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1883 /* FRAGMENT and LAST_FRAGMENT packets */
1884 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1885}
1886
Jon Paul Maloye099e862014-02-13 17:29:18 -05001887/* tipc_link_find_owner - locate owner node of link by link's name
1888 * @name: pointer to link name string
1889 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001890 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001891 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001893static struct tipc_node *tipc_link_find_owner(const char *link_name,
1894 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001896 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001897 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001898 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001899 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900
Jon Paul Maloye099e862014-02-13 17:29:18 -05001901 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001902 rcu_read_lock();
1903 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001904 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001905 for (i = 0; i < MAX_BEARERS; i++) {
1906 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001907 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1908 *bearer_id = i;
1909 found_node = n_ptr;
1910 break;
1911 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001912 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001913 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001914 if (found_node)
1915 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001916 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001917 rcu_read_unlock();
1918
Jon Paul Maloye099e862014-02-13 17:29:18 -05001919 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920}
1921
Allan Stephens5c216e12011-10-18 11:34:29 -04001922/**
1923 * link_value_is_valid -- validate proposed link tolerance/priority/window
1924 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001925 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1926 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001927 *
1928 * Returns 1 if value is within range, 0 if not.
1929 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001930static int link_value_is_valid(u16 cmd, u32 new_value)
1931{
1932 switch (cmd) {
1933 case TIPC_CMD_SET_LINK_TOL:
1934 return (new_value >= TIPC_MIN_LINK_TOL) &&
1935 (new_value <= TIPC_MAX_LINK_TOL);
1936 case TIPC_CMD_SET_LINK_PRI:
1937 return (new_value <= TIPC_MAX_LINK_PRI);
1938 case TIPC_CMD_SET_LINK_WINDOW:
1939 return (new_value >= TIPC_MIN_LINK_WIN) &&
1940 (new_value <= TIPC_MAX_LINK_WIN);
1941 }
1942 return 0;
1943}
1944
Allan Stephens5c216e12011-10-18 11:34:29 -04001945/**
1946 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00001947 * @name: ptr to link, bearer, or media name
1948 * @new_value: new value of link, bearer, or media setting
1949 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04001950 *
Ying Xue7216cd92014-04-21 10:55:48 +08001951 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04001952 *
1953 * Returns 0 if value updated and negative value on error.
1954 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001955static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
1956{
1957 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001958 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04001959 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05001960 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001961 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02001962 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04001963
Jon Paul Maloye099e862014-02-13 17:29:18 -05001964 node = tipc_link_find_owner(name, &bearer_id);
1965 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04001966 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001967 l_ptr = node->links[bearer_id];
1968
1969 if (l_ptr) {
1970 switch (cmd) {
1971 case TIPC_CMD_SET_LINK_TOL:
1972 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08001973 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
1974 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001975 break;
1976 case TIPC_CMD_SET_LINK_PRI:
1977 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08001978 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
1979 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001980 break;
1981 case TIPC_CMD_SET_LINK_WINDOW:
1982 tipc_link_set_queue_limits(l_ptr, new_value);
1983 break;
1984 default:
1985 res = -EINVAL;
1986 break;
1987 }
Allan Stephens5c216e12011-10-18 11:34:29 -04001988 }
1989 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02001990 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04001991 }
1992
1993 b_ptr = tipc_bearer_find(name);
1994 if (b_ptr) {
1995 switch (cmd) {
1996 case TIPC_CMD_SET_LINK_TOL:
1997 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02001998 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04001999 case TIPC_CMD_SET_LINK_PRI:
2000 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002001 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002002 case TIPC_CMD_SET_LINK_WINDOW:
2003 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002004 break;
2005 default:
2006 res = -EINVAL;
2007 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002008 }
Ying Xue636c0372013-10-18 07:23:20 +02002009 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002010 }
2011
2012 m_ptr = tipc_media_find(name);
2013 if (!m_ptr)
2014 return -ENODEV;
2015 switch (cmd) {
2016 case TIPC_CMD_SET_LINK_TOL:
2017 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002018 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002019 case TIPC_CMD_SET_LINK_PRI:
2020 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002021 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002022 case TIPC_CMD_SET_LINK_WINDOW:
2023 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002024 break;
2025 default:
2026 res = -EINVAL;
2027 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002028 }
Ying Xue636c0372013-10-18 07:23:20 +02002029 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002030}
2031
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002032struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002033 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034{
2035 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002036 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002037 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038
2039 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002040 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041
2042 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2043 new_value = ntohl(args->value);
2044
Allan Stephens5c216e12011-10-18 11:34:29 -04002045 if (!link_value_is_valid(cmd, new_value))
2046 return tipc_cfg_reply_error_string(
2047 "cannot change, value invalid");
2048
Per Liden4323add2006-01-18 00:38:21 +01002049 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002050 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002051 (tipc_bclink_set_queue_limits(new_value) == 0))
2052 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002053 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002054 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 }
2056
Allan Stephens5c216e12011-10-18 11:34:29 -04002057 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002059 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002060
Per Liden4323add2006-01-18 00:38:21 +01002061 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062}
2063
2064/**
2065 * link_reset_statistics - reset link statistics
2066 * @l_ptr: pointer to link
2067 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002068static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069{
2070 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2071 l_ptr->stats.sent_info = l_ptr->next_out_no;
2072 l_ptr->stats.recv_info = l_ptr->next_in_no;
2073}
2074
Per Liden4323add2006-01-18 00:38:21 +01002075struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076{
2077 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002078 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002079 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002080 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081
2082 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002083 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084
2085 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002086 if (!strcmp(link_name, tipc_bclink_name)) {
2087 if (tipc_bclink_reset_stats())
2088 return tipc_cfg_reply_error_string("link not found");
2089 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002090 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002091 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002092 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002093 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002094
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002095 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002096 l_ptr = node->links[bearer_id];
2097 if (!l_ptr) {
2098 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002099 return tipc_cfg_reply_error_string("link not found");
2100 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002102 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002103 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104}
2105
2106/**
2107 * percent - convert count to a percentage of total (rounding up or down)
2108 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002109static u32 percent(u32 count, u32 total)
2110{
2111 return (count * 100 + (total / 2)) / total;
2112}
2113
2114/**
Per Liden4323add2006-01-18 00:38:21 +01002115 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116 * @name: link name
2117 * @buf: print buffer area
2118 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002119 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120 * Returns length of print buffer data string (or 0 if error)
2121 */
Per Liden4323add2006-01-18 00:38:21 +01002122static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002124 struct tipc_link *l;
2125 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002126 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127 char *status;
2128 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002129 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002130 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131
Per Liden4323add2006-01-18 00:38:21 +01002132 if (!strcmp(name, tipc_bclink_name))
2133 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134
Jon Paul Maloye099e862014-02-13 17:29:18 -05002135 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002136 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002137 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002138
Per Liden4323add2006-01-18 00:38:21 +01002139 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002140
2141 l = node->links[bearer_id];
2142 if (!l) {
2143 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002144 return 0;
2145 }
2146
Erik Hugnedc1aed32012-06-29 00:50:23 -04002147 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148
Erik Hugnedc1aed32012-06-29 00:50:23 -04002149 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002151 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 status = "STANDBY";
2153 else
2154 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002155
2156 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2157 " %s MTU:%u Priority:%u Tolerance:%u ms"
2158 " Window:%u packets\n",
2159 l->name, status, l->max_pkt, l->priority,
2160 l->tolerance, l->queue_limit[0]);
2161
2162 ret += tipc_snprintf(buf + ret, buf_size - ret,
2163 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2164 l->next_in_no - s->recv_info, s->recv_fragments,
2165 s->recv_fragmented, s->recv_bundles,
2166 s->recv_bundled);
2167
2168 ret += tipc_snprintf(buf + ret, buf_size - ret,
2169 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2170 l->next_out_no - s->sent_info, s->sent_fragments,
2171 s->sent_fragmented, s->sent_bundles,
2172 s->sent_bundled);
2173
2174 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175 if (!profile_total)
2176 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002177
2178 ret += tipc_snprintf(buf + ret, buf_size - ret,
2179 " TX profile sample:%u packets average:%u octets\n"
2180 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2181 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2182 s->msg_length_counts,
2183 s->msg_lengths_total / profile_total,
2184 percent(s->msg_length_profile[0], profile_total),
2185 percent(s->msg_length_profile[1], profile_total),
2186 percent(s->msg_length_profile[2], profile_total),
2187 percent(s->msg_length_profile[3], profile_total),
2188 percent(s->msg_length_profile[4], profile_total),
2189 percent(s->msg_length_profile[5], profile_total),
2190 percent(s->msg_length_profile[6], profile_total));
2191
2192 ret += tipc_snprintf(buf + ret, buf_size - ret,
2193 " RX states:%u probes:%u naks:%u defs:%u"
2194 " dups:%u\n", s->recv_states, s->recv_probes,
2195 s->recv_nacks, s->deferred_recv, s->duplicates);
2196
2197 ret += tipc_snprintf(buf + ret, buf_size - ret,
2198 " TX states:%u probes:%u naks:%u acks:%u"
2199 " dups:%u\n", s->sent_states, s->sent_probes,
2200 s->sent_nacks, s->sent_acks, s->retransmitted);
2201
2202 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002203 " Congestion link:%u Send queue"
2204 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002205 s->max_queue_sz, s->queue_sz_counts ?
2206 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002207
Per Liden4323add2006-01-18 00:38:21 +01002208 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002209 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210}
2211
Per Liden4323add2006-01-18 00:38:21 +01002212struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002213{
2214 struct sk_buff *buf;
2215 struct tlv_desc *rep_tlv;
2216 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002217 int pb_len;
2218 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002219
2220 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002221 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002222
Erik Hugnedc1aed32012-06-29 00:50:23 -04002223 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002224 if (!buf)
2225 return NULL;
2226
2227 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002228 pb = TLV_DATA(rep_tlv);
2229 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002230 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002231 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002232 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002233 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002234 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002235 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002236 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002237 skb_put(buf, TLV_SPACE(str_len));
2238 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2239
2240 return buf;
2241}
2242
Per Lidenb97bf3f2006-01-02 19:04:38 +01002243/**
Per Liden4323add2006-01-18 00:38:21 +01002244 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245 * @dest: network address of destination node
2246 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002247 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002248 * If no active link can be found, uses default maximum packet size.
2249 */
Per Liden4323add2006-01-18 00:38:21 +01002250u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002251{
David S. Miller6c000552008-09-02 23:38:32 -07002252 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002253 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002254 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002255
Per Lidenb97bf3f2006-01-02 19:04:38 +01002256 if (dest == tipc_own_addr)
2257 return MAX_MSG_SIZE;
2258
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002259 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002260 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002261 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002262 l_ptr = n_ptr->active_links[selector & 1];
2263 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002264 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002265 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002266 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 return res;
2268}
2269
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002270static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002271{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002272 struct tipc_bearer *b_ptr;
2273
2274 rcu_read_lock();
2275 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2276 if (b_ptr)
2277 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2278 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002279
Per Lidenb97bf3f2006-01-02 19:04:38 +01002280 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002281 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002282 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002283 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002284 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002285 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002286 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002287 pr_cont(":WW\n");
2288 else
2289 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002291
2292/* Parse and validate nested (link) properties valid for media, bearer and link
2293 */
2294int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2295{
2296 int err;
2297
2298 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2299 tipc_nl_prop_policy);
2300 if (err)
2301 return err;
2302
2303 if (props[TIPC_NLA_PROP_PRIO]) {
2304 u32 prio;
2305
2306 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2307 if (prio > TIPC_MAX_LINK_PRI)
2308 return -EINVAL;
2309 }
2310
2311 if (props[TIPC_NLA_PROP_TOL]) {
2312 u32 tol;
2313
2314 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2315 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2316 return -EINVAL;
2317 }
2318
2319 if (props[TIPC_NLA_PROP_WIN]) {
2320 u32 win;
2321
2322 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2323 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2324 return -EINVAL;
2325 }
2326
2327 return 0;
2328}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002329
Richard Alpef96ce7a2014-11-20 10:29:13 +01002330int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2331{
2332 int err;
2333 int res = 0;
2334 int bearer_id;
2335 char *name;
2336 struct tipc_link *link;
2337 struct tipc_node *node;
2338 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2339
2340 if (!info->attrs[TIPC_NLA_LINK])
2341 return -EINVAL;
2342
2343 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2344 info->attrs[TIPC_NLA_LINK],
2345 tipc_nl_link_policy);
2346 if (err)
2347 return err;
2348
2349 if (!attrs[TIPC_NLA_LINK_NAME])
2350 return -EINVAL;
2351
2352 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2353
2354 node = tipc_link_find_owner(name, &bearer_id);
2355 if (!node)
2356 return -EINVAL;
2357
2358 tipc_node_lock(node);
2359
2360 link = node->links[bearer_id];
2361 if (!link) {
2362 res = -EINVAL;
2363 goto out;
2364 }
2365
2366 if (attrs[TIPC_NLA_LINK_PROP]) {
2367 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2368
2369 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2370 props);
2371 if (err) {
2372 res = err;
2373 goto out;
2374 }
2375
2376 if (props[TIPC_NLA_PROP_TOL]) {
2377 u32 tol;
2378
2379 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2380 link_set_supervision_props(link, tol);
2381 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2382 }
2383 if (props[TIPC_NLA_PROP_PRIO]) {
2384 u32 prio;
2385
2386 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2387 link->priority = prio;
2388 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2389 }
2390 if (props[TIPC_NLA_PROP_WIN]) {
2391 u32 win;
2392
2393 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2394 tipc_link_set_queue_limits(link, win);
2395 }
2396 }
2397
2398out:
2399 tipc_node_unlock(node);
2400
2401 return res;
2402}
Richard Alped8182802014-11-24 11:10:29 +01002403
2404static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002405{
2406 int i;
2407 struct nlattr *stats;
2408
2409 struct nla_map {
2410 u32 key;
2411 u32 val;
2412 };
2413
2414 struct nla_map map[] = {
2415 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2416 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2417 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2418 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2419 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2420 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2421 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2422 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2423 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2424 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2425 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2426 s->msg_length_counts : 1},
2427 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2428 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2429 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2430 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2431 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2432 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2433 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2434 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2435 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2436 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2437 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2438 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2439 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2440 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2441 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2442 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2443 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2444 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2445 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2446 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2447 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2448 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2449 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2450 };
2451
2452 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2453 if (!stats)
2454 return -EMSGSIZE;
2455
2456 for (i = 0; i < ARRAY_SIZE(map); i++)
2457 if (nla_put_u32(skb, map[i].key, map[i].val))
2458 goto msg_full;
2459
2460 nla_nest_end(skb, stats);
2461
2462 return 0;
2463msg_full:
2464 nla_nest_cancel(skb, stats);
2465
2466 return -EMSGSIZE;
2467}
2468
2469/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002470static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002471{
2472 int err;
2473 void *hdr;
2474 struct nlattr *attrs;
2475 struct nlattr *prop;
2476
2477 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2478 NLM_F_MULTI, TIPC_NL_LINK_GET);
2479 if (!hdr)
2480 return -EMSGSIZE;
2481
2482 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2483 if (!attrs)
2484 goto msg_full;
2485
2486 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2487 goto attr_msg_full;
2488 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2489 tipc_cluster_mask(tipc_own_addr)))
2490 goto attr_msg_full;
2491 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2492 goto attr_msg_full;
2493 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2494 goto attr_msg_full;
2495 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2496 goto attr_msg_full;
2497
2498 if (tipc_link_is_up(link))
2499 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2500 goto attr_msg_full;
2501 if (tipc_link_is_active(link))
2502 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2503 goto attr_msg_full;
2504
2505 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2506 if (!prop)
2507 goto attr_msg_full;
2508 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2509 goto prop_msg_full;
2510 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2511 goto prop_msg_full;
2512 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2513 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2514 goto prop_msg_full;
2515 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2516 goto prop_msg_full;
2517 nla_nest_end(msg->skb, prop);
2518
2519 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2520 if (err)
2521 goto attr_msg_full;
2522
2523 nla_nest_end(msg->skb, attrs);
2524 genlmsg_end(msg->skb, hdr);
2525
2526 return 0;
2527
2528prop_msg_full:
2529 nla_nest_cancel(msg->skb, prop);
2530attr_msg_full:
2531 nla_nest_cancel(msg->skb, attrs);
2532msg_full:
2533 genlmsg_cancel(msg->skb, hdr);
2534
2535 return -EMSGSIZE;
2536}
2537
2538/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002539static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2540 struct tipc_node *node,
2541 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002542{
2543 u32 i;
2544 int err;
2545
2546 for (i = *prev_link; i < MAX_BEARERS; i++) {
2547 *prev_link = i;
2548
2549 if (!node->links[i])
2550 continue;
2551
2552 err = __tipc_nl_add_link(msg, node->links[i]);
2553 if (err)
2554 return err;
2555 }
2556 *prev_link = 0;
2557
2558 return 0;
2559}
2560
2561int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2562{
2563 struct tipc_node *node;
2564 struct tipc_nl_msg msg;
2565 u32 prev_node = cb->args[0];
2566 u32 prev_link = cb->args[1];
2567 int done = cb->args[2];
2568 int err;
2569
2570 if (done)
2571 return 0;
2572
2573 msg.skb = skb;
2574 msg.portid = NETLINK_CB(cb->skb).portid;
2575 msg.seq = cb->nlh->nlmsg_seq;
2576
2577 rcu_read_lock();
2578
2579 if (prev_node) {
2580 node = tipc_node_find(prev_node);
2581 if (!node) {
2582 /* We never set seq or call nl_dump_check_consistent()
2583 * this means that setting prev_seq here will cause the
2584 * consistence check to fail in the netlink callback
2585 * handler. Resulting in the last NLMSG_DONE message
2586 * having the NLM_F_DUMP_INTR flag set.
2587 */
2588 cb->prev_seq = 1;
2589 goto out;
2590 }
2591
2592 list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
2593 tipc_node_lock(node);
2594 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2595 tipc_node_unlock(node);
2596 if (err)
2597 goto out;
2598
2599 prev_node = node->addr;
2600 }
2601 } else {
2602 err = tipc_nl_add_bc_link(&msg);
2603 if (err)
2604 goto out;
2605
2606 list_for_each_entry_rcu(node, &tipc_node_list, list) {
2607 tipc_node_lock(node);
2608 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2609 tipc_node_unlock(node);
2610 if (err)
2611 goto out;
2612
2613 prev_node = node->addr;
2614 }
2615 }
2616 done = 1;
2617out:
2618 rcu_read_unlock();
2619
2620 cb->args[0] = prev_node;
2621 cb->args[1] = prev_link;
2622 cb->args[2] = done;
2623
2624 return skb->len;
2625}
2626
2627int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2628{
2629 struct sk_buff *ans_skb;
2630 struct tipc_nl_msg msg;
2631 struct tipc_link *link;
2632 struct tipc_node *node;
2633 char *name;
2634 int bearer_id;
2635 int err;
2636
2637 if (!info->attrs[TIPC_NLA_LINK_NAME])
2638 return -EINVAL;
2639
2640 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2641 node = tipc_link_find_owner(name, &bearer_id);
2642 if (!node)
2643 return -EINVAL;
2644
2645 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2646 if (!ans_skb)
2647 return -ENOMEM;
2648
2649 msg.skb = ans_skb;
2650 msg.portid = info->snd_portid;
2651 msg.seq = info->snd_seq;
2652
2653 tipc_node_lock(node);
2654 link = node->links[bearer_id];
2655 if (!link) {
2656 err = -EINVAL;
2657 goto err_out;
2658 }
2659
2660 err = __tipc_nl_add_link(&msg, link);
2661 if (err)
2662 goto err_out;
2663
2664 tipc_node_unlock(node);
2665
2666 return genlmsg_reply(ans_skb, info);
2667
2668err_out:
2669 tipc_node_unlock(node);
2670 nlmsg_free(ans_skb);
2671
2672 return err;
2673}
Richard Alpeae363422014-11-20 10:29:14 +01002674
2675int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2676{
2677 int err;
2678 char *link_name;
2679 unsigned int bearer_id;
2680 struct tipc_link *link;
2681 struct tipc_node *node;
2682 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2683
2684 if (!info->attrs[TIPC_NLA_LINK])
2685 return -EINVAL;
2686
2687 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2688 info->attrs[TIPC_NLA_LINK],
2689 tipc_nl_link_policy);
2690 if (err)
2691 return err;
2692
2693 if (!attrs[TIPC_NLA_LINK_NAME])
2694 return -EINVAL;
2695
2696 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2697
2698 if (strcmp(link_name, tipc_bclink_name) == 0) {
2699 err = tipc_bclink_reset_stats();
2700 if (err)
2701 return err;
2702 return 0;
2703 }
2704
2705 node = tipc_link_find_owner(link_name, &bearer_id);
2706 if (!node)
2707 return -EINVAL;
2708
2709 tipc_node_lock(node);
2710
2711 link = node->links[bearer_id];
2712 if (!link) {
2713 tipc_node_unlock(node);
2714 return -EINVAL;
2715 }
2716
2717 link_reset_statistics(link);
2718
2719 tipc_node_unlock(node);
2720
2721 return 0;
2722}