blob: 4b7cbfd5771443631fcd16734b8f6e63aa0b065f [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
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500152static u32 link_next_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153{
154 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -0400155 return buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 return mod(l_ptr->next_out_no);
157}
158
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500159static u32 link_last_sent(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160{
161 return mod(link_next_sent(l_ptr) - 1);
162}
163
164/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800165 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500167int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168{
169 if (!l_ptr)
170 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000171 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172}
173
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500174int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000176 return (l_ptr->owner->active_links[0] == l_ptr) ||
177 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178}
179
180/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 * link_timeout - handle expiration of link timer
182 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500184static void link_timeout(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185{
Per Liden4323add2006-01-18 00:38:21 +0100186 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187
188 /* update counters used in statistical profiling of send traffic */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
190 l_ptr->stats.queue_sz_counts++;
191
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 if (l_ptr->first_out) {
193 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
194 u32 length = msg_size(msg);
195
Joe Perchesf64f9e72009-11-29 16:55:45 -0800196 if ((msg_user(msg) == MSG_FRAGMENTER) &&
197 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 length = msg_size(msg_get_wrapped(msg));
199 }
200 if (length) {
201 l_ptr->stats.msg_lengths_total += length;
202 l_ptr->stats.msg_length_counts++;
203 if (length <= 64)
204 l_ptr->stats.msg_length_profile[0]++;
205 else if (length <= 256)
206 l_ptr->stats.msg_length_profile[1]++;
207 else if (length <= 1024)
208 l_ptr->stats.msg_length_profile[2]++;
209 else if (length <= 4096)
210 l_ptr->stats.msg_length_profile[3]++;
211 else if (length <= 16384)
212 l_ptr->stats.msg_length_profile[4]++;
213 else if (length <= 32768)
214 l_ptr->stats.msg_length_profile[5]++;
215 else
216 l_ptr->stats.msg_length_profile[6]++;
217 }
218 }
219
220 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221
222 link_state_event(l_ptr, TIMEOUT_EVT);
223
224 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100225 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226
Per Liden4323add2006-01-18 00:38:21 +0100227 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228}
229
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500230static void link_set_timer(struct tipc_link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231{
232 k_start_timer(&l_ptr->timer, time);
233}
234
235/**
Per Liden4323add2006-01-18 00:38:21 +0100236 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500237 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900240 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 * Returns pointer to link.
242 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500243struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500244 struct tipc_bearer *b_ptr,
245 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500247 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 struct tipc_msg *msg;
249 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500250 char addr_string[16];
251 u32 peer = n_ptr->addr;
252
Holger Brunck0372bf52014-11-14 18:33:19 +0100253 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500254 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100255 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
256 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500257 return NULL;
258 }
259
260 if (n_ptr->links[b_ptr->identity]) {
261 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400262 pr_err("Attempt to establish second link on <%s> to %s\n",
263 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500264 return NULL;
265 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700267 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400269 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 return NULL;
271 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272
273 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500274 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400275 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900277 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 if_name,
279 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400280 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500282 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400284 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800285 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400286 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 l_ptr->state = RESET_UNKNOWN;
288
289 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
290 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000291 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700293 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 msg_set_bearer_id(msg, b_ptr->identity);
295 strcpy((char *)msg_data(msg), if_name);
296
297 l_ptr->priority = b_ptr->priority;
Allan Stephens5c216e12011-10-18 11:34:29 -0400298 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299
Ying Xue7a2f7d12014-04-21 10:55:46 +0800300 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 link_init_max_pkt(l_ptr);
302
303 l_ptr->next_out_no = 1;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400304 __skb_queue_head_init(&l_ptr->waiting_sks);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
306 link_reset_statistics(l_ptr);
307
Allan Stephens37b9c082011-02-28 11:32:27 -0500308 tipc_node_attach_link(n_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309
Jon Paul Maloy170b3922014-01-07 17:02:41 -0500310 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
311 (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500312
313 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 return l_ptr;
316}
317
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500318void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500319{
320 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500321 struct tipc_node *n_ptr;
Ying Xue8d8439b2014-02-13 17:29:07 -0500322
Ying Xue6c7a7622014-03-27 12:54:37 +0800323 rcu_read_lock();
324 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800325 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500326 l_ptr = n_ptr->links[bearer_id];
327 if (l_ptr) {
328 tipc_link_reset(l_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500329 if (shutting_down || !tipc_node_is_up(n_ptr)) {
330 tipc_node_detach_link(l_ptr->owner, l_ptr);
331 tipc_link_reset_fragments(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800332 tipc_node_unlock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500333
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500334 /* Nobody else can access this link now: */
335 del_timer_sync(&l_ptr->timer);
336 kfree(l_ptr);
337 } else {
338 /* Detach/delete when failover is finished: */
339 l_ptr->flags |= LINK_STOPPED;
Ying Xue5356f3d2014-05-05 08:56:09 +0800340 tipc_node_unlock(n_ptr);
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500341 del_timer_sync(&l_ptr->timer);
342 }
Ying Xuec61dd612014-02-13 17:29:09 -0500343 continue;
344 }
Ying Xue5356f3d2014-05-05 08:56:09 +0800345 tipc_node_unlock(n_ptr);
Ying Xue8d8439b2014-02-13 17:29:07 -0500346 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800347 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500348}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349
350/**
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400351 * link_schedule_user - schedule user for wakeup after congestion
352 * @link: congested link
353 * @oport: sending port
354 * @chain_sz: size of buffer chain that was attempted sent
355 * @imp: importance of message attempted sent
356 * Create pseudo msg to send back to user when congestion abates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 */
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400358static bool link_schedule_user(struct tipc_link *link, u32 oport,
359 uint chain_sz, uint imp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400361 struct sk_buff *buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400363 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, tipc_own_addr,
364 tipc_own_addr, oport, 0, 0);
365 if (!buf)
366 return false;
367 TIPC_SKB_CB(buf)->chain_sz = chain_sz;
368 TIPC_SKB_CB(buf)->chain_imp = imp;
369 __skb_queue_tail(&link->waiting_sks, buf);
370 link->stats.link_congs++;
371 return true;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372}
373
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400374/**
375 * link_prepare_wakeup - prepare users for wakeup after congestion
376 * @link: congested link
377 * Move a number of waiting users, as permitted by available space in
378 * the send queue, from link wait queue to node wait queue for wakeup
379 */
380static void link_prepare_wakeup(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381{
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400382 struct sk_buff_head *wq = &link->waiting_sks;
383 struct sk_buff *buf;
384 uint pend_qsz = link->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400386 for (buf = skb_peek(wq); buf; buf = skb_peek(wq)) {
387 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(buf)->chain_imp])
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400389 pend_qsz += TIPC_SKB_CB(buf)->chain_sz;
390 __skb_queue_tail(&link->owner->waiting_sks, __skb_dequeue(wq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392}
393
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 * link_release_outqueue - purge link's outbound message queue
396 * @l_ptr: pointer to link
397 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500398static void link_release_outqueue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399{
Ying Xued77b3832013-12-10 20:45:38 -0800400 kfree_skb_list(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 l_ptr->first_out = NULL;
402 l_ptr->out_queue_size = 0;
403}
404
405/**
Per Liden4323add2006-01-18 00:38:21 +0100406 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 * @l_ptr: pointer to link
408 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500409void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400411 kfree_skb(l_ptr->reasm_buf);
412 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500416 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 * @l_ptr: pointer to link
418 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500419void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420{
Ying Xued77b3832013-12-10 20:45:38 -0800421 kfree_skb_list(l_ptr->oldest_deferred_in);
422 kfree_skb_list(l_ptr->first_out);
Per Liden4323add2006-01-18 00:38:21 +0100423 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424}
425
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500426void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 u32 prev_state = l_ptr->state;
429 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700430 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400431 struct tipc_node *owner = l_ptr->owner;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900432
Allan Stephensa686e682008-06-04 17:29:39 -0700433 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
Allan Stephensa686e682008-06-04 17:29:39 -0700435 /* Link is down, accept any session */
436 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900438 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900440
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442
443 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
444 return;
445
Per Liden4323add2006-01-18 00:38:21 +0100446 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800447 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000448
Jon Paul Maloyb9d4c332014-01-07 17:02:42 -0500449 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 l_ptr->reset_checkpoint = checkpoint;
451 l_ptr->exp_msg_count = START_CHANGEOVER;
452 }
453
454 /* Clean up all queues: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 link_release_outqueue(l_ptr);
Ying Xued77b3832013-12-10 20:45:38 -0800456 kfree_skb_list(l_ptr->oldest_deferred_in);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400457 if (!skb_queue_empty(&l_ptr->waiting_sks)) {
458 skb_queue_splice_init(&l_ptr->waiting_sks, &owner->waiting_sks);
459 owner->action_flags |= TIPC_WAKEUP_USERS;
460 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 l_ptr->last_out = NULL;
462 l_ptr->first_out = NULL;
463 l_ptr->next_out = NULL;
464 l_ptr->unacked_window = 0;
465 l_ptr->checkpoint = 1;
466 l_ptr->next_out_no = 1;
467 l_ptr->deferred_inqueue_sz = 0;
468 l_ptr->oldest_deferred_in = NULL;
469 l_ptr->newest_deferred_in = NULL;
470 l_ptr->fsm_msg_cnt = 0;
471 l_ptr->stale_count = 0;
472 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473}
474
Ying Xuec61dd612014-02-13 17:29:09 -0500475void tipc_link_reset_list(unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500476{
477 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500478 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500479
Ying Xue6c7a7622014-03-27 12:54:37 +0800480 rcu_read_lock();
481 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800482 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500483 l_ptr = n_ptr->links[bearer_id];
484 if (l_ptr)
485 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800486 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500487 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800488 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500489}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500491static void link_activate(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492{
Allan Stephens5392d642006-06-25 23:52:50 -0700493 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100494 tipc_node_link_up(l_ptr->owner, l_ptr);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800495 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496}
497
498/**
499 * link_state_event - link finite state machine
500 * @l_ptr: pointer to link
501 * @event: state machine event to process
502 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000503static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500505 struct tipc_link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 u32 cont_intv = l_ptr->continuity_interval;
507
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500508 if (l_ptr->flags & LINK_STOPPED)
509 return;
510
Ying Xue135daee2014-02-13 17:29:08 -0500511 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 return; /* Not yet. */
513
Ying Xue77a7e072013-12-10 20:45:44 -0800514 /* Check whether changeover is going on */
515 if (l_ptr->exp_msg_count) {
Allan Stephensa0168922010-12-31 18:59:35 +0000516 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800518 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520
521 switch (l_ptr->state) {
522 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 switch (event) {
524 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 break;
527 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 if (l_ptr->next_in_no != l_ptr->checkpoint) {
529 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100530 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800531 tipc_link_proto_xmit(l_ptr, STATE_MSG,
532 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 l_ptr->fsm_msg_cnt++;
534 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800535 tipc_link_proto_xmit(l_ptr, STATE_MSG,
536 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 l_ptr->fsm_msg_cnt++;
538 }
539 link_set_timer(l_ptr, cont_intv);
540 break;
541 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 l_ptr->state = WORKING_UNKNOWN;
543 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800544 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 l_ptr->fsm_msg_cnt++;
546 link_set_timer(l_ptr, cont_intv / 4);
547 break;
548 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400549 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
550 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100551 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 l_ptr->state = RESET_RESET;
553 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800554 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
555 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 l_ptr->fsm_msg_cnt++;
557 link_set_timer(l_ptr, cont_intv);
558 break;
559 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400560 pr_err("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 }
562 break;
563 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 switch (event) {
565 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 l_ptr->state = WORKING_WORKING;
568 l_ptr->fsm_msg_cnt = 0;
569 link_set_timer(l_ptr, cont_intv);
570 break;
571 case RESET_MSG:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400572 pr_info("%s<%s>, requested by peer while probing\n",
573 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100574 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 l_ptr->state = RESET_RESET;
576 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800577 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
578 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 l_ptr->fsm_msg_cnt++;
580 link_set_timer(l_ptr, cont_intv);
581 break;
582 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 l_ptr->state = WORKING_WORKING;
585 l_ptr->fsm_msg_cnt = 0;
586 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100587 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800588 tipc_link_proto_xmit(l_ptr, STATE_MSG,
589 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 l_ptr->fsm_msg_cnt++;
591 }
592 link_set_timer(l_ptr, cont_intv);
593 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800594 tipc_link_proto_xmit(l_ptr, STATE_MSG,
595 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 l_ptr->fsm_msg_cnt++;
597 link_set_timer(l_ptr, cont_intv / 4);
598 } else { /* Link has failed */
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400599 pr_warn("%s<%s>, peer not responding\n",
600 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100601 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 l_ptr->state = RESET_UNKNOWN;
603 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800604 tipc_link_proto_xmit(l_ptr, RESET_MSG,
605 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 l_ptr->fsm_msg_cnt++;
607 link_set_timer(l_ptr, cont_intv);
608 }
609 break;
610 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400611 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 }
613 break;
614 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 switch (event) {
616 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 break;
618 case ACTIVATE_MSG:
619 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000620 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 l_ptr->state = WORKING_WORKING;
623 l_ptr->fsm_msg_cnt = 0;
624 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800625 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800627 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800628 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 link_set_timer(l_ptr, cont_intv);
630 break;
631 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 l_ptr->state = RESET_RESET;
633 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800634 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
635 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 l_ptr->fsm_msg_cnt++;
637 link_set_timer(l_ptr, cont_intv);
638 break;
639 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500640 l_ptr->flags |= LINK_STARTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 /* fall through */
642 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800643 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 l_ptr->fsm_msg_cnt++;
645 link_set_timer(l_ptr, cont_intv);
646 break;
647 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400648 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 }
650 break;
651 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 switch (event) {
653 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 case ACTIVATE_MSG:
655 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000656 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658 l_ptr->state = WORKING_WORKING;
659 l_ptr->fsm_msg_cnt = 0;
660 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800661 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800663 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800664 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 link_set_timer(l_ptr, cont_intv);
666 break;
667 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 break;
669 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800670 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
671 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 l_ptr->fsm_msg_cnt++;
673 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 break;
675 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400676 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677 }
678 break;
679 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400680 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 }
682}
683
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500684/* tipc_link_cong: determine return value and how to treat the
685 * sent buffer during link congestion.
686 * - For plain, errorless user data messages we keep the buffer and
687 * return -ELINKONG.
688 * - For all other messages we discard the buffer and return -EHOSTUNREACH
689 * - For TIPC internal messages we also reset the link
690 */
691static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
692{
693 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500694 uint imp = tipc_msg_tot_importance(msg);
695 u32 oport = msg_tot_origport(msg);
696
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400697 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500698 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
699 tipc_link_reset(link);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400700 goto drop;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500701 }
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400702 if (unlikely(msg_errcode(msg)))
703 goto drop;
704 if (unlikely(msg_reroute_cnt(msg)))
705 goto drop;
706 if (TIPC_SKB_CB(buf)->wakeup_pending)
707 return -ELINKCONG;
708 if (link_schedule_user(link, oport, TIPC_SKB_CB(buf)->chain_sz, imp))
709 return -ELINKCONG;
710drop:
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500711 kfree_skb_list(buf);
712 return -EHOSTUNREACH;
713}
714
715/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400716 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500717 * @link: link to use
718 * @buf: chain of buffers containing message
719 * Consumes the buffer chain, except when returning -ELINKCONG
720 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
721 * user data messages) or -EHOSTUNREACH (all other messages/senders)
722 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
723 * to act on the return value, since they may need to do more send attempts.
724 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400725int __tipc_link_xmit(struct tipc_link *link, struct sk_buff *buf)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500726{
727 struct tipc_msg *msg = buf_msg(buf);
728 uint psz = msg_size(msg);
729 uint qsz = link->out_queue_size;
730 uint sndlim = link->queue_limit[0];
731 uint imp = tipc_msg_tot_importance(msg);
732 uint mtu = link->max_pkt;
733 uint ack = mod(link->next_in_no - 1);
734 uint seqno = link->next_out_no;
735 uint bc_last_in = link->owner->bclink.last_in;
736 struct tipc_media_addr *addr = &link->media_addr;
737 struct sk_buff *next = buf->next;
738
739 /* Match queue limits against msg importance: */
740 if (unlikely(qsz >= link->queue_limit[imp]))
741 return tipc_link_cong(link, buf);
742
743 /* Has valid packet limit been used ? */
744 if (unlikely(psz > mtu)) {
745 kfree_skb_list(buf);
746 return -EMSGSIZE;
747 }
748
749 /* Prepare each packet for sending, and add to outqueue: */
750 while (buf) {
751 next = buf->next;
752 msg = buf_msg(buf);
753 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
754 msg_set_bcast_ack(msg, bc_last_in);
755
756 if (!link->first_out) {
757 link->first_out = buf;
758 } else if (qsz < sndlim) {
759 link->last_out->next = buf;
760 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
761 link->stats.sent_bundled++;
762 buf = next;
763 next = buf->next;
764 continue;
765 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
766 link->stats.sent_bundled++;
767 link->stats.sent_bundles++;
768 link->last_out->next = buf;
769 if (!link->next_out)
770 link->next_out = buf;
771 } else {
772 link->last_out->next = buf;
773 if (!link->next_out)
774 link->next_out = buf;
775 }
776
777 /* Send packet if possible: */
778 if (likely(++qsz <= sndlim)) {
779 tipc_bearer_send(link->bearer_id, buf, addr);
780 link->next_out = next;
781 link->unacked_window = 0;
782 }
783 seqno++;
784 link->last_out = buf;
785 buf = next;
786 }
787 link->next_out_no = seqno;
788 link->out_queue_size = qsz;
789 return 0;
790}
791
792/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400793 * tipc_link_xmit() is the general link level function for message sending
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500794 * @buf: chain of buffers containing message
795 * @dsz: amount of user data to be sent
796 * @dnode: address of destination node
797 * @selector: a number used for deterministic link selection
798 * Consumes the buffer chain, except when returning -ELINKCONG
799 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
800 */
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400801int tipc_link_xmit(struct sk_buff *buf, u32 dnode, u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500802{
803 struct tipc_link *link = NULL;
804 struct tipc_node *node;
805 int rc = -EHOSTUNREACH;
806
807 node = tipc_node_find(dnode);
808 if (node) {
809 tipc_node_lock(node);
810 link = node->active_links[selector & 1];
811 if (link)
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400812 rc = __tipc_link_xmit(link, buf);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500813 tipc_node_unlock(node);
814 }
815
816 if (link)
817 return rc;
818
819 if (likely(in_own_node(dnode)))
820 return tipc_sk_rcv(buf);
821
822 kfree_skb_list(buf);
823 return rc;
824}
825
Jon Maloyc64f7a62012-11-16 13:51:31 +0800826/*
Ying Xue247f0f32014-02-18 16:06:46 +0800827 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800828 *
829 * Give a newly added peer node the sequence number where it should
830 * start receiving and acking broadcast packets.
831 *
832 * Called with node locked
833 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400834static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800835{
836 struct sk_buff *buf;
837 struct tipc_msg *msg;
838
839 buf = tipc_buf_acquire(INT_H_SIZE);
840 if (!buf)
841 return;
842
843 msg = buf_msg(buf);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400844 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, link->addr);
845 msg_set_last_bcast(msg, link->owner->bclink.acked);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400846 __tipc_link_xmit(link, buf);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800847}
848
849/*
Ying Xue247f0f32014-02-18 16:06:46 +0800850 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800851 * Receive the sequence number where we should start receiving and
852 * acking broadcast packets from a newly added peer node, and open
853 * up for reception of such packets.
854 *
855 * Called with node locked
856 */
Ying Xue247f0f32014-02-18 16:06:46 +0800857static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800858{
859 struct tipc_msg *msg = buf_msg(buf);
860
861 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
862 n->bclink.recv_permitted = true;
863 kfree_skb(buf);
864}
865
866/*
Per Liden4323add2006-01-18 00:38:21 +0100867 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 */
stephen hemminger98056962014-01-04 13:47:48 -0800869static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870{
Ying Xue7b6f0872014-11-26 11:41:47 +0800871 struct sk_buff *buf = l_ptr->next_out;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 /* Send one deferred data message, if send window not full: */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 if (buf) {
875 struct tipc_msg *msg = buf_msg(buf);
876 u32 next = msg_seqno(msg);
Allan Stephensf9057302011-10-24 16:03:12 -0400877 u32 first = buf_seqno(l_ptr->first_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878
879 if (mod(next - first) < l_ptr->queue_limit[0]) {
880 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800882 tipc_bearer_send(l_ptr->bearer_id, buf,
883 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800884 if (msg_user(msg) == MSG_BUNDLER)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500885 msg_set_type(msg, BUNDLE_CLOSED);
Ying Xue3c294cb2012-11-15 11:34:45 +0800886 l_ptr->next_out = buf->next;
887 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 }
889 }
Ying Xue3c294cb2012-11-15 11:34:45 +0800890 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891}
892
893/*
894 * push_queue(): push out the unsent messages of a link where
895 * congestion has abated. Node is locked
896 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500897void tipc_link_push_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898{
899 u32 res;
900
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 do {
Per Liden4323add2006-01-18 00:38:21 +0100902 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700903 } while (!res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904}
905
Ying Xue3f5a12b2014-05-05 08:56:17 +0800906void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700907{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700908 char addr_string[16];
909 u32 i;
910
Ying Xue3f5a12b2014-05-05 08:56:17 +0800911 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700912
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400913 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800914 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700915
916 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800917 if (node->links[i]) {
918 link_print(node->links[i], "Resetting link\n");
919 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700920 }
921 }
922
Ying Xue3f5a12b2014-05-05 08:56:17 +0800923 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700924}
925
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500926static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400927 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700928{
929 struct tipc_msg *msg = buf_msg(buf);
930
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400931 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700932
933 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700934 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000935 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700936 tipc_link_reset(l_ptr);
937
938 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700939 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700940 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700941 char addr_string[16];
942
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400943 pr_info("Msg seq number: %u, ", msg_seqno(msg));
944 pr_cont("Outstanding acks: %lu\n",
945 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700946
Allan Stephens01d83ed2011-01-18 13:53:16 -0500947 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -0700948 tipc_node_lock(n_ptr);
949
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000950 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400951 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800952 pr_info("Reception permitted: %d, Acked: %u\n",
953 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400954 n_ptr->bclink.acked);
955 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
956 n_ptr->bclink.last_in,
957 n_ptr->bclink.oos_state,
958 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700959
Allan Stephensd356eeb2006-06-25 23:40:01 -0700960 tipc_node_unlock(n_ptr);
961
Ying Xue3f5a12b2014-05-05 08:56:17 +0800962 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700963 l_ptr->stale_count = 0;
964 }
965}
966
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500967void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +0100968 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969{
970 struct tipc_msg *msg;
971
Allan Stephensd356eeb2006-06-25 23:40:01 -0700972 if (!buf)
973 return;
974
975 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900976
Erik Hugne512137e2013-12-06 10:08:00 -0500977 /* Detect repeated retransmit failures */
978 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
979 if (++l_ptr->stale_count > 100) {
980 link_retransmit_failure(l_ptr, buf);
981 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700982 }
983 } else {
Erik Hugne512137e2013-12-06 10:08:00 -0500984 l_ptr->last_retransmitted = msg_seqno(msg);
985 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986 }
Allan Stephensd356eeb2006-06-25 23:40:01 -0700987
Neil Hormanca509102010-03-15 07:58:45 +0000988 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 msg = buf_msg(buf);
990 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900991 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800992 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +0800993 buf = buf->next;
994 retransmits--;
995 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997}
998
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900999/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 * link_insert_deferred_queue - insert deferred messages back into receive chain
1001 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001002static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 struct sk_buff *buf)
1004{
1005 u32 seq_no;
1006
1007 if (l_ptr->oldest_deferred_in == NULL)
1008 return buf;
1009
Allan Stephensf9057302011-10-24 16:03:12 -04001010 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 if (seq_no == mod(l_ptr->next_in_no)) {
1012 l_ptr->newest_deferred_in->next = buf;
1013 buf = l_ptr->oldest_deferred_in;
1014 l_ptr->oldest_deferred_in = NULL;
1015 l_ptr->deferred_inqueue_sz = 0;
1016 }
1017 return buf;
1018}
1019
Allan Stephens85035562008-04-15 19:04:54 -07001020/**
1021 * link_recv_buf_validate - validate basic format of received message
1022 *
1023 * This routine ensures a TIPC message has an acceptable header, and at least
1024 * as much data as the header indicates it should. The routine also ensures
1025 * that the entire message header is stored in the main fragment of the message
1026 * buffer, to simplify future access to message header fields.
1027 *
1028 * Note: Having extra info present in the message header or data areas is OK.
1029 * TIPC will ignore the excess, under the assumption that it is optional info
1030 * introduced by a later release of the protocol.
1031 */
Allan Stephens85035562008-04-15 19:04:54 -07001032static int link_recv_buf_validate(struct sk_buff *buf)
1033{
1034 static u32 min_data_hdr_size[8] = {
Allan Stephens741d9eb2011-05-31 15:03:18 -04001035 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
Allan Stephens85035562008-04-15 19:04:54 -07001036 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1037 };
1038
1039 struct tipc_msg *msg;
1040 u32 tipc_hdr[2];
1041 u32 size;
1042 u32 hdr_size;
1043 u32 min_hdr_size;
1044
Erik Hugne64380a02014-02-11 11:38:26 +01001045 /* If this packet comes from the defer queue, the skb has already
1046 * been validated
1047 */
1048 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1049 return 1;
1050
Allan Stephens85035562008-04-15 19:04:54 -07001051 if (unlikely(buf->len < MIN_H_SIZE))
1052 return 0;
1053
1054 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1055 if (msg == NULL)
1056 return 0;
1057
1058 if (unlikely(msg_version(msg) != TIPC_VERSION))
1059 return 0;
1060
1061 size = msg_size(msg);
1062 hdr_size = msg_hdr_sz(msg);
1063 min_hdr_size = msg_isdata(msg) ?
1064 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1065
1066 if (unlikely((hdr_size < min_hdr_size) ||
1067 (size < hdr_size) ||
1068 (buf->len < size) ||
1069 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1070 return 0;
1071
1072 return pskb_may_pull(buf, hdr_size);
1073}
1074
Allan Stephensb02b69c2010-08-17 11:00:07 +00001075/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001076 * tipc_rcv - process TIPC packets/messages arriving from off-node
Allan Stephensb02b69c2010-08-17 11:00:07 +00001077 * @head: pointer to message buffer chain
Ying Xue7a2f7d12014-04-21 10:55:46 +08001078 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001079 *
1080 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1081 * structure (i.e. cannot be NULL), but bearer can be inactive.
1082 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001083void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001086 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001087 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 struct sk_buff *crs;
1089 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001090 struct tipc_msg *msg;
1091 u32 seq_no;
1092 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 u32 released = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 head = head->next;
Erik Hugne732256b2014-01-07 15:51:36 -05001096 buf->next = NULL;
Allan Stephens85035562008-04-15 19:04:54 -07001097
1098 /* Ensure message is well-formed */
Allan Stephens85035562008-04-15 19:04:54 -07001099 if (unlikely(!link_recv_buf_validate(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001100 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101
Allan Stephensfe13dda2008-04-15 19:03:23 -07001102 /* Ensure message data is a single contiguous unit */
Allan Stephens5f6d9122011-11-04 13:24:29 -04001103 if (unlikely(skb_linearize(buf)))
Ying Xue3af390e2013-10-30 11:26:57 +08001104 goto discard;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001105
Allan Stephens85035562008-04-15 19:04:54 -07001106 /* Handle arrival of a non-unicast link message */
Allan Stephens85035562008-04-15 19:04:54 -07001107 msg = buf_msg(buf);
1108
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001110 if (msg_user(msg) == LINK_CONFIG)
Ying Xue247f0f32014-02-18 16:06:46 +08001111 tipc_disc_rcv(buf, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001112 else
Ying Xue247f0f32014-02-18 16:06:46 +08001113 tipc_bclink_rcv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 continue;
1115 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001116
Allan Stephensed33a9c2011-04-05 15:15:04 -04001117 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001118 if (unlikely(!msg_short(msg) &&
1119 (msg_destnode(msg) != tipc_own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001120 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001121
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001122 /* Locate neighboring node that sent message */
Per Liden4323add2006-01-18 00:38:21 +01001123 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001125 goto discard;
Per Liden4323add2006-01-18 00:38:21 +01001126 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001127
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001128 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001130 if (unlikely(!l_ptr))
1131 goto unlock_discard;
Allan Stephens85035562008-04-15 19:04:54 -07001132
Allan Stephensb4b56102011-05-27 11:00:51 -04001133 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001134 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001135 msg_user(msg) == LINK_PROTOCOL &&
1136 (msg_type(msg) == RESET_MSG ||
1137 msg_type(msg) == ACTIVATE_MSG) &&
1138 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001139 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001140
Ying Xue10f465c2014-05-05 08:56:11 +08001141 if (tipc_node_blocked(n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001142 goto unlock_discard;
Allan Stephensb4b56102011-05-27 11:00:51 -04001143
Allan Stephens85035562008-04-15 19:04:54 -07001144 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001145 seq_no = msg_seqno(msg);
1146 ackd = msg_ack(msg);
1147
1148 /* Release acked messages */
Ying Xue389dd9b2012-11-16 13:51:30 +08001149 if (n_ptr->bclink.recv_permitted)
Allan Stephens365595912011-10-24 15:26:24 -04001150 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151
1152 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001153 while ((crs != l_ptr->next_out) &&
Allan Stephensf9057302011-10-24 16:03:12 -04001154 less_eq(buf_seqno(crs), ackd)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 struct sk_buff *next = crs->next;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001156 kfree_skb(crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 crs = next;
1158 released++;
1159 }
1160 if (released) {
1161 l_ptr->first_out = crs;
1162 l_ptr->out_queue_size -= released;
1163 }
Allan Stephens85035562008-04-15 19:04:54 -07001164
1165 /* Try sending any messages link endpoint has pending */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001167 tipc_link_push_queue(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001168
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001169 if (released && !skb_queue_empty(&l_ptr->waiting_sks)) {
1170 link_prepare_wakeup(l_ptr);
1171 l_ptr->owner->action_flags |= TIPC_WAKEUP_USERS;
1172 }
Jon Paul Maloya5377832014-02-13 17:29:15 -05001173
Jon Paul Maloya5377832014-02-13 17:29:15 -05001174 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001175 if (unlikely(!link_working_working(l_ptr))) {
1176 if (msg_user(msg) == LINK_PROTOCOL) {
Ying Xue247f0f32014-02-18 16:06:46 +08001177 tipc_link_proto_rcv(l_ptr, buf);
Ying Xue3af390e2013-10-30 11:26:57 +08001178 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001179 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 continue;
1181 }
Ying Xue3af390e2013-10-30 11:26:57 +08001182
1183 /* Traffic message. Conditionally activate link */
1184 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1185
1186 if (link_working_working(l_ptr)) {
1187 /* Re-insert buffer in front of queue */
1188 buf->next = head;
1189 head = buf;
1190 tipc_node_unlock(n_ptr);
1191 continue;
1192 }
1193 goto unlock_discard;
1194 }
1195
1196 /* Link is now in state WORKING_WORKING */
1197 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 link_handle_out_of_seq_msg(l_ptr, buf);
1199 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001200 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 continue;
1202 }
Ying Xue3af390e2013-10-30 11:26:57 +08001203 l_ptr->next_in_no++;
1204 if (unlikely(l_ptr->oldest_deferred_in))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 head = link_insert_deferred_queue(l_ptr, head);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001206
Erik Hugne3f53bd82014-07-01 10:22:41 +02001207 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1208 l_ptr->stats.sent_acks++;
1209 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1210 }
1211
Erik Hugne7ae934b2014-07-01 10:22:40 +02001212 if (tipc_link_prepare_input(l_ptr, &buf)) {
Per Liden4323add2006-01-18 00:38:21 +01001213 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 }
Per Liden4323add2006-01-18 00:38:21 +01001216 tipc_node_unlock(n_ptr);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001217 msg = buf_msg(buf);
1218 if (tipc_link_input(l_ptr, buf) != 0)
1219 goto discard;
Ying Xue3af390e2013-10-30 11:26:57 +08001220 continue;
1221unlock_discard:
Ying Xue3af390e2013-10-30 11:26:57 +08001222 tipc_node_unlock(n_ptr);
1223discard:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001224 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226}
1227
Ben Hutchings2c530402012-07-10 10:55:09 +00001228/**
Erik Hugne7ae934b2014-07-01 10:22:40 +02001229 * tipc_link_prepare_input - process TIPC link messages
1230 *
1231 * returns nonzero if the message was consumed
1232 *
1233 * Node lock must be held
1234 */
1235static int tipc_link_prepare_input(struct tipc_link *l, struct sk_buff **buf)
1236{
1237 struct tipc_node *n;
1238 struct tipc_msg *msg;
1239 int res = -EINVAL;
1240
1241 n = l->owner;
1242 msg = buf_msg(*buf);
1243 switch (msg_user(msg)) {
1244 case CHANGEOVER_PROTOCOL:
1245 if (tipc_link_tunnel_rcv(n, buf))
1246 res = 0;
1247 break;
1248 case MSG_FRAGMENTER:
1249 l->stats.recv_fragments++;
1250 if (tipc_buf_append(&l->reasm_buf, buf)) {
1251 l->stats.recv_fragmented++;
1252 res = 0;
1253 } else if (!l->reasm_buf) {
1254 tipc_link_reset(l);
1255 }
1256 break;
1257 case MSG_BUNDLER:
1258 l->stats.recv_bundles++;
1259 l->stats.recv_bundled += msg_msgcnt(msg);
1260 res = 0;
1261 break;
1262 case NAME_DISTRIBUTOR:
1263 n->bclink.recv_permitted = true;
1264 res = 0;
1265 break;
1266 case BCAST_PROTOCOL:
1267 tipc_link_sync_rcv(n, *buf);
1268 break;
1269 default:
1270 res = 0;
1271 }
1272 return res;
1273}
1274/**
1275 * tipc_link_input - Deliver message too higher layers
1276 */
1277static int tipc_link_input(struct tipc_link *l, struct sk_buff *buf)
1278{
1279 struct tipc_msg *msg = buf_msg(buf);
1280 int res = 0;
1281
1282 switch (msg_user(msg)) {
1283 case TIPC_LOW_IMPORTANCE:
1284 case TIPC_MEDIUM_IMPORTANCE:
1285 case TIPC_HIGH_IMPORTANCE:
1286 case TIPC_CRITICAL_IMPORTANCE:
1287 case CONN_MANAGER:
1288 tipc_sk_rcv(buf);
1289 break;
1290 case NAME_DISTRIBUTOR:
1291 tipc_named_rcv(buf);
1292 break;
1293 case MSG_BUNDLER:
1294 tipc_link_bundle_rcv(buf);
1295 break;
1296 default:
1297 res = -EINVAL;
1298 }
1299 return res;
1300}
1301
1302/**
Allan Stephens8809b252011-10-25 10:44:35 -04001303 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1304 *
1305 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 */
Allan Stephens8809b252011-10-25 10:44:35 -04001307u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
Per Liden4323add2006-01-18 00:38:21 +01001308 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309{
Allan Stephens8809b252011-10-25 10:44:35 -04001310 struct sk_buff *queue_buf;
1311 struct sk_buff **prev;
Allan Stephensf9057302011-10-24 16:03:12 -04001312 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313
1314 buf->next = NULL;
1315
1316 /* Empty queue ? */
1317 if (*head == NULL) {
1318 *head = *tail = buf;
1319 return 1;
1320 }
1321
1322 /* Last ? */
Allan Stephensf9057302011-10-24 16:03:12 -04001323 if (less(buf_seqno(*tail), seq_no)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 (*tail)->next = buf;
1325 *tail = buf;
1326 return 1;
1327 }
1328
Allan Stephens8809b252011-10-25 10:44:35 -04001329 /* Locate insertion point in queue, then insert; discard if duplicate */
1330 prev = head;
1331 queue_buf = *head;
1332 for (;;) {
1333 u32 curr_seqno = buf_seqno(queue_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334
Allan Stephens8809b252011-10-25 10:44:35 -04001335 if (seq_no == curr_seqno) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001336 kfree_skb(buf);
Allan Stephens8809b252011-10-25 10:44:35 -04001337 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 }
Allan Stephens8809b252011-10-25 10:44:35 -04001339
1340 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342
Allan Stephens8809b252011-10-25 10:44:35 -04001343 prev = &queue_buf->next;
1344 queue_buf = queue_buf->next;
1345 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346
Allan Stephens8809b252011-10-25 10:44:35 -04001347 buf->next = queue_buf;
1348 *prev = buf;
1349 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350}
1351
Allan Stephens8809b252011-10-25 10:44:35 -04001352/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1354 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001355static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 struct sk_buff *buf)
1357{
Allan Stephensf9057302011-10-24 16:03:12 -04001358 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359
1360 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Ying Xue247f0f32014-02-18 16:06:46 +08001361 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 return;
1363 }
1364
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 l_ptr->checkpoint--;
1367
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001368 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 * Discard packet if a duplicate; otherwise add it to deferred queue
1370 * and notify peer of gap as per protocol specification
1371 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 if (less(seq_no, mod(l_ptr->next_in_no))) {
1373 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001374 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 return;
1376 }
1377
Per Liden4323add2006-01-18 00:38:21 +01001378 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1379 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 l_ptr->deferred_inqueue_sz++;
1381 l_ptr->stats.deferred_recv++;
Erik Hugne64380a02014-02-11 11:38:26 +01001382 TIPC_SKB_CB(buf)->deferred = true;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001384 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 } else
1386 l_ptr->stats.duplicates++;
1387}
1388
1389/*
1390 * Send protocol message to the other endpoint.
1391 */
Ying Xue247f0f32014-02-18 16:06:46 +08001392void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1393 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001395 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001397 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001398 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399
Ying Xue77a7e072013-12-10 20:45:44 -08001400 /* Don't send protocol message during link changeover */
1401 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001403
1404 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001405 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001406 return;
1407
Allan Stephens92d2c902011-10-25 11:20:26 -04001408 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001410 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001411 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001412 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413
1414 if (msg_typ == STATE_MSG) {
1415 u32 next_sent = mod(l_ptr->next_out_no);
1416
Per Liden4323add2006-01-18 00:38:21 +01001417 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 return;
1419 if (l_ptr->next_out)
Allan Stephensf9057302011-10-24 16:03:12 -04001420 next_sent = buf_seqno(l_ptr->next_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 msg_set_next_sent(msg, next_sent);
1422 if (l_ptr->oldest_deferred_in) {
Allan Stephensf9057302011-10-24 16:03:12 -04001423 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 gap = mod(rec - mod(l_ptr->next_in_no));
1425 }
1426 msg_set_seq_gap(msg, gap);
1427 if (gap)
1428 l_ptr->stats.sent_nacks++;
1429 msg_set_link_tolerance(msg, tolerance);
1430 msg_set_linkprio(msg, priority);
1431 msg_set_max_pkt(msg, ack_mtu);
1432 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1433 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001434 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 u32 mtu = l_ptr->max_pkt;
1436
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001437 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 link_working_working(l_ptr) &&
1439 l_ptr->fsm_msg_cnt) {
1440 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001441 if (l_ptr->max_pkt_probes == 10) {
1442 l_ptr->max_pkt_target = (msg_size - 4);
1443 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001445 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001447 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448
1449 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001450 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 l_ptr->stats.sent_states++;
1452 } else { /* RESET_MSG or ACTIVATE_MSG */
1453 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1454 msg_set_seq_gap(msg, 0);
1455 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001456 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 msg_set_link_tolerance(msg, l_ptr->tolerance);
1458 msg_set_linkprio(msg, l_ptr->priority);
1459 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1460 }
1461
Allan Stephens75f0aa42011-02-28 15:30:20 -05001462 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1463 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001465 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466
1467 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1468
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001469 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 if (!buf)
1471 return;
1472
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001473 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001474 buf->priority = TC_PRIO_CONTROL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475
Ying Xue7a2f7d12014-04-21 10:55:46 +08001476 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
Allan Stephens92d2c902011-10-25 11:20:26 -04001477 l_ptr->unacked_window = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001478 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479}
1480
1481/*
1482 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001483 * Note that network plane id propagates through the network, and may
1484 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485 */
Ying Xue247f0f32014-02-18 16:06:46 +08001486static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487{
1488 u32 rec_gap = 0;
1489 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001490 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491 u32 msg_tol;
1492 struct tipc_msg *msg = buf_msg(buf);
1493
Ying Xue77a7e072013-12-10 20:45:44 -08001494 /* Discard protocol message during link changeover */
1495 if (l_ptr->exp_msg_count)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496 goto exit;
1497
Ying Xue7a2f7d12014-04-21 10:55:46 +08001498 if (l_ptr->net_plane != msg_net_plane(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499 if (tipc_own_addr > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001500 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001503
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001505 if (!link_working_unknown(l_ptr) &&
1506 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001507 if (less_eq(msg_session(msg), l_ptr->peer_session))
1508 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001510
1511 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1512 link_working_unknown(l_ptr))) {
1513 /*
1514 * peer has lost contact -- don't allow peer's links
1515 * to reactivate before we recognize loss & clean up
1516 */
Ying Xueca9cf062014-05-08 08:54:40 +08001517 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001518 }
1519
Allan Stephens47361c82011-10-26 10:55:16 -04001520 link_state_event(l_ptr, RESET_MSG);
1521
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522 /* fall thru' */
1523 case ACTIVATE_MSG:
1524 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1526
Allan Stephens2db99832010-12-31 18:59:33 +00001527 msg_tol = msg_link_tolerance(msg);
1528 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529 link_set_supervision_props(l_ptr, msg_tol);
1530
1531 if (msg_linkprio(msg) > l_ptr->priority)
1532 l_ptr->priority = msg_linkprio(msg);
1533
1534 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001535 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001536 if (max_pkt_info < l_ptr->max_pkt_target)
1537 l_ptr->max_pkt_target = max_pkt_info;
1538 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1539 l_ptr->max_pkt = l_ptr->max_pkt_target;
1540 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001541 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543
Allan Stephens4d753132011-10-25 12:19:05 -04001544 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001545 if (!tipc_node_is_up(l_ptr->owner)) {
1546 l_ptr->owner->bclink.last_sent =
1547 l_ptr->owner->bclink.last_in =
1548 msg_last_bcast(msg);
1549 l_ptr->owner->bclink.oos_state = 0;
1550 }
Allan Stephens4d753132011-10-25 12:19:05 -04001551
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552 l_ptr->peer_session = msg_session(msg);
1553 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001554
1555 if (msg_type(msg) == ACTIVATE_MSG)
1556 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557 break;
1558 case STATE_MSG:
1559
Allan Stephens2db99832010-12-31 18:59:33 +00001560 msg_tol = msg_link_tolerance(msg);
1561 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001563
1564 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001566 pr_warn("%s<%s>, priority change %u->%u\n",
1567 link_rst_msg, l_ptr->name, l_ptr->priority,
1568 msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001570 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571 break;
1572 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001573
1574 /* Record reception; force mismatch at next timeout: */
1575 l_ptr->checkpoint--;
1576
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1578 l_ptr->stats.recv_states++;
1579 if (link_reset_unknown(l_ptr))
1580 break;
1581
1582 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001583 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 mod(l_ptr->next_in_no));
1585 }
1586
1587 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001588 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001589 l_ptr->max_pkt = max_pkt_ack;
1590 l_ptr->max_pkt_probes = 0;
1591 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592
1593 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001594 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001596 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001597 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001598 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599
1600 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001601 if (l_ptr->owner->bclink.recv_permitted)
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001602 tipc_bclink_update_link_state(l_ptr->owner,
1603 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604
1605 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001606 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1607 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001608 }
1609 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01001611 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1612 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 }
1614 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 }
1616exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001617 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618}
1619
1620
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001621/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1622 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001624static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1625 struct tipc_msg *tunnel_hdr,
1626 struct tipc_msg *msg,
1627 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001629 struct tipc_link *tunnel;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 struct sk_buff *buf;
1631 u32 length = msg_size(msg);
1632
1633 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001634 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001635 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001637 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001639 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07001640 if (!buf) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001641 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001643 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001644 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1645 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001646 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647}
1648
1649
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001650/* tipc_link_failover_send_queue(): A link has gone down, but a second
1651 * link is still active. We can do failover. Tunnel the failing link's
1652 * whole send queue via the remaining link. This way, we don't lose
1653 * any packets, and sequence order is preserved for subsequent traffic
1654 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001656void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657{
1658 u32 msgcount = l_ptr->out_queue_size;
1659 struct sk_buff *crs = l_ptr->first_out;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001660 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07001662 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663
1664 if (!tunnel)
1665 return;
1666
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001667 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001668 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1670 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001671
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 if (!l_ptr->first_out) {
1673 struct sk_buff *buf;
1674
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001675 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001677 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001679 __tipc_link_xmit(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001681 pr_warn("%sunable to send changeover msg\n",
1682 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 }
1684 return;
1685 }
Allan Stephensf1310722006-06-25 23:51:37 -07001686
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001687 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001688 l_ptr->owner->active_links[1]);
1689
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 while (crs) {
1691 struct tipc_msg *msg = buf_msg(crs);
1692
1693 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001695 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696
Florian Westphald788d802007-08-02 19:28:06 -07001697 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001699 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001700 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1701 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 pos += align(msg_size(m));
1703 m = (struct tipc_msg *)pos;
1704 }
1705 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001706 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1707 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 }
1709 crs = crs->next;
1710 }
1711}
1712
Ying Xue247f0f32014-02-18 16:06:46 +08001713/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001714 * duplicate of the first link's send queue via the new link. This way, we
1715 * are guaranteed that currently queued packets from a socket are delivered
1716 * before future traffic from the same socket, even if this is using the
1717 * new link. The last arriving copy of each duplicate packet is dropped at
1718 * the receiving end by the regular protocol check, so packet cardinality
1719 * and sequence order is preserved per sender/receiver socket pair.
1720 * Owner node is locked.
1721 */
Ying Xue247f0f32014-02-18 16:06:46 +08001722void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001723 struct tipc_link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724{
1725 struct sk_buff *iter;
1726 struct tipc_msg tunnel_hdr;
1727
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001728 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07001729 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1731 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1732 iter = l_ptr->first_out;
1733 while (iter) {
1734 struct sk_buff *outbuf;
1735 struct tipc_msg *msg = buf_msg(iter);
1736 u32 length = msg_size(msg);
1737
1738 if (msg_user(msg) == MSG_BUNDLER)
1739 msg_set_type(msg, CLOSED_MSG);
1740 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001741 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001743 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 if (outbuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001745 pr_warn("%sunable to send duplicate msg\n",
1746 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 return;
1748 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001749 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1750 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1751 length);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001752 __tipc_link_xmit(tunnel, outbuf);
Per Liden4323add2006-01-18 00:38:21 +01001753 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 return;
1755 iter = iter->next;
1756 }
1757}
1758
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759/**
1760 * buf_extract - extracts embedded TIPC message from another message
1761 * @skb: encapsulating message buffer
1762 * @from_pos: offset to extract from
1763 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001764 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 * encapsulating message itself is left unchanged.
1766 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1768{
1769 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1770 u32 size = msg_size(msg);
1771 struct sk_buff *eb;
1772
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001773 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001775 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 return eb;
1777}
1778
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001779
1780
1781/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1782 * Owner node is locked.
1783 */
1784static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1785 struct sk_buff *t_buf)
1786{
1787 struct sk_buff *buf;
1788
1789 if (!tipc_link_is_up(l_ptr))
1790 return;
1791
1792 buf = buf_extract(t_buf, INT_H_SIZE);
1793 if (buf == NULL) {
1794 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1795 return;
1796 }
1797
1798 /* Add buffer to deferred queue, if applicable: */
1799 link_handle_out_of_seq_msg(l_ptr, buf);
1800}
1801
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001802/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
1803 * Owner node is locked.
1804 */
1805static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
1806 struct sk_buff *t_buf)
1807{
1808 struct tipc_msg *t_msg = buf_msg(t_buf);
1809 struct sk_buff *buf = NULL;
1810 struct tipc_msg *msg;
1811
1812 if (tipc_link_is_up(l_ptr))
1813 tipc_link_reset(l_ptr);
1814
1815 /* First failover packet? */
1816 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1817 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1818
1819 /* Should there be an inner packet? */
1820 if (l_ptr->exp_msg_count) {
1821 l_ptr->exp_msg_count--;
1822 buf = buf_extract(t_buf, INT_H_SIZE);
1823 if (buf == NULL) {
1824 pr_warn("%sno inner failover pkt\n", link_co_err);
1825 goto exit;
1826 }
1827 msg = buf_msg(buf);
1828
1829 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
1830 kfree_skb(buf);
1831 buf = NULL;
1832 goto exit;
1833 }
1834 if (msg_user(msg) == MSG_FRAGMENTER) {
1835 l_ptr->stats.recv_fragments++;
Jon Paul Maloy37e22162014-05-14 05:39:12 -04001836 tipc_buf_append(&l_ptr->reasm_buf, &buf);
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001837 }
1838 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001839exit:
Jon Paul Maloy7d339392014-02-13 17:29:16 -05001840 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
1841 tipc_node_detach_link(l_ptr->owner, l_ptr);
1842 kfree(l_ptr);
1843 }
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001844 return buf;
1845}
1846
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001847/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001848 * via other link as result of a failover (ORIGINAL_MSG) or
1849 * a new active link (DUPLICATE_MSG). Failover packets are
1850 * returned to the active link for delivery upwards.
1851 * Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 */
Jon Paul Maloy3bb53382014-02-13 17:29:12 -05001853static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001854 struct sk_buff **buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855{
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001856 struct sk_buff *t_buf = *buf;
1857 struct tipc_link *l_ptr;
1858 struct tipc_msg *t_msg = buf_msg(t_buf);
1859 u32 bearer_id = msg_bearer_id(t_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001861 *buf = NULL;
1862
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001863 if (bearer_id >= MAX_BEARERS)
1864 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001865
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001866 l_ptr = n_ptr->links[bearer_id];
1867 if (!l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001870 if (msg_type(t_msg) == DUPLICATE_MSG)
1871 tipc_link_dup_rcv(l_ptr, t_buf);
1872 else if (msg_type(t_msg) == ORIGINAL_MSG)
1873 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001874 else
1875 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876exit:
Jon Paul Maloy02842f72014-02-13 17:29:14 -05001877 kfree_skb(t_buf);
Jon Paul Maloy1e9d47a2014-02-13 17:29:13 -05001878 return *buf != NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879}
1880
1881/*
1882 * Bundler functionality:
1883 */
Ying Xue247f0f32014-02-18 16:06:46 +08001884void tipc_link_bundle_rcv(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885{
1886 u32 msgcount = msg_msgcnt(buf_msg(buf));
1887 u32 pos = INT_H_SIZE;
1888 struct sk_buff *obuf;
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001889 struct tipc_msg *omsg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 while (msgcount--) {
1892 obuf = buf_extract(buf, pos);
1893 if (obuf == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001894 pr_warn("Link unable to unbundle message(s)\n");
Allan Stephensa10bd922006-06-25 23:52:17 -07001895 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001896 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001897 omsg = buf_msg(obuf);
1898 pos += align(msg_size(omsg));
Jon Paul Maloy643566d2014-10-17 15:25:28 -04001899 if (msg_isdata(omsg)) {
1900 if (unlikely(msg_type(omsg) == TIPC_MCAST_MSG))
1901 tipc_sk_mcast_rcv(obuf);
1902 else
1903 tipc_sk_rcv(obuf);
1904 } else if (msg_user(omsg) == CONN_MANAGER) {
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001905 tipc_sk_rcv(obuf);
1906 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
1907 tipc_named_rcv(obuf);
1908 } else {
1909 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
1910 kfree_skb(obuf);
1911 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912 }
Allan Stephens5f6d9122011-11-04 13:24:29 -04001913 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914}
1915
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001916static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917{
Allan Stephens5413b4c2011-01-18 13:24:55 -05001918 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
1919 return;
1920
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921 l_ptr->tolerance = tolerance;
1922 l_ptr->continuity_interval =
1923 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
1924 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
1925}
1926
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001927void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928{
1929 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001930 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
1931 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
1932 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
1933 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08001935 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
1936 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
1937 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1938 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1941 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1942 /* FRAGMENT and LAST_FRAGMENT packets */
1943 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1944}
1945
Jon Paul Maloye099e862014-02-13 17:29:18 -05001946/* tipc_link_find_owner - locate owner node of link by link's name
1947 * @name: pointer to link name string
1948 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001949 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001950 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 */
Jon Paul Maloye099e862014-02-13 17:29:18 -05001952static struct tipc_node *tipc_link_find_owner(const char *link_name,
1953 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001955 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001956 struct tipc_node *n_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05001957 struct tipc_node *found_node = 0;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001958 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959
Jon Paul Maloye099e862014-02-13 17:29:18 -05001960 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001961 rcu_read_lock();
1962 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001963 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001964 for (i = 0; i < MAX_BEARERS; i++) {
1965 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001966 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1967 *bearer_id = i;
1968 found_node = n_ptr;
1969 break;
1970 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001971 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001972 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001973 if (found_node)
1974 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001975 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001976 rcu_read_unlock();
1977
Jon Paul Maloye099e862014-02-13 17:29:18 -05001978 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979}
1980
Allan Stephens5c216e12011-10-18 11:34:29 -04001981/**
1982 * link_value_is_valid -- validate proposed link tolerance/priority/window
1983 *
Ben Hutchings2c530402012-07-10 10:55:09 +00001984 * @cmd: value type (TIPC_CMD_SET_LINK_*)
1985 * @new_value: the new value
Allan Stephens5c216e12011-10-18 11:34:29 -04001986 *
1987 * Returns 1 if value is within range, 0 if not.
1988 */
Allan Stephens5c216e12011-10-18 11:34:29 -04001989static int link_value_is_valid(u16 cmd, u32 new_value)
1990{
1991 switch (cmd) {
1992 case TIPC_CMD_SET_LINK_TOL:
1993 return (new_value >= TIPC_MIN_LINK_TOL) &&
1994 (new_value <= TIPC_MAX_LINK_TOL);
1995 case TIPC_CMD_SET_LINK_PRI:
1996 return (new_value <= TIPC_MAX_LINK_PRI);
1997 case TIPC_CMD_SET_LINK_WINDOW:
1998 return (new_value >= TIPC_MIN_LINK_WIN) &&
1999 (new_value <= TIPC_MAX_LINK_WIN);
2000 }
2001 return 0;
2002}
2003
Allan Stephens5c216e12011-10-18 11:34:29 -04002004/**
2005 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
Ben Hutchings2c530402012-07-10 10:55:09 +00002006 * @name: ptr to link, bearer, or media name
2007 * @new_value: new value of link, bearer, or media setting
2008 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
Allan Stephens5c216e12011-10-18 11:34:29 -04002009 *
Ying Xue7216cd92014-04-21 10:55:48 +08002010 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
Allan Stephens5c216e12011-10-18 11:34:29 -04002011 *
2012 * Returns 0 if value updated and negative value on error.
2013 */
Allan Stephens5c216e12011-10-18 11:34:29 -04002014static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2015{
2016 struct tipc_node *node;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002017 struct tipc_link *l_ptr;
Allan Stephens5c216e12011-10-18 11:34:29 -04002018 struct tipc_bearer *b_ptr;
Paul Gortmaker358a0d12011-12-29 20:19:42 -05002019 struct tipc_media *m_ptr;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002020 int bearer_id;
Ying Xue636c0372013-10-18 07:23:20 +02002021 int res = 0;
Allan Stephens5c216e12011-10-18 11:34:29 -04002022
Jon Paul Maloye099e862014-02-13 17:29:18 -05002023 node = tipc_link_find_owner(name, &bearer_id);
2024 if (node) {
Allan Stephens5c216e12011-10-18 11:34:29 -04002025 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002026 l_ptr = node->links[bearer_id];
2027
2028 if (l_ptr) {
2029 switch (cmd) {
2030 case TIPC_CMD_SET_LINK_TOL:
2031 link_set_supervision_props(l_ptr, new_value);
Ying Xue247f0f32014-02-18 16:06:46 +08002032 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2033 new_value, 0, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002034 break;
2035 case TIPC_CMD_SET_LINK_PRI:
2036 l_ptr->priority = new_value;
Ying Xue247f0f32014-02-18 16:06:46 +08002037 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2038 0, new_value, 0);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002039 break;
2040 case TIPC_CMD_SET_LINK_WINDOW:
2041 tipc_link_set_queue_limits(l_ptr, new_value);
2042 break;
2043 default:
2044 res = -EINVAL;
2045 break;
2046 }
Allan Stephens5c216e12011-10-18 11:34:29 -04002047 }
2048 tipc_node_unlock(node);
Ying Xue636c0372013-10-18 07:23:20 +02002049 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002050 }
2051
2052 b_ptr = tipc_bearer_find(name);
2053 if (b_ptr) {
2054 switch (cmd) {
2055 case TIPC_CMD_SET_LINK_TOL:
2056 b_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002057 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002058 case TIPC_CMD_SET_LINK_PRI:
2059 b_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002060 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002061 case TIPC_CMD_SET_LINK_WINDOW:
2062 b_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002063 break;
2064 default:
2065 res = -EINVAL;
2066 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002067 }
Ying Xue636c0372013-10-18 07:23:20 +02002068 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002069 }
2070
2071 m_ptr = tipc_media_find(name);
2072 if (!m_ptr)
2073 return -ENODEV;
2074 switch (cmd) {
2075 case TIPC_CMD_SET_LINK_TOL:
2076 m_ptr->tolerance = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002077 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002078 case TIPC_CMD_SET_LINK_PRI:
2079 m_ptr->priority = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002080 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002081 case TIPC_CMD_SET_LINK_WINDOW:
2082 m_ptr->window = new_value;
Ying Xue636c0372013-10-18 07:23:20 +02002083 break;
2084 default:
2085 res = -EINVAL;
2086 break;
Allan Stephens5c216e12011-10-18 11:34:29 -04002087 }
Ying Xue636c0372013-10-18 07:23:20 +02002088 return res;
Allan Stephens5c216e12011-10-18 11:34:29 -04002089}
2090
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002091struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002092 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002093{
2094 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002095 u32 new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002096 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097
2098 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002099 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100
2101 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2102 new_value = ntohl(args->value);
2103
Allan Stephens5c216e12011-10-18 11:34:29 -04002104 if (!link_value_is_valid(cmd, new_value))
2105 return tipc_cfg_reply_error_string(
2106 "cannot change, value invalid");
2107
Per Liden4323add2006-01-18 00:38:21 +01002108 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002109 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002110 (tipc_bclink_set_queue_limits(new_value) == 0))
2111 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002112 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002113 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114 }
2115
Allan Stephens5c216e12011-10-18 11:34:29 -04002116 res = link_cmd_set_value(args->name, new_value, cmd);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002118 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119
Per Liden4323add2006-01-18 00:38:21 +01002120 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121}
2122
2123/**
2124 * link_reset_statistics - reset link statistics
2125 * @l_ptr: pointer to link
2126 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002127static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128{
2129 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2130 l_ptr->stats.sent_info = l_ptr->next_out_no;
2131 l_ptr->stats.recv_info = l_ptr->next_in_no;
2132}
2133
Per Liden4323add2006-01-18 00:38:21 +01002134struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135{
2136 char *link_name;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002137 struct tipc_link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002138 struct tipc_node *node;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002139 unsigned int bearer_id;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140
2141 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002142 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143
2144 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002145 if (!strcmp(link_name, tipc_bclink_name)) {
2146 if (tipc_bclink_reset_stats())
2147 return tipc_cfg_reply_error_string("link not found");
2148 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 }
Jon Paul Maloye099e862014-02-13 17:29:18 -05002150 node = tipc_link_find_owner(link_name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002151 if (!node)
Per Liden4323add2006-01-18 00:38:21 +01002152 return tipc_cfg_reply_error_string("link not found");
Ying Xue7216cd92014-04-21 10:55:48 +08002153
Jon Paul Maloya11607f2014-02-14 16:40:44 -05002154 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002155 l_ptr = node->links[bearer_id];
2156 if (!l_ptr) {
2157 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002158 return tipc_cfg_reply_error_string("link not found");
2159 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002160 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002161 tipc_node_unlock(node);
Per Liden4323add2006-01-18 00:38:21 +01002162 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002163}
2164
2165/**
2166 * percent - convert count to a percentage of total (rounding up or down)
2167 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168static u32 percent(u32 count, u32 total)
2169{
2170 return (count * 100 + (total / 2)) / total;
2171}
2172
2173/**
Per Liden4323add2006-01-18 00:38:21 +01002174 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175 * @name: link name
2176 * @buf: print buffer area
2177 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002178 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179 * Returns length of print buffer data string (or 0 if error)
2180 */
Per Liden4323add2006-01-18 00:38:21 +01002181static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002182{
Erik Hugnedc1aed32012-06-29 00:50:23 -04002183 struct tipc_link *l;
2184 struct tipc_stats *s;
David S. Miller6c000552008-09-02 23:38:32 -07002185 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002186 char *status;
2187 u32 profile_total = 0;
Jon Paul Maloye099e862014-02-13 17:29:18 -05002188 unsigned int bearer_id;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002189 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002190
Per Liden4323add2006-01-18 00:38:21 +01002191 if (!strcmp(name, tipc_bclink_name))
2192 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193
Jon Paul Maloye099e862014-02-13 17:29:18 -05002194 node = tipc_link_find_owner(name, &bearer_id);
Ying Xue7216cd92014-04-21 10:55:48 +08002195 if (!node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 return 0;
Ying Xue7216cd92014-04-21 10:55:48 +08002197
Per Liden4323add2006-01-18 00:38:21 +01002198 tipc_node_lock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002199
2200 l = node->links[bearer_id];
2201 if (!l) {
2202 tipc_node_unlock(node);
Jon Paul Maloye099e862014-02-13 17:29:18 -05002203 return 0;
2204 }
2205
Erik Hugnedc1aed32012-06-29 00:50:23 -04002206 s = &l->stats;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002207
Erik Hugnedc1aed32012-06-29 00:50:23 -04002208 if (tipc_link_is_active(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002209 status = "ACTIVE";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002210 else if (tipc_link_is_up(l))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002211 status = "STANDBY";
2212 else
2213 status = "DEFUNCT";
Erik Hugnedc1aed32012-06-29 00:50:23 -04002214
2215 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2216 " %s MTU:%u Priority:%u Tolerance:%u ms"
2217 " Window:%u packets\n",
2218 l->name, status, l->max_pkt, l->priority,
2219 l->tolerance, l->queue_limit[0]);
2220
2221 ret += tipc_snprintf(buf + ret, buf_size - ret,
2222 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2223 l->next_in_no - s->recv_info, s->recv_fragments,
2224 s->recv_fragmented, s->recv_bundles,
2225 s->recv_bundled);
2226
2227 ret += tipc_snprintf(buf + ret, buf_size - ret,
2228 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2229 l->next_out_no - s->sent_info, s->sent_fragments,
2230 s->sent_fragmented, s->sent_bundles,
2231 s->sent_bundled);
2232
2233 profile_total = s->msg_length_counts;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002234 if (!profile_total)
2235 profile_total = 1;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002236
2237 ret += tipc_snprintf(buf + ret, buf_size - ret,
2238 " TX profile sample:%u packets average:%u octets\n"
2239 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2240 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2241 s->msg_length_counts,
2242 s->msg_lengths_total / profile_total,
2243 percent(s->msg_length_profile[0], profile_total),
2244 percent(s->msg_length_profile[1], profile_total),
2245 percent(s->msg_length_profile[2], profile_total),
2246 percent(s->msg_length_profile[3], profile_total),
2247 percent(s->msg_length_profile[4], profile_total),
2248 percent(s->msg_length_profile[5], profile_total),
2249 percent(s->msg_length_profile[6], profile_total));
2250
2251 ret += tipc_snprintf(buf + ret, buf_size - ret,
2252 " RX states:%u probes:%u naks:%u defs:%u"
2253 " dups:%u\n", s->recv_states, s->recv_probes,
2254 s->recv_nacks, s->deferred_recv, s->duplicates);
2255
2256 ret += tipc_snprintf(buf + ret, buf_size - ret,
2257 " TX states:%u probes:%u naks:%u acks:%u"
2258 " dups:%u\n", s->sent_states, s->sent_probes,
2259 s->sent_nacks, s->sent_acks, s->retransmitted);
2260
2261 ret += tipc_snprintf(buf + ret, buf_size - ret,
Ying Xue3c294cb2012-11-15 11:34:45 +08002262 " Congestion link:%u Send queue"
2263 " max:%u avg:%u\n", s->link_congs,
Erik Hugnedc1aed32012-06-29 00:50:23 -04002264 s->max_queue_sz, s->queue_sz_counts ?
2265 (s->accu_queue_sz / s->queue_sz_counts) : 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002266
Per Liden4323add2006-01-18 00:38:21 +01002267 tipc_node_unlock(node);
Erik Hugnedc1aed32012-06-29 00:50:23 -04002268 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002269}
2270
Per Liden4323add2006-01-18 00:38:21 +01002271struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002272{
2273 struct sk_buff *buf;
2274 struct tlv_desc *rep_tlv;
2275 int str_len;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002276 int pb_len;
2277 char *pb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002278
2279 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002280 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002281
Erik Hugnedc1aed32012-06-29 00:50:23 -04002282 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002283 if (!buf)
2284 return NULL;
2285
2286 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -04002287 pb = TLV_DATA(rep_tlv);
2288 pb_len = ULTRA_STRING_MAX_LEN;
Per Liden4323add2006-01-18 00:38:21 +01002289 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
Erik Hugnedc1aed32012-06-29 00:50:23 -04002290 pb, pb_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002291 if (!str_len) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002292 kfree_skb(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002293 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002294 }
Erik Hugnedc1aed32012-06-29 00:50:23 -04002295 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002296 skb_put(buf, TLV_SPACE(str_len));
2297 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2298
2299 return buf;
2300}
2301
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302/**
Per Liden4323add2006-01-18 00:38:21 +01002303 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002304 * @dest: network address of destination node
2305 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002306 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307 * If no active link can be found, uses default maximum packet size.
2308 */
Per Liden4323add2006-01-18 00:38:21 +01002309u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002310{
David S. Miller6c000552008-09-02 23:38:32 -07002311 struct tipc_node *n_ptr;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002312 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002313 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002314
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315 if (dest == tipc_own_addr)
2316 return MAX_MSG_SIZE;
2317
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002318 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002319 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002320 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002321 l_ptr = n_ptr->active_links[selector & 1];
2322 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002323 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002324 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002325 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002326 return res;
2327}
2328
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05002329static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002330{
Ying Xue7a2f7d12014-04-21 10:55:46 +08002331 struct tipc_bearer *b_ptr;
2332
2333 rcu_read_lock();
2334 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2335 if (b_ptr)
2336 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2337 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002338
Per Lidenb97bf3f2006-01-02 19:04:38 +01002339 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002340 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002341 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002342 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002343 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002344 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002345 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04002346 pr_cont(":WW\n");
2347 else
2348 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002349}
Richard Alpe0655f6a2014-11-20 10:29:07 +01002350
2351/* Parse and validate nested (link) properties valid for media, bearer and link
2352 */
2353int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
2354{
2355 int err;
2356
2357 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
2358 tipc_nl_prop_policy);
2359 if (err)
2360 return err;
2361
2362 if (props[TIPC_NLA_PROP_PRIO]) {
2363 u32 prio;
2364
2365 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2366 if (prio > TIPC_MAX_LINK_PRI)
2367 return -EINVAL;
2368 }
2369
2370 if (props[TIPC_NLA_PROP_TOL]) {
2371 u32 tol;
2372
2373 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2374 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
2375 return -EINVAL;
2376 }
2377
2378 if (props[TIPC_NLA_PROP_WIN]) {
2379 u32 win;
2380
2381 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2382 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
2383 return -EINVAL;
2384 }
2385
2386 return 0;
2387}
Richard Alpe7be57fc2014-11-20 10:29:12 +01002388
Richard Alpef96ce7a2014-11-20 10:29:13 +01002389int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2390{
2391 int err;
2392 int res = 0;
2393 int bearer_id;
2394 char *name;
2395 struct tipc_link *link;
2396 struct tipc_node *node;
2397 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2398
2399 if (!info->attrs[TIPC_NLA_LINK])
2400 return -EINVAL;
2401
2402 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2403 info->attrs[TIPC_NLA_LINK],
2404 tipc_nl_link_policy);
2405 if (err)
2406 return err;
2407
2408 if (!attrs[TIPC_NLA_LINK_NAME])
2409 return -EINVAL;
2410
2411 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2412
2413 node = tipc_link_find_owner(name, &bearer_id);
2414 if (!node)
2415 return -EINVAL;
2416
2417 tipc_node_lock(node);
2418
2419 link = node->links[bearer_id];
2420 if (!link) {
2421 res = -EINVAL;
2422 goto out;
2423 }
2424
2425 if (attrs[TIPC_NLA_LINK_PROP]) {
2426 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
2427
2428 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
2429 props);
2430 if (err) {
2431 res = err;
2432 goto out;
2433 }
2434
2435 if (props[TIPC_NLA_PROP_TOL]) {
2436 u32 tol;
2437
2438 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2439 link_set_supervision_props(link, tol);
2440 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
2441 }
2442 if (props[TIPC_NLA_PROP_PRIO]) {
2443 u32 prio;
2444
2445 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2446 link->priority = prio;
2447 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
2448 }
2449 if (props[TIPC_NLA_PROP_WIN]) {
2450 u32 win;
2451
2452 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2453 tipc_link_set_queue_limits(link, win);
2454 }
2455 }
2456
2457out:
2458 tipc_node_unlock(node);
2459
2460 return res;
2461}
Richard Alped8182802014-11-24 11:10:29 +01002462
2463static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002464{
2465 int i;
2466 struct nlattr *stats;
2467
2468 struct nla_map {
2469 u32 key;
2470 u32 val;
2471 };
2472
2473 struct nla_map map[] = {
2474 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2475 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2476 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2477 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2478 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2479 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2480 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2481 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2482 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2483 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2484 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2485 s->msg_length_counts : 1},
2486 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2487 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2488 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2489 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2490 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2491 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2492 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2493 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2494 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2495 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2496 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2497 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2498 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2499 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2500 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2501 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2502 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2503 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2504 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2505 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2506 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2507 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2508 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2509 };
2510
2511 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2512 if (!stats)
2513 return -EMSGSIZE;
2514
2515 for (i = 0; i < ARRAY_SIZE(map); i++)
2516 if (nla_put_u32(skb, map[i].key, map[i].val))
2517 goto msg_full;
2518
2519 nla_nest_end(skb, stats);
2520
2521 return 0;
2522msg_full:
2523 nla_nest_cancel(skb, stats);
2524
2525 return -EMSGSIZE;
2526}
2527
2528/* Caller should hold appropriate locks to protect the link */
Richard Alped8182802014-11-24 11:10:29 +01002529static int __tipc_nl_add_link(struct tipc_nl_msg *msg, struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002530{
2531 int err;
2532 void *hdr;
2533 struct nlattr *attrs;
2534 struct nlattr *prop;
2535
2536 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
2537 NLM_F_MULTI, TIPC_NL_LINK_GET);
2538 if (!hdr)
2539 return -EMSGSIZE;
2540
2541 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2542 if (!attrs)
2543 goto msg_full;
2544
2545 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2546 goto attr_msg_full;
2547 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2548 tipc_cluster_mask(tipc_own_addr)))
2549 goto attr_msg_full;
2550 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2551 goto attr_msg_full;
2552 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2553 goto attr_msg_full;
2554 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2555 goto attr_msg_full;
2556
2557 if (tipc_link_is_up(link))
2558 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2559 goto attr_msg_full;
2560 if (tipc_link_is_active(link))
2561 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2562 goto attr_msg_full;
2563
2564 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2565 if (!prop)
2566 goto attr_msg_full;
2567 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2568 goto prop_msg_full;
2569 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2570 goto prop_msg_full;
2571 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2572 link->queue_limit[TIPC_LOW_IMPORTANCE]))
2573 goto prop_msg_full;
2574 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2575 goto prop_msg_full;
2576 nla_nest_end(msg->skb, prop);
2577
2578 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2579 if (err)
2580 goto attr_msg_full;
2581
2582 nla_nest_end(msg->skb, attrs);
2583 genlmsg_end(msg->skb, hdr);
2584
2585 return 0;
2586
2587prop_msg_full:
2588 nla_nest_cancel(msg->skb, prop);
2589attr_msg_full:
2590 nla_nest_cancel(msg->skb, attrs);
2591msg_full:
2592 genlmsg_cancel(msg->skb, hdr);
2593
2594 return -EMSGSIZE;
2595}
2596
2597/* Caller should hold node lock */
Richard Alped8182802014-11-24 11:10:29 +01002598static int __tipc_nl_add_node_links(struct tipc_nl_msg *msg,
2599 struct tipc_node *node,
2600 u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002601{
2602 u32 i;
2603 int err;
2604
2605 for (i = *prev_link; i < MAX_BEARERS; i++) {
2606 *prev_link = i;
2607
2608 if (!node->links[i])
2609 continue;
2610
2611 err = __tipc_nl_add_link(msg, node->links[i]);
2612 if (err)
2613 return err;
2614 }
2615 *prev_link = 0;
2616
2617 return 0;
2618}
2619
2620int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2621{
2622 struct tipc_node *node;
2623 struct tipc_nl_msg msg;
2624 u32 prev_node = cb->args[0];
2625 u32 prev_link = cb->args[1];
2626 int done = cb->args[2];
2627 int err;
2628
2629 if (done)
2630 return 0;
2631
2632 msg.skb = skb;
2633 msg.portid = NETLINK_CB(cb->skb).portid;
2634 msg.seq = cb->nlh->nlmsg_seq;
2635
2636 rcu_read_lock();
2637
2638 if (prev_node) {
2639 node = tipc_node_find(prev_node);
2640 if (!node) {
2641 /* We never set seq or call nl_dump_check_consistent()
2642 * this means that setting prev_seq here will cause the
2643 * consistence check to fail in the netlink callback
2644 * handler. Resulting in the last NLMSG_DONE message
2645 * having the NLM_F_DUMP_INTR flag set.
2646 */
2647 cb->prev_seq = 1;
2648 goto out;
2649 }
2650
2651 list_for_each_entry_continue_rcu(node, &tipc_node_list, list) {
2652 tipc_node_lock(node);
2653 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2654 tipc_node_unlock(node);
2655 if (err)
2656 goto out;
2657
2658 prev_node = node->addr;
2659 }
2660 } else {
2661 err = tipc_nl_add_bc_link(&msg);
2662 if (err)
2663 goto out;
2664
2665 list_for_each_entry_rcu(node, &tipc_node_list, list) {
2666 tipc_node_lock(node);
2667 err = __tipc_nl_add_node_links(&msg, node, &prev_link);
2668 tipc_node_unlock(node);
2669 if (err)
2670 goto out;
2671
2672 prev_node = node->addr;
2673 }
2674 }
2675 done = 1;
2676out:
2677 rcu_read_unlock();
2678
2679 cb->args[0] = prev_node;
2680 cb->args[1] = prev_link;
2681 cb->args[2] = done;
2682
2683 return skb->len;
2684}
2685
2686int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2687{
2688 struct sk_buff *ans_skb;
2689 struct tipc_nl_msg msg;
2690 struct tipc_link *link;
2691 struct tipc_node *node;
2692 char *name;
2693 int bearer_id;
2694 int err;
2695
2696 if (!info->attrs[TIPC_NLA_LINK_NAME])
2697 return -EINVAL;
2698
2699 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
2700 node = tipc_link_find_owner(name, &bearer_id);
2701 if (!node)
2702 return -EINVAL;
2703
2704 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2705 if (!ans_skb)
2706 return -ENOMEM;
2707
2708 msg.skb = ans_skb;
2709 msg.portid = info->snd_portid;
2710 msg.seq = info->snd_seq;
2711
2712 tipc_node_lock(node);
2713 link = node->links[bearer_id];
2714 if (!link) {
2715 err = -EINVAL;
2716 goto err_out;
2717 }
2718
2719 err = __tipc_nl_add_link(&msg, link);
2720 if (err)
2721 goto err_out;
2722
2723 tipc_node_unlock(node);
2724
2725 return genlmsg_reply(ans_skb, info);
2726
2727err_out:
2728 tipc_node_unlock(node);
2729 nlmsg_free(ans_skb);
2730
2731 return err;
2732}
Richard Alpeae363422014-11-20 10:29:14 +01002733
2734int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2735{
2736 int err;
2737 char *link_name;
2738 unsigned int bearer_id;
2739 struct tipc_link *link;
2740 struct tipc_node *node;
2741 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
2742
2743 if (!info->attrs[TIPC_NLA_LINK])
2744 return -EINVAL;
2745
2746 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2747 info->attrs[TIPC_NLA_LINK],
2748 tipc_nl_link_policy);
2749 if (err)
2750 return err;
2751
2752 if (!attrs[TIPC_NLA_LINK_NAME])
2753 return -EINVAL;
2754
2755 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2756
2757 if (strcmp(link_name, tipc_bclink_name) == 0) {
2758 err = tipc_bclink_reset_stats();
2759 if (err)
2760 return err;
2761 return 0;
2762 }
2763
2764 node = tipc_link_find_owner(link_name, &bearer_id);
2765 if (!node)
2766 return -EINVAL;
2767
2768 tipc_node_lock(node);
2769
2770 link = node->links[bearer_id];
2771 if (!link) {
2772 tipc_node_unlock(node);
2773 return -EINVAL;
2774 }
2775
2776 link_reset_statistics(link);
2777
2778 tipc_node_unlock(node);
2779
2780 return 0;
2781}