blob: b1e17953eeea0838ef33209cfc7aa8105c1689a4 [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 Maloyc1336ee2015-03-13 16:08:08 -04004 * Copyright (c) 1996-2007, 2012-2015, 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"
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -040038#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "link.h"
Richard Alpe7be57fc2014-11-20 10:29:12 +010040#include "bcast.h"
Jon Paul Maloy9816f062014-05-14 05:39:15 -040041#include "socket.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#include "discover.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/*
Jon Paul Maloydff29b12015-04-02 09:33:01 -040092 * State value stored in 'failover_pkts'
Per Lidenb97bf3f2006-01-02 19:04:38 +010093 */
Jon Paul Maloydff29b12015-04-02 09:33:01 -040094#define FIRST_FAILOVER 0xffffu
Per Lidenb97bf3f2006-01-02 19:04:38 +010095
Jon Paul Maloyc5898632015-02-05 08:36:36 -050096static void link_handle_out_of_seq_msg(struct tipc_link *link,
97 struct sk_buff *skb);
98static void tipc_link_proto_rcv(struct tipc_link *link,
99 struct sk_buff *skb);
Ying Xue2f55c432015-01-09 15:27:00 +0800100static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500101static void link_state_event(struct tipc_link *l_ptr, u32 event);
102static void link_reset_statistics(struct tipc_link *l_ptr);
103static void link_print(struct tipc_link *l_ptr, const char *str);
Ying Xue247f0f32014-02-18 16:06:46 +0800104static void tipc_link_sync_xmit(struct tipc_link *l);
105static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500106static void tipc_link_input(struct tipc_link *l, struct sk_buff *skb);
107static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb);
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400108static bool tipc_link_failover_rcv(struct tipc_link *l, struct sk_buff **skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800110 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800112static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113{
114 return (i + 3) & ~3u;
115}
116
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500117static void tipc_link_release(struct kref *kref)
118{
119 kfree(container_of(kref, struct tipc_link, ref));
120}
121
122static void tipc_link_get(struct tipc_link *l_ptr)
123{
124 kref_get(&l_ptr->ref);
125}
126
127static void tipc_link_put(struct tipc_link *l_ptr)
128{
129 kref_put(&l_ptr->ref, tipc_link_release);
130}
131
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -0400132static struct tipc_link *tipc_parallel_link(struct tipc_link *l)
133{
134 if (l->owner->active_links[0] != l)
135 return l->owner->active_links[0];
136 return l->owner->active_links[1];
137}
138
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500139static void link_init_max_pkt(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800141 struct tipc_node *node = l_ptr->owner;
142 struct tipc_net *tn = net_generic(node->net, tipc_net_id);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800143 struct tipc_bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900145
Ying Xue7a2f7d12014-04-21 10:55:46 +0800146 rcu_read_lock();
Ying Xue7f9f95d2015-01-09 15:27:06 +0800147 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800148 if (!b_ptr) {
149 rcu_read_unlock();
150 return;
151 }
152 max_pkt = (b_ptr->mtu & ~3);
153 rcu_read_unlock();
154
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 if (max_pkt > MAX_MSG_SIZE)
156 max_pkt = MAX_MSG_SIZE;
157
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900158 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
160 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900161 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 l_ptr->max_pkt = MAX_PKT_DEFAULT;
163
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900164 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165}
166
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800168 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500170int tipc_link_is_up(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171{
172 if (!l_ptr)
173 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000174 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175}
176
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500177int tipc_link_is_active(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000179 return (l_ptr->owner->active_links[0] == l_ptr) ||
180 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181}
182
183/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 * link_timeout - handle expiration of link timer
185 * @l_ptr: pointer to link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 */
Ying Xue2f55c432015-01-09 15:27:00 +0800187static void link_timeout(unsigned long data)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188{
Ying Xue2f55c432015-01-09 15:27:00 +0800189 struct tipc_link *l_ptr = (struct tipc_link *)data;
Ying Xue58dc55f2014-11-26 11:41:52 +0800190 struct sk_buff *skb;
191
Per Liden4323add2006-01-18 00:38:21 +0100192 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193
194 /* update counters used in statistical profiling of send traffic */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400195 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->transmq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 l_ptr->stats.queue_sz_counts++;
197
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400198 skb = skb_peek(&l_ptr->transmq);
Ying Xue58dc55f2014-11-26 11:41:52 +0800199 if (skb) {
200 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 u32 length = msg_size(msg);
202
Joe Perchesf64f9e72009-11-29 16:55:45 -0800203 if ((msg_user(msg) == MSG_FRAGMENTER) &&
204 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 length = msg_size(msg_get_wrapped(msg));
206 }
207 if (length) {
208 l_ptr->stats.msg_lengths_total += length;
209 l_ptr->stats.msg_length_counts++;
210 if (length <= 64)
211 l_ptr->stats.msg_length_profile[0]++;
212 else if (length <= 256)
213 l_ptr->stats.msg_length_profile[1]++;
214 else if (length <= 1024)
215 l_ptr->stats.msg_length_profile[2]++;
216 else if (length <= 4096)
217 l_ptr->stats.msg_length_profile[3]++;
218 else if (length <= 16384)
219 l_ptr->stats.msg_length_profile[4]++;
220 else if (length <= 32768)
221 l_ptr->stats.msg_length_profile[5]++;
222 else
223 l_ptr->stats.msg_length_profile[6]++;
224 }
225 }
226
227 /* do all other link processing performed on a periodic basis */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 link_state_event(l_ptr, TIMEOUT_EVT);
229
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400230 if (skb_queue_len(&l_ptr->backlogq))
Ying Xue47b4c9a82014-11-26 11:41:48 +0800231 tipc_link_push_packets(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232
Per Liden4323add2006-01-18 00:38:21 +0100233 tipc_node_unlock(l_ptr->owner);
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500234 tipc_link_put(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235}
236
Ying Xue2f55c432015-01-09 15:27:00 +0800237static void link_set_timer(struct tipc_link *link, unsigned long time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238{
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500239 if (!mod_timer(&link->timer, jiffies + time))
240 tipc_link_get(link);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241}
242
243/**
Per Liden4323add2006-01-18 00:38:21 +0100244 * tipc_link_create - create a new link
Allan Stephens37b9c082011-02-28 11:32:27 -0500245 * @n_ptr: pointer to associated node
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 * @b_ptr: pointer to associated bearer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900248 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 * Returns pointer to link.
250 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500251struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
Ying Xuec61dd612014-02-13 17:29:09 -0500252 struct tipc_bearer *b_ptr,
253 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254{
Ying Xue34747532015-01-09 15:27:10 +0800255 struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500256 struct tipc_link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 struct tipc_msg *msg;
258 char *if_name;
Allan Stephens37b9c082011-02-28 11:32:27 -0500259 char addr_string[16];
260 u32 peer = n_ptr->addr;
261
Holger Brunck0372bf52014-11-14 18:33:19 +0100262 if (n_ptr->link_cnt >= MAX_BEARERS) {
Allan Stephens37b9c082011-02-28 11:32:27 -0500263 tipc_addr_string_fill(addr_string, n_ptr->addr);
Holger Brunck0372bf52014-11-14 18:33:19 +0100264 pr_err("Attempt to establish %uth link to %s. Max %u allowed.\n",
265 n_ptr->link_cnt, addr_string, MAX_BEARERS);
Allan Stephens37b9c082011-02-28 11:32:27 -0500266 return NULL;
267 }
268
269 if (n_ptr->links[b_ptr->identity]) {
270 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400271 pr_err("Attempt to establish second link on <%s> to %s\n",
272 b_ptr->name, addr_string);
Allan Stephens37b9c082011-02-28 11:32:27 -0500273 return NULL;
274 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700276 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 if (!l_ptr) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400278 pr_warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 return NULL;
280 }
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500281 kref_init(&l_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500283 if_name = strchr(b_ptr->name, ':') + 1;
Allan Stephens062b4c92011-04-07 09:28:47 -0400284 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
Ying Xue34747532015-01-09 15:27:10 +0800285 tipc_zone(tn->own_addr), tipc_cluster(tn->own_addr),
286 tipc_node(tn->own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 if_name,
288 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
Allan Stephens062b4c92011-04-07 09:28:47 -0400289 /* note: peer i/f name is updated by reset/activate message */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Allan Stephens37b9c082011-02-28 11:32:27 -0500291 l_ptr->owner = n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 l_ptr->checkpoint = 1;
Allan Stephensf882cb762011-04-07 09:43:27 -0400293 l_ptr->peer_session = INVALID_SESSION;
Ying Xue7a2f7d12014-04-21 10:55:46 +0800294 l_ptr->bearer_id = b_ptr->identity;
Allan Stephens5c216e12011-10-18 11:34:29 -0400295 link_set_supervision_props(l_ptr, b_ptr->tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 l_ptr->state = RESET_UNKNOWN;
297
298 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
299 msg = l_ptr->pmsg;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500300 tipc_msg_init(tn->own_addr, msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE,
Ying Xue34747532015-01-09 15:27:10 +0800301 l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Ying Xuebafa29e2015-01-09 15:27:12 +0800303 msg_set_session(msg, (tn->random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 msg_set_bearer_id(msg, b_ptr->identity);
305 strcpy((char *)msg_data(msg), if_name);
Ying Xue7a2f7d12014-04-21 10:55:46 +0800306 l_ptr->net_plane = b_ptr->net_plane;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 link_init_max_pkt(l_ptr);
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -0400308 l_ptr->priority = b_ptr->priority;
309 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 l_ptr->next_out_no = 1;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400311 __skb_queue_head_init(&l_ptr->transmq);
312 __skb_queue_head_init(&l_ptr->backlogq);
313 __skb_queue_head_init(&l_ptr->deferdq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500314 skb_queue_head_init(&l_ptr->wakeupq);
315 skb_queue_head_init(&l_ptr->inputq);
316 skb_queue_head_init(&l_ptr->namedq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 link_reset_statistics(l_ptr);
Allan Stephens37b9c082011-02-28 11:32:27 -0500318 tipc_node_attach_link(n_ptr, l_ptr);
Ying Xue2f55c432015-01-09 15:27:00 +0800319 setup_timer(&l_ptr->timer, link_timeout, (unsigned long)l_ptr);
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500320 link_state_event(l_ptr, STARTING_EVT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 return l_ptr;
323}
324
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500325/**
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400326 * tipc_link_delete - Delete a link
327 * @l: link to be deleted
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500328 */
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400329void tipc_link_delete(struct tipc_link *l)
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500330{
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400331 tipc_link_reset(l);
332 if (del_timer(&l->timer))
333 tipc_link_put(l);
334 l->flags |= LINK_STOPPED;
335 /* Delete link now, or when timer is finished: */
336 tipc_link_reset_fragments(l);
337 tipc_node_detach_link(l->owner, l);
338 tipc_link_put(l);
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500339}
340
Ying Xuef2f98002015-01-09 15:27:05 +0800341void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
342 bool shutting_down)
Ying Xue8d8439b2014-02-13 17:29:07 -0500343{
Ying Xuef2f98002015-01-09 15:27:05 +0800344 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500345 struct tipc_link *link;
346 struct tipc_node *node;
Ying Xue8d8439b2014-02-13 17:29:07 -0500347
Ying Xue6c7a7622014-03-27 12:54:37 +0800348 rcu_read_lock();
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500349 list_for_each_entry_rcu(node, &tn->node_list, list) {
350 tipc_node_lock(node);
351 link = node->links[bearer_id];
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400352 if (link)
Jon Paul Maloy2d72d492015-02-03 08:59:17 -0500353 tipc_link_delete(link);
354 tipc_node_unlock(node);
Ying Xue8d8439b2014-02-13 17:29:07 -0500355 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800356 rcu_read_unlock();
Ying Xue8d8439b2014-02-13 17:29:07 -0500357}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
359/**
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400360 * link_schedule_user - schedule a message sender for wakeup after congestion
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400361 * @link: congested link
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400362 * @list: message that was attempted sent
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400363 * Create pseudo msg to send back to user when congestion abates
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400364 * Only consumes message if there is an error
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 */
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400366static int link_schedule_user(struct tipc_link *link, struct sk_buff_head *list)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367{
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400368 struct tipc_msg *msg = buf_msg(skb_peek(list));
369 int imp = msg_importance(msg);
370 u32 oport = msg_origport(msg);
371 u32 addr = link_own_addr(link);
372 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400374 /* This really cannot happen... */
375 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
376 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
377 tipc_link_reset(link);
378 goto err;
379 }
380 /* Non-blocking sender: */
381 if (TIPC_SKB_CB(skb_peek(list))->wakeup_pending)
382 return -ELINKCONG;
383
384 /* Create and schedule wakeup pseudo message */
385 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
386 addr, addr, oport, 0, 0);
387 if (!skb)
388 goto err;
389 TIPC_SKB_CB(skb)->chain_sz = skb_queue_len(list);
390 TIPC_SKB_CB(skb)->chain_imp = imp;
391 skb_queue_tail(&link->wakeupq, skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400392 link->stats.link_congs++;
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400393 return -ELINKCONG;
394err:
395 __skb_queue_purge(list);
396 return -ENOBUFS;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397}
398
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400399/**
400 * link_prepare_wakeup - prepare users for wakeup after congestion
401 * @link: congested link
402 * Move a number of waiting users, as permitted by available space in
403 * the send queue, from link wait queue to node wait queue for wakeup
404 */
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400405void link_prepare_wakeup(struct tipc_link *l)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406{
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400407 int pnd[TIPC_SYSTEM_IMPORTANCE + 1] = {0,};
408 int imp, lim;
Ying Xue58d78b32014-11-26 11:41:51 +0800409 struct sk_buff *skb, *tmp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400411 skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
412 imp = TIPC_SKB_CB(skb)->chain_imp;
413 lim = l->window + l->backlog[imp].limit;
414 pnd[imp] += TIPC_SKB_CB(skb)->chain_sz;
415 if ((pnd[imp] + l->backlog[imp].len) >= lim)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 break;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400417 skb_unlink(skb, &l->wakeupq);
418 skb_queue_tail(&l->inputq, skb);
419 l->owner->inputq = &l->inputq;
420 l->owner->action_flags |= TIPC_MSG_EVT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422}
423
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424/**
Per Liden4323add2006-01-18 00:38:21 +0100425 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 * @l_ptr: pointer to link
427 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500428void tipc_link_reset_fragments(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429{
Jon Paul Maloy37e22162014-05-14 05:39:12 -0400430 kfree_skb(l_ptr->reasm_buf);
431 l_ptr->reasm_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432}
433
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400434static void tipc_link_purge_backlog(struct tipc_link *l)
435{
436 __skb_queue_purge(&l->backlogq);
437 l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
438 l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
439 l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
440 l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
441 l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
442}
443
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900444/**
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500445 * tipc_link_purge_queues - purge all pkt queues associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 * @l_ptr: pointer to link
447 */
Jon Paul Maloy581465f2014-01-07 17:02:44 -0500448void tipc_link_purge_queues(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449{
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400450 __skb_queue_purge(&l_ptr->deferdq);
451 __skb_queue_purge(&l_ptr->transmq);
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400452 tipc_link_purge_backlog(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100453 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454}
455
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500456void tipc_link_reset(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 u32 prev_state = l_ptr->state;
Allan Stephens5392d642006-06-25 23:52:50 -0700459 int was_active_link = tipc_link_is_active(l_ptr);
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400460 struct tipc_node *owner = l_ptr->owner;
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400461 struct tipc_link *pl = tipc_parallel_link(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900462
Allan Stephensa686e682008-06-04 17:29:39 -0700463 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464
Allan Stephensa686e682008-06-04 17:29:39 -0700465 /* Link is down, accept any session */
466 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900468 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900470
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472
473 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
474 return;
475
Per Liden4323add2006-01-18 00:38:21 +0100476 tipc_node_link_down(l_ptr->owner, l_ptr);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800477 tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000478
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400479 if (was_active_link && tipc_node_is_up(l_ptr->owner) && (pl != l_ptr)) {
480 l_ptr->flags |= LINK_FAILINGOVER;
481 l_ptr->failover_checkpt = l_ptr->next_in_no;
482 pl->failover_pkts = FIRST_FAILOVER;
483 pl->failover_checkpt = l_ptr->next_in_no;
484 pl->failover_skb = l_ptr->reasm_buf;
485 } else {
486 kfree_skb(l_ptr->reasm_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500488 /* Clean up all queues, except inputq: */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400489 __skb_queue_purge(&l_ptr->transmq);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400490 __skb_queue_purge(&l_ptr->deferdq);
Jon Paul Maloye6441ba2015-03-09 16:16:22 -0400491 if (!owner->inputq)
492 owner->inputq = &l_ptr->inputq;
493 skb_queue_splice_init(&l_ptr->wakeupq, owner->inputq);
494 if (!skb_queue_empty(owner->inputq))
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500495 owner->action_flags |= TIPC_MSG_EVT;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400496 tipc_link_purge_backlog(l_ptr);
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400497 l_ptr->reasm_buf = NULL;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400498 l_ptr->rcv_unacked = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 l_ptr->checkpoint = 1;
500 l_ptr->next_out_no = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 l_ptr->fsm_msg_cnt = 0;
502 l_ptr->stale_count = 0;
503 link_reset_statistics(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504}
505
Ying Xuef2f98002015-01-09 15:27:05 +0800506void tipc_link_reset_list(struct net *net, unsigned int bearer_id)
Ying Xuee0ca2c32014-02-13 17:29:06 -0500507{
Ying Xuef2f98002015-01-09 15:27:05 +0800508 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500509 struct tipc_link *l_ptr;
Ying Xuec61dd612014-02-13 17:29:09 -0500510 struct tipc_node *n_ptr;
Ying Xuee0ca2c32014-02-13 17:29:06 -0500511
Ying Xue6c7a7622014-03-27 12:54:37 +0800512 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +0800513 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Ying Xue5356f3d2014-05-05 08:56:09 +0800514 tipc_node_lock(n_ptr);
Ying Xuec61dd612014-02-13 17:29:09 -0500515 l_ptr = n_ptr->links[bearer_id];
516 if (l_ptr)
517 tipc_link_reset(l_ptr);
Ying Xue5356f3d2014-05-05 08:56:09 +0800518 tipc_node_unlock(n_ptr);
Ying Xuee0ca2c32014-02-13 17:29:06 -0500519 }
Ying Xue6c7a7622014-03-27 12:54:37 +0800520 rcu_read_unlock();
Ying Xuee0ca2c32014-02-13 17:29:06 -0500521}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522
Ying Xue7f9f95d2015-01-09 15:27:06 +0800523static void link_activate(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524{
Ying Xue7f9f95d2015-01-09 15:27:06 +0800525 struct tipc_node *node = link->owner;
526
527 link->next_in_no = 1;
528 link->stats.recv_info = 1;
529 tipc_node_link_up(node, link);
530 tipc_bearer_add_dest(node->net, link->bearer_id, link->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531}
532
533/**
534 * link_state_event - link finite state machine
535 * @l_ptr: pointer to link
536 * @event: state machine event to process
537 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000538static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500540 struct tipc_link *other;
Ying Xue2f55c432015-01-09 15:27:00 +0800541 unsigned long cont_intv = l_ptr->cont_intv;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542
Jon Paul Maloy7d339392014-02-13 17:29:16 -0500543 if (l_ptr->flags & LINK_STOPPED)
544 return;
545
Ying Xue135daee2014-02-13 17:29:08 -0500546 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 return; /* Not yet. */
548
Jon Paul Maloydff29b12015-04-02 09:33:01 -0400549 if (l_ptr->flags & LINK_FAILINGOVER) {
Allan Stephensa0168922010-12-31 18:59:35 +0000550 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 link_set_timer(l_ptr, cont_intv);
Ying Xue77a7e072013-12-10 20:45:44 -0800552 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554
555 switch (l_ptr->state) {
556 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 switch (event) {
558 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 break;
561 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 if (l_ptr->next_in_no != l_ptr->checkpoint) {
563 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100564 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800565 tipc_link_proto_xmit(l_ptr, STATE_MSG,
566 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 l_ptr->fsm_msg_cnt++;
568 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
Ying Xue247f0f32014-02-18 16:06:46 +0800569 tipc_link_proto_xmit(l_ptr, STATE_MSG,
570 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 l_ptr->fsm_msg_cnt++;
572 }
573 link_set_timer(l_ptr, cont_intv);
574 break;
575 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 l_ptr->state = WORKING_UNKNOWN;
577 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800578 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 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 / 4);
581 break;
582 case RESET_MSG:
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100583 pr_debug("%s<%s>, requested by peer\n",
584 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100585 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 l_ptr->state = RESET_RESET;
587 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800588 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
589 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 l_ptr->fsm_msg_cnt++;
591 link_set_timer(l_ptr, cont_intv);
592 break;
593 default:
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100594 pr_debug("%s%u in WW state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 }
596 break;
597 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 switch (event) {
599 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 l_ptr->state = WORKING_WORKING;
602 l_ptr->fsm_msg_cnt = 0;
603 link_set_timer(l_ptr, cont_intv);
604 break;
605 case RESET_MSG:
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100606 pr_debug("%s<%s>, requested by peer while probing\n",
607 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100608 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 l_ptr->state = RESET_RESET;
610 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800611 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
612 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 l_ptr->fsm_msg_cnt++;
614 link_set_timer(l_ptr, cont_intv);
615 break;
616 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 l_ptr->state = WORKING_WORKING;
619 l_ptr->fsm_msg_cnt = 0;
620 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100621 if (tipc_bclink_acks_missing(l_ptr->owner)) {
Ying Xue247f0f32014-02-18 16:06:46 +0800622 tipc_link_proto_xmit(l_ptr, STATE_MSG,
623 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 l_ptr->fsm_msg_cnt++;
625 }
626 link_set_timer(l_ptr, cont_intv);
627 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
Ying Xue247f0f32014-02-18 16:06:46 +0800628 tipc_link_proto_xmit(l_ptr, STATE_MSG,
629 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 l_ptr->fsm_msg_cnt++;
631 link_set_timer(l_ptr, cont_intv / 4);
632 } else { /* Link has failed */
Erik Hugne3fa9cac2015-01-22 17:10:31 +0100633 pr_debug("%s<%s>, peer not responding\n",
634 link_rst_msg, l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100635 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 l_ptr->state = RESET_UNKNOWN;
637 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800638 tipc_link_proto_xmit(l_ptr, RESET_MSG,
639 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 l_ptr->fsm_msg_cnt++;
641 link_set_timer(l_ptr, cont_intv);
642 }
643 break;
644 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400645 pr_err("%s%u in WU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 }
647 break;
648 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 switch (event) {
650 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 break;
652 case ACTIVATE_MSG:
653 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000654 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 l_ptr->state = WORKING_WORKING;
657 l_ptr->fsm_msg_cnt = 0;
658 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800659 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800661 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800662 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 link_set_timer(l_ptr, cont_intv);
664 break;
665 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 l_ptr->state = RESET_RESET;
667 l_ptr->fsm_msg_cnt = 0;
Ying Xue247f0f32014-02-18 16:06:46 +0800668 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
669 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 l_ptr->fsm_msg_cnt++;
671 link_set_timer(l_ptr, cont_intv);
672 break;
673 case STARTING_EVT:
Ying Xue135daee2014-02-13 17:29:08 -0500674 l_ptr->flags |= LINK_STARTED;
Jon Paul Maloyaf9946f2015-02-03 08:59:20 -0500675 l_ptr->fsm_msg_cnt++;
676 link_set_timer(l_ptr, cont_intv);
677 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800679 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 l_ptr->fsm_msg_cnt++;
681 link_set_timer(l_ptr, cont_intv);
682 break;
683 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400684 pr_err("%s%u in RU state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 }
686 break;
687 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 switch (event) {
689 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 case ACTIVATE_MSG:
691 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000692 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 l_ptr->state = WORKING_WORKING;
695 l_ptr->fsm_msg_cnt = 0;
696 link_activate(l_ptr);
Ying Xue247f0f32014-02-18 16:06:46 +0800697 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698 l_ptr->fsm_msg_cnt++;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800699 if (l_ptr->owner->working_links == 1)
Ying Xue247f0f32014-02-18 16:06:46 +0800700 tipc_link_sync_xmit(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701 link_set_timer(l_ptr, cont_intv);
702 break;
703 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704 break;
705 case TIMEOUT_EVT:
Ying Xue247f0f32014-02-18 16:06:46 +0800706 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
707 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 l_ptr->fsm_msg_cnt++;
709 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 break;
711 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400712 pr_err("%s%u in RR state\n", link_unk_evt, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 }
714 break;
715 default:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400716 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717 }
718}
719
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500720/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400721 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500722 * @link: link to use
Ying Xuea6ca1092014-11-26 11:41:55 +0800723 * @list: chain of buffers containing message
724 *
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400725 * Consumes the buffer chain, except when returning -ELINKCONG,
726 * since the caller then may want to make more send attempts.
727 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
728 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500729 */
Ying Xue7f9f95d2015-01-09 15:27:06 +0800730int __tipc_link_xmit(struct net *net, struct tipc_link *link,
731 struct sk_buff_head *list)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500732{
Ying Xuea6ca1092014-11-26 11:41:55 +0800733 struct tipc_msg *msg = buf_msg(skb_peek(list));
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400734 unsigned int maxwin = link->window;
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -0400735 unsigned int imp = msg_importance(msg);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500736 uint mtu = link->max_pkt;
737 uint ack = mod(link->next_in_no - 1);
738 uint seqno = link->next_out_no;
739 uint bc_last_in = link->owner->bclink.last_in;
740 struct tipc_media_addr *addr = &link->media_addr;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400741 struct sk_buff_head *transmq = &link->transmq;
742 struct sk_buff_head *backlogq = &link->backlogq;
Ying Xuea6ca1092014-11-26 11:41:55 +0800743 struct sk_buff *skb, *tmp;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500744
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400745 /* Match backlog limit against msg importance: */
746 if (unlikely(link->backlog[imp].len >= link->backlog[imp].limit))
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400747 return link_schedule_user(link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500748
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400749 if (unlikely(msg_size(msg) > mtu)) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800750 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500751 return -EMSGSIZE;
752 }
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400753 /* Prepare each packet for sending, and add to relevant queue: */
Ying Xuea6ca1092014-11-26 11:41:55 +0800754 skb_queue_walk_safe(list, skb, tmp) {
755 __skb_unlink(skb, list);
Ying Xue58dc55f2014-11-26 11:41:52 +0800756 msg = buf_msg(skb);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400757 msg_set_seqno(msg, seqno);
758 msg_set_ack(msg, ack);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500759 msg_set_bcast_ack(msg, bc_last_in);
760
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400761 if (likely(skb_queue_len(transmq) < maxwin)) {
762 __skb_queue_tail(transmq, skb);
763 tipc_bearer_send(net, link->bearer_id, skb, addr);
764 link->rcv_unacked = 0;
765 seqno++;
766 continue;
767 }
768 if (tipc_msg_bundle(skb_peek_tail(backlogq), skb, mtu)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500769 link->stats.sent_bundled++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500770 continue;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400771 }
772 if (tipc_msg_make_bundle(&skb, mtu, link->addr)) {
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500773 link->stats.sent_bundled++;
774 link->stats.sent_bundles++;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400775 imp = msg_importance(buf_msg(skb));
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500776 }
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400777 __skb_queue_tail(backlogq, skb);
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400778 link->backlog[imp].len++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500779 seqno++;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500780 }
781 link->next_out_no = seqno;
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500782 return 0;
783}
784
Ying Xuea6ca1092014-11-26 11:41:55 +0800785static void skb2list(struct sk_buff *skb, struct sk_buff_head *list)
786{
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500787 skb_queue_head_init(list);
Ying Xuea6ca1092014-11-26 11:41:55 +0800788 __skb_queue_tail(list, skb);
789}
790
791static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
792{
793 struct sk_buff_head head;
794
795 skb2list(skb, &head);
Ying Xue7f9f95d2015-01-09 15:27:06 +0800796 return __tipc_link_xmit(link->owner->net, link, &head);
Ying Xuea6ca1092014-11-26 11:41:55 +0800797}
798
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400799/* tipc_link_xmit_skb(): send single buffer to destination
800 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
801 * messages, which will not be rejected
802 * The only exception is datagram messages rerouted after secondary
803 * lookup, which are rare and safe to dispose of anyway.
804 * TODO: Return real return value, and let callers use
805 * tipc_wait_for_sendpkt() where applicable
806 */
Ying Xuef2f98002015-01-09 15:27:05 +0800807int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
808 u32 selector)
Ying Xuea6ca1092014-11-26 11:41:55 +0800809{
810 struct sk_buff_head head;
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400811 int rc;
Ying Xuea6ca1092014-11-26 11:41:55 +0800812
813 skb2list(skb, &head);
Jon Paul Maloy3127a022015-03-25 12:07:25 -0400814 rc = tipc_link_xmit(net, &head, dnode, selector);
815 if (rc == -ELINKCONG)
816 kfree_skb(skb);
817 return 0;
Ying Xuea6ca1092014-11-26 11:41:55 +0800818}
819
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500820/**
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400821 * tipc_link_xmit() is the general link level function for message sending
Ying Xuef2f98002015-01-09 15:27:05 +0800822 * @net: the applicable net namespace
Ying Xuea6ca1092014-11-26 11:41:55 +0800823 * @list: chain of buffers containing message
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500824 * @dsz: amount of user data to be sent
825 * @dnode: address of destination node
826 * @selector: a number used for deterministic link selection
827 * Consumes the buffer chain, except when returning -ELINKCONG
828 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
829 */
Ying Xuef2f98002015-01-09 15:27:05 +0800830int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
831 u32 selector)
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500832{
833 struct tipc_link *link = NULL;
834 struct tipc_node *node;
835 int rc = -EHOSTUNREACH;
836
Ying Xuef2f98002015-01-09 15:27:05 +0800837 node = tipc_node_find(net, dnode);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500838 if (node) {
839 tipc_node_lock(node);
840 link = node->active_links[selector & 1];
841 if (link)
Ying Xue7f9f95d2015-01-09 15:27:06 +0800842 rc = __tipc_link_xmit(net, link, list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500843 tipc_node_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +0800844 tipc_node_put(node);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500845 }
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500846 if (link)
847 return rc;
848
Erik Hugne3bd88ee2015-03-19 09:02:18 +0100849 if (likely(in_own_node(net, dnode))) {
850 tipc_sk_rcv(net, list);
851 return 0;
852 }
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500853
Jon Paul Maloyc637c102015-02-05 08:36:41 -0500854 __skb_queue_purge(list);
Jon Paul Maloy4f1688b2014-06-25 20:41:32 -0500855 return rc;
856}
857
Jon Maloyc64f7a62012-11-16 13:51:31 +0800858/*
Ying Xue247f0f32014-02-18 16:06:46 +0800859 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800860 *
861 * Give a newly added peer node the sequence number where it should
862 * start receiving and acking broadcast packets.
863 *
864 * Called with node locked
865 */
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400866static void tipc_link_sync_xmit(struct tipc_link *link)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800867{
Ying Xuea6ca1092014-11-26 11:41:55 +0800868 struct sk_buff *skb;
Jon Maloyc64f7a62012-11-16 13:51:31 +0800869 struct tipc_msg *msg;
870
Ying Xuea6ca1092014-11-26 11:41:55 +0800871 skb = tipc_buf_acquire(INT_H_SIZE);
872 if (!skb)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800873 return;
874
Ying Xuea6ca1092014-11-26 11:41:55 +0800875 msg = buf_msg(skb);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500876 tipc_msg_init(link_own_addr(link), msg, BCAST_PROTOCOL, STATE_MSG,
Ying Xue34747532015-01-09 15:27:10 +0800877 INT_H_SIZE, link->addr);
Jon Paul Maloy25b660c2014-07-16 20:40:59 -0400878 msg_set_last_bcast(msg, link->owner->bclink.acked);
Ying Xuea6ca1092014-11-26 11:41:55 +0800879 __tipc_link_xmit_skb(link, skb);
Jon Maloyc64f7a62012-11-16 13:51:31 +0800880}
881
882/*
Ying Xue247f0f32014-02-18 16:06:46 +0800883 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
Jon Maloyc64f7a62012-11-16 13:51:31 +0800884 * Receive the sequence number where we should start receiving and
885 * acking broadcast packets from a newly added peer node, and open
886 * up for reception of such packets.
887 *
888 * Called with node locked
889 */
Ying Xue247f0f32014-02-18 16:06:46 +0800890static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
Jon Maloyc64f7a62012-11-16 13:51:31 +0800891{
892 struct tipc_msg *msg = buf_msg(buf);
893
894 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
895 n->bclink.recv_permitted = true;
896 kfree_skb(buf);
897}
898
899/*
Ying Xue47b4c9a82014-11-26 11:41:48 +0800900 * tipc_link_push_packets - push unsent packets to bearer
901 *
902 * Push out the unsent messages of a link where congestion
903 * has abated. Node is locked.
904 *
905 * Called with node locked
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400907void tipc_link_push_packets(struct tipc_link *link)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908{
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400909 struct sk_buff *skb;
Ying Xue47b4c9a82014-11-26 11:41:48 +0800910 struct tipc_msg *msg;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400911 unsigned int ack = mod(link->next_in_no - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400913 while (skb_queue_len(&link->transmq) < link->window) {
914 skb = __skb_dequeue(&link->backlogq);
915 if (!skb)
Ying Xue47b4c9a82014-11-26 11:41:48 +0800916 break;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400917 msg = buf_msg(skb);
Jon Paul Maloy1f66d162015-03-25 12:07:24 -0400918 link->backlog[msg_importance(msg)].len--;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -0400919 msg_set_ack(msg, ack);
920 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
921 link->rcv_unacked = 0;
922 __skb_queue_tail(&link->transmq, skb);
923 tipc_bearer_send(link->owner->net, link->bearer_id,
924 skb, &link->media_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926}
927
Ying Xue3f5a12b2014-05-05 08:56:17 +0800928void tipc_link_reset_all(struct tipc_node *node)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700929{
Allan Stephensd356eeb2006-06-25 23:40:01 -0700930 char addr_string[16];
931 u32 i;
932
Ying Xue3f5a12b2014-05-05 08:56:17 +0800933 tipc_node_lock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700934
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400935 pr_warn("Resetting all links to %s\n",
Ying Xue3f5a12b2014-05-05 08:56:17 +0800936 tipc_addr_string_fill(addr_string, node->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -0700937
938 for (i = 0; i < MAX_BEARERS; i++) {
Ying Xue3f5a12b2014-05-05 08:56:17 +0800939 if (node->links[i]) {
940 link_print(node->links[i], "Resetting link\n");
941 tipc_link_reset(node->links[i]);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700942 }
943 }
944
Ying Xue3f5a12b2014-05-05 08:56:17 +0800945 tipc_node_unlock(node);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700946}
947
Paul Gortmakera18c4bc2011-12-29 20:58:42 -0500948static void link_retransmit_failure(struct tipc_link *l_ptr,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400949 struct sk_buff *buf)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700950{
951 struct tipc_msg *msg = buf_msg(buf);
Ying Xue1da46562015-01-09 15:27:07 +0800952 struct net *net = l_ptr->owner->net;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700953
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400954 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700955
956 if (l_ptr->addr) {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700957 /* Handle failure on standard link */
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000958 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -0700959 tipc_link_reset(l_ptr);
960
961 } else {
Allan Stephensd356eeb2006-06-25 23:40:01 -0700962 /* Handle failure on broadcast link */
David S. Miller6c000552008-09-02 23:38:32 -0700963 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700964 char addr_string[16];
965
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400966 pr_info("Msg seq number: %u, ", msg_seqno(msg));
967 pr_cont("Outstanding acks: %lu\n",
968 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -0700969
Ying Xue1da46562015-01-09 15:27:07 +0800970 n_ptr = tipc_bclink_retransmit_to(net);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700971
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000972 tipc_addr_string_fill(addr_string, n_ptr->addr);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400973 pr_info("Broadcast link info for %s\n", addr_string);
Ying Xue389dd9b2012-11-16 13:51:30 +0800974 pr_info("Reception permitted: %d, Acked: %u\n",
975 n_ptr->bclink.recv_permitted,
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400976 n_ptr->bclink.acked);
977 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
978 n_ptr->bclink.last_in,
979 n_ptr->bclink.oos_state,
980 n_ptr->bclink.last_sent);
Allan Stephensd356eeb2006-06-25 23:40:01 -0700981
Ying Xueb952b2b2015-03-26 18:10:23 +0800982 n_ptr->action_flags |= TIPC_BCAST_RESET;
Allan Stephensd356eeb2006-06-25 23:40:01 -0700983 l_ptr->stale_count = 0;
984 }
985}
986
Ying Xue58dc55f2014-11-26 11:41:52 +0800987void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
Per Liden4323add2006-01-18 00:38:21 +0100988 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989{
990 struct tipc_msg *msg;
991
Ying Xue58dc55f2014-11-26 11:41:52 +0800992 if (!skb)
Allan Stephensd356eeb2006-06-25 23:40:01 -0700993 return;
994
Ying Xue58dc55f2014-11-26 11:41:52 +0800995 msg = buf_msg(skb);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900996
Erik Hugne512137e2013-12-06 10:08:00 -0500997 /* Detect repeated retransmit failures */
998 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
999 if (++l_ptr->stale_count > 100) {
Ying Xue58dc55f2014-11-26 11:41:52 +08001000 link_retransmit_failure(l_ptr, skb);
Erik Hugne512137e2013-12-06 10:08:00 -05001001 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001002 }
1003 } else {
Erik Hugne512137e2013-12-06 10:08:00 -05001004 l_ptr->last_retransmitted = msg_seqno(msg);
1005 l_ptr->stale_count = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001007
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001008 skb_queue_walk_from(&l_ptr->transmq, skb) {
1009 if (!retransmits)
Ying Xue58dc55f2014-11-26 11:41:52 +08001010 break;
1011 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001013 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue7f9f95d2015-01-09 15:27:06 +08001014 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, skb,
1015 &l_ptr->media_addr);
Ying Xue3c294cb2012-11-15 11:34:45 +08001016 retransmits--;
1017 l_ptr->stats.retransmitted++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019}
1020
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -04001021/* link_synch(): check if all packets arrived before the synch
1022 * point have been consumed
1023 * Returns true if the parallel links are synched, otherwise false
1024 */
1025static bool link_synch(struct tipc_link *l)
1026{
1027 unsigned int post_synch;
1028 struct tipc_link *pl;
1029
1030 pl = tipc_parallel_link(l);
1031 if (pl == l)
1032 goto synched;
1033
1034 /* Was last pre-synch packet added to input queue ? */
1035 if (less_eq(pl->next_in_no, l->synch_point))
1036 return false;
1037
1038 /* Is it still in the input queue ? */
1039 post_synch = mod(pl->next_in_no - l->synch_point) - 1;
1040 if (skb_queue_len(&pl->inputq) > post_synch)
1041 return false;
1042synched:
1043 l->flags &= ~LINK_SYNCHING;
1044 return true;
1045}
1046
Ying Xuef03273f2014-11-26 11:41:54 +08001047static void link_retrieve_defq(struct tipc_link *link,
1048 struct sk_buff_head *list)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049{
1050 u32 seq_no;
1051
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001052 if (skb_queue_empty(&link->deferdq))
Ying Xuef03273f2014-11-26 11:41:54 +08001053 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001055 seq_no = buf_seqno(skb_peek(&link->deferdq));
Ying Xuef03273f2014-11-26 11:41:54 +08001056 if (seq_no == mod(link->next_in_no))
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001057 skb_queue_splice_tail_init(&link->deferdq, list);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058}
1059
Allan Stephens85035562008-04-15 19:04:54 -07001060/**
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001061 * tipc_rcv - process TIPC packets/messages arriving from off-node
Ying Xuef2f98002015-01-09 15:27:05 +08001062 * @net: the applicable net namespace
Ying Xuef03273f2014-11-26 11:41:54 +08001063 * @skb: TIPC packet
Ying Xue7a2f7d12014-04-21 10:55:46 +08001064 * @b_ptr: pointer to bearer message arrived on
Allan Stephensb02b69c2010-08-17 11:00:07 +00001065 *
1066 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1067 * structure (i.e. cannot be NULL), but bearer can be inactive.
1068 */
Ying Xuec93d3ba2015-01-09 15:27:04 +08001069void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070{
Ying Xue34747532015-01-09 15:27:10 +08001071 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuef03273f2014-11-26 11:41:54 +08001072 struct sk_buff_head head;
1073 struct tipc_node *n_ptr;
1074 struct tipc_link *l_ptr;
1075 struct sk_buff *skb1, *tmp;
1076 struct tipc_msg *msg;
1077 u32 seq_no;
1078 u32 ackd;
1079 u32 released;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080
Ying Xuea6ca1092014-11-26 11:41:55 +08001081 skb2list(skb, &head);
Allan Stephens85035562008-04-15 19:04:54 -07001082
Ying Xuef03273f2014-11-26 11:41:54 +08001083 while ((skb = __skb_dequeue(&head))) {
Allan Stephens85035562008-04-15 19:04:54 -07001084 /* Ensure message is well-formed */
Jon Paul Maloycf2157f2015-03-13 16:08:06 -04001085 if (unlikely(!tipc_msg_validate(skb)))
Ying Xue3af390e2013-10-30 11:26:57 +08001086 goto discard;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087
Allan Stephens85035562008-04-15 19:04:54 -07001088 /* Handle arrival of a non-unicast link message */
Ying Xuef03273f2014-11-26 11:41:54 +08001089 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001091 if (msg_user(msg) == LINK_CONFIG)
Ying Xuec93d3ba2015-01-09 15:27:04 +08001092 tipc_disc_rcv(net, skb, b_ptr);
Allan Stephens1265a022008-06-04 17:32:35 -07001093 else
Ying Xuec93d3ba2015-01-09 15:27:04 +08001094 tipc_bclink_rcv(net, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 continue;
1096 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001097
Allan Stephensed33a9c2011-04-05 15:15:04 -04001098 /* Discard unicast link messages destined for another node */
Allan Stephens26008242006-06-25 23:39:31 -07001099 if (unlikely(!msg_short(msg) &&
Ying Xue34747532015-01-09 15:27:10 +08001100 (msg_destnode(msg) != tn->own_addr)))
Ying Xue3af390e2013-10-30 11:26:57 +08001101 goto discard;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001102
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001103 /* Locate neighboring node that sent message */
Ying Xuef2f98002015-01-09 15:27:05 +08001104 n_ptr = tipc_node_find(net, msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 if (unlikely(!n_ptr))
Ying Xue3af390e2013-10-30 11:26:57 +08001106 goto discard;
Allan Stephens85035562008-04-15 19:04:54 -07001107
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001108 tipc_node_lock(n_ptr);
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001109 /* Locate unicast link endpoint that should handle message */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 l_ptr = n_ptr->links[b_ptr->identity];
Ying Xue3af390e2013-10-30 11:26:57 +08001111 if (unlikely(!l_ptr))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001112 goto unlock;
Allan Stephens85035562008-04-15 19:04:54 -07001113
Allan Stephensb4b56102011-05-27 11:00:51 -04001114 /* Verify that communication with node is currently allowed */
Ying Xueaecb9bb2014-05-08 08:54:39 +08001115 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
Ying Xue10f465c2014-05-05 08:56:11 +08001116 msg_user(msg) == LINK_PROTOCOL &&
1117 (msg_type(msg) == RESET_MSG ||
1118 msg_type(msg) == ACTIVATE_MSG) &&
1119 !msg_redundant_link(msg))
Ying Xueaecb9bb2014-05-08 08:54:39 +08001120 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001121
Ying Xue10f465c2014-05-05 08:56:11 +08001122 if (tipc_node_blocked(n_ptr))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001123 goto unlock;
Allan Stephensb4b56102011-05-27 11:00:51 -04001124
Allan Stephens85035562008-04-15 19:04:54 -07001125 /* Validate message sequence number info */
Allan Stephens85035562008-04-15 19:04:54 -07001126 seq_no = msg_seqno(msg);
1127 ackd = msg_ack(msg);
1128
1129 /* Release acked messages */
Jon Paul Maloy2cdf3912015-03-13 16:08:09 -04001130 if (unlikely(n_ptr->bclink.acked != msg_bcast_ack(msg)))
Allan Stephens365595912011-10-24 15:26:24 -04001131 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132
Ying Xue58dc55f2014-11-26 11:41:52 +08001133 released = 0;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001134 skb_queue_walk_safe(&l_ptr->transmq, skb1, tmp) {
1135 if (more(buf_seqno(skb1), ackd))
Ying Xue58dc55f2014-11-26 11:41:52 +08001136 break;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001137 __skb_unlink(skb1, &l_ptr->transmq);
Ying Xue58dc55f2014-11-26 11:41:52 +08001138 kfree_skb(skb1);
1139 released = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 }
Allan Stephens85035562008-04-15 19:04:54 -07001141
1142 /* Try sending any messages link endpoint has pending */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001143 if (unlikely(skb_queue_len(&l_ptr->backlogq)))
Ying Xue47b4c9a82014-11-26 11:41:48 +08001144 tipc_link_push_packets(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001145
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001146 if (released && !skb_queue_empty(&l_ptr->wakeupq))
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001147 link_prepare_wakeup(l_ptr);
Jon Paul Maloya5377832014-02-13 17:29:15 -05001148
Jon Paul Maloya5377832014-02-13 17:29:15 -05001149 /* Process the incoming packet */
Ying Xue3af390e2013-10-30 11:26:57 +08001150 if (unlikely(!link_working_working(l_ptr))) {
1151 if (msg_user(msg) == LINK_PROTOCOL) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001152 tipc_link_proto_rcv(l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001153 link_retrieve_defq(l_ptr, &head);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001154 skb = NULL;
1155 goto unlock;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 }
Ying Xue3af390e2013-10-30 11:26:57 +08001157
1158 /* Traffic message. Conditionally activate link */
1159 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1160
1161 if (link_working_working(l_ptr)) {
1162 /* Re-insert buffer in front of queue */
Ying Xuef03273f2014-11-26 11:41:54 +08001163 __skb_queue_head(&head, skb);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001164 skb = NULL;
1165 goto unlock;
Ying Xue3af390e2013-10-30 11:26:57 +08001166 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001167 goto unlock;
Ying Xue3af390e2013-10-30 11:26:57 +08001168 }
1169
1170 /* Link is now in state WORKING_WORKING */
1171 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001172 link_handle_out_of_seq_msg(l_ptr, skb);
Ying Xuef03273f2014-11-26 11:41:54 +08001173 link_retrieve_defq(l_ptr, &head);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001174 skb = NULL;
1175 goto unlock;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 }
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -04001177 /* Synchronize with parallel link if applicable */
1178 if (unlikely((l_ptr->flags & LINK_SYNCHING) && !msg_dup(msg))) {
1179 link_handle_out_of_seq_msg(l_ptr, skb);
1180 if (link_synch(l_ptr))
1181 link_retrieve_defq(l_ptr, &head);
1182 skb = NULL;
1183 goto unlock;
1184 }
Ying Xue3af390e2013-10-30 11:26:57 +08001185 l_ptr->next_in_no++;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001186 if (unlikely(!skb_queue_empty(&l_ptr->deferdq)))
Ying Xuef03273f2014-11-26 11:41:54 +08001187 link_retrieve_defq(l_ptr, &head);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001188 if (unlikely(++l_ptr->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
Erik Hugne3f53bd82014-07-01 10:22:41 +02001189 l_ptr->stats.sent_acks++;
1190 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1191 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001192 tipc_link_input(l_ptr, skb);
1193 skb = NULL;
1194unlock:
Ying Xue3af390e2013-10-30 11:26:57 +08001195 tipc_node_unlock(n_ptr);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08001196 tipc_node_put(n_ptr);
Ying Xue3af390e2013-10-30 11:26:57 +08001197discard:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001198 if (unlikely(skb))
1199 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201}
1202
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001203/* tipc_data_input - deliver data and name distr msgs to upper layer
Erik Hugne7ae934b2014-07-01 10:22:40 +02001204 *
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001205 * Consumes buffer if message is of right type
Erik Hugne7ae934b2014-07-01 10:22:40 +02001206 * Node lock must be held
1207 */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001208static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb)
Erik Hugne7ae934b2014-07-01 10:22:40 +02001209{
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001210 struct tipc_node *node = link->owner;
1211 struct tipc_msg *msg = buf_msg(skb);
1212 u32 dport = msg_destport(msg);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001213
1214 switch (msg_user(msg)) {
1215 case TIPC_LOW_IMPORTANCE:
1216 case TIPC_MEDIUM_IMPORTANCE:
1217 case TIPC_HIGH_IMPORTANCE:
1218 case TIPC_CRITICAL_IMPORTANCE:
1219 case CONN_MANAGER:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001220 if (tipc_skb_queue_tail(&link->inputq, skb, dport)) {
1221 node->inputq = &link->inputq;
1222 node->action_flags |= TIPC_MSG_EVT;
1223 }
1224 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001225 case NAME_DISTRIBUTOR:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001226 node->bclink.recv_permitted = true;
1227 node->namedq = &link->namedq;
1228 skb_queue_tail(&link->namedq, skb);
1229 if (skb_queue_len(&link->namedq) == 1)
1230 node->action_flags |= TIPC_NAMED_MSG_EVT;
1231 return true;
Erik Hugne7ae934b2014-07-01 10:22:40 +02001232 case MSG_BUNDLER:
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001233 case TUNNEL_PROTOCOL:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001234 case MSG_FRAGMENTER:
1235 case BCAST_PROTOCOL:
1236 return false;
1237 default:
1238 pr_warn("Dropping received illegal msg type\n");
1239 kfree_skb(skb);
1240 return false;
1241 };
1242}
1243
1244/* tipc_link_input - process packet that has passed link protocol check
1245 *
1246 * Consumes buffer
1247 * Node lock must be held
1248 */
1249static void tipc_link_input(struct tipc_link *link, struct sk_buff *skb)
1250{
1251 struct tipc_node *node = link->owner;
1252 struct tipc_msg *msg = buf_msg(skb);
1253 struct sk_buff *iskb;
1254 int pos = 0;
1255
1256 if (likely(tipc_data_input(link, skb)))
1257 return;
1258
1259 switch (msg_user(msg)) {
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001260 case TUNNEL_PROTOCOL:
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -04001261 if (msg_dup(msg)) {
1262 link->flags |= LINK_SYNCHING;
1263 link->synch_point = msg_seqno(msg_get_wrapped(msg));
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001264 kfree_skb(skb);
1265 break;
Jon Paul Maloy8b4ed862015-03-25 12:07:26 -04001266 }
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001267 if (!tipc_link_failover_rcv(link, &skb))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001268 break;
1269 if (msg_user(buf_msg(skb)) != MSG_BUNDLER) {
1270 tipc_data_input(link, skb);
1271 break;
1272 }
1273 case MSG_BUNDLER:
1274 link->stats.recv_bundles++;
1275 link->stats.recv_bundled += msg_msgcnt(msg);
1276
1277 while (tipc_msg_extract(skb, &iskb, &pos))
1278 tipc_data_input(link, iskb);
1279 break;
1280 case MSG_FRAGMENTER:
1281 link->stats.recv_fragments++;
1282 if (tipc_buf_append(&link->reasm_buf, &skb)) {
1283 link->stats.recv_fragmented++;
1284 tipc_data_input(link, skb);
1285 } else if (!link->reasm_buf) {
1286 tipc_link_reset(link);
1287 }
1288 break;
1289 case BCAST_PROTOCOL:
1290 tipc_link_sync_rcv(node, skb);
Erik Hugne7ae934b2014-07-01 10:22:40 +02001291 break;
1292 default:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001293 break;
1294 };
Erik Hugne7ae934b2014-07-01 10:22:40 +02001295}
1296
1297/**
Allan Stephens8809b252011-10-25 10:44:35 -04001298 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1299 *
1300 * Returns increase in queue length (i.e. 0 or 1)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301 */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001302u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303{
Ying Xuebc6fecd2014-11-26 11:41:53 +08001304 struct sk_buff *skb1;
1305 u32 seq_no = buf_seqno(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306
1307 /* Empty queue ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001308 if (skb_queue_empty(list)) {
1309 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 return 1;
1311 }
1312
1313 /* Last ? */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001314 if (less(buf_seqno(skb_peek_tail(list)), seq_no)) {
1315 __skb_queue_tail(list, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 return 1;
1317 }
1318
Allan Stephens8809b252011-10-25 10:44:35 -04001319 /* Locate insertion point in queue, then insert; discard if duplicate */
Ying Xuebc6fecd2014-11-26 11:41:53 +08001320 skb_queue_walk(list, skb1) {
1321 u32 curr_seqno = buf_seqno(skb1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322
Allan Stephens8809b252011-10-25 10:44:35 -04001323 if (seq_no == curr_seqno) {
Ying Xuebc6fecd2014-11-26 11:41:53 +08001324 kfree_skb(skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001325 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 }
Allan Stephens8809b252011-10-25 10:44:35 -04001327
1328 if (less(seq_no, curr_seqno))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 break;
Allan Stephens8809b252011-10-25 10:44:35 -04001330 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331
Ying Xuebc6fecd2014-11-26 11:41:53 +08001332 __skb_queue_before(list, skb1, skb);
Allan Stephens8809b252011-10-25 10:44:35 -04001333 return 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334}
1335
Allan Stephens8809b252011-10-25 10:44:35 -04001336/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1338 */
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001339static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 struct sk_buff *buf)
1341{
Allan Stephensf9057302011-10-24 16:03:12 -04001342 u32 seq_no = buf_seqno(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343
1344 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001345 tipc_link_proto_rcv(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 return;
1347 }
1348
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 /* Record OOS packet arrival (force mismatch on next timeout) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 l_ptr->checkpoint--;
1351
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001352 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 * Discard packet if a duplicate; otherwise add it to deferred queue
1354 * and notify peer of gap as per protocol specification
1355 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 if (less(seq_no, mod(l_ptr->next_in_no))) {
1357 l_ptr->stats.duplicates++;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001358 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 return;
1360 }
1361
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001362 if (tipc_link_defer_pkt(&l_ptr->deferdq, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 l_ptr->stats.deferred_recv++;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001364 if ((skb_queue_len(&l_ptr->deferdq) % TIPC_MIN_LINK_WIN) == 1)
Ying Xue247f0f32014-02-18 16:06:46 +08001365 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Ying Xuebc6fecd2014-11-26 11:41:53 +08001366 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 l_ptr->stats.duplicates++;
Ying Xuebc6fecd2014-11-26 11:41:53 +08001368 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369}
1370
1371/*
1372 * Send protocol message to the other endpoint.
1373 */
Ying Xue247f0f32014-02-18 16:06:46 +08001374void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1375 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001377 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001379 u32 msg_size = sizeof(l_ptr->proto_msg);
Allan Stephens75f0aa42011-02-28 15:30:20 -05001380 int r_flag;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001382 /* Don't send protocol message during link failover */
1383 if (l_ptr->flags & LINK_FAILINGOVER)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 return;
Allan Stephensb4b56102011-05-27 11:00:51 -04001385
1386 /* Abort non-RESET send if communication with node is prohibited */
Ying Xue10f465c2014-05-05 08:56:11 +08001387 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
Allan Stephensb4b56102011-05-27 11:00:51 -04001388 return;
1389
Allan Stephens92d2c902011-10-25 11:20:26 -04001390 /* Create protocol message with "out-of-sequence" sequence number */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 msg_set_type(msg, msg_typ);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001392 msg_set_net_plane(msg, l_ptr->net_plane);
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001393 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Ying Xue1da46562015-01-09 15:27:07 +08001394 msg_set_last_bcast(msg, tipc_bclink_get_last_sent(l_ptr->owner->net));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395
1396 if (msg_typ == STATE_MSG) {
1397 u32 next_sent = mod(l_ptr->next_out_no);
1398
Per Liden4323add2006-01-18 00:38:21 +01001399 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 return;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001401 if (skb_queue_len(&l_ptr->backlogq))
1402 next_sent = buf_seqno(skb_peek(&l_ptr->backlogq));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 msg_set_next_sent(msg, next_sent);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001404 if (!skb_queue_empty(&l_ptr->deferdq)) {
1405 u32 rec = buf_seqno(skb_peek(&l_ptr->deferdq));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 gap = mod(rec - mod(l_ptr->next_in_no));
1407 }
1408 msg_set_seq_gap(msg, gap);
1409 if (gap)
1410 l_ptr->stats.sent_nacks++;
1411 msg_set_link_tolerance(msg, tolerance);
1412 msg_set_linkprio(msg, priority);
1413 msg_set_max_pkt(msg, ack_mtu);
1414 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1415 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001416 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 u32 mtu = l_ptr->max_pkt;
1418
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001419 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 link_working_working(l_ptr) &&
1421 l_ptr->fsm_msg_cnt) {
1422 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001423 if (l_ptr->max_pkt_probes == 10) {
1424 l_ptr->max_pkt_target = (msg_size - 4);
1425 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001427 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001429 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430
1431 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001432 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 l_ptr->stats.sent_states++;
1434 } else { /* RESET_MSG or ACTIVATE_MSG */
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001435 msg_set_ack(msg, mod(l_ptr->failover_checkpt - 1));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 msg_set_seq_gap(msg, 0);
1437 msg_set_next_sent(msg, 1);
Allan Stephensf23d9bf2011-01-18 15:15:34 -05001438 msg_set_probe(msg, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 msg_set_link_tolerance(msg, l_ptr->tolerance);
1440 msg_set_linkprio(msg, l_ptr->priority);
1441 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1442 }
1443
Allan Stephens75f0aa42011-02-28 15:30:20 -05001444 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1445 msg_set_redundant_link(msg, r_flag);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 msg_set_linkprio(msg, l_ptr->priority);
Allan Stephens92d2c902011-10-25 11:20:26 -04001447 msg_set_size(msg, msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448
1449 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1450
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001451 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 if (!buf)
1453 return;
1454
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001455 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Ying Xue796c75d2013-06-17 10:54:48 -04001456 buf->priority = TC_PRIO_CONTROL;
Ying Xue7f9f95d2015-01-09 15:27:06 +08001457 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
1458 &l_ptr->media_addr);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001459 l_ptr->rcv_unacked = 0;
Allan Stephens5f6d9122011-11-04 13:24:29 -04001460 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461}
1462
1463/*
1464 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001465 * Note that network plane id propagates through the network, and may
1466 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 */
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001468static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
Ying Xuec93d3ba2015-01-09 15:27:04 +08001469 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470{
1471 u32 rec_gap = 0;
1472 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001473 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 u32 msg_tol;
1475 struct tipc_msg *msg = buf_msg(buf);
1476
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001477 if (l_ptr->flags & LINK_FAILINGOVER)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 goto exit;
1479
Ying Xue7a2f7d12014-04-21 10:55:46 +08001480 if (l_ptr->net_plane != msg_net_plane(msg))
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001481 if (link_own_addr(l_ptr) > msg_prevnode(msg))
Ying Xue7a2f7d12014-04-21 10:55:46 +08001482 l_ptr->net_plane = msg_net_plane(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001485
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07001487 if (!link_working_unknown(l_ptr) &&
1488 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephens641c2182011-04-07 09:54:43 -04001489 if (less_eq(msg_session(msg), l_ptr->peer_session))
1490 break; /* duplicate or old reset: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491 }
Allan Stephensb4b56102011-05-27 11:00:51 -04001492
1493 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1494 link_working_unknown(l_ptr))) {
1495 /*
1496 * peer has lost contact -- don't allow peer's links
1497 * to reactivate before we recognize loss & clean up
1498 */
Ying Xueca9cf062014-05-08 08:54:40 +08001499 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
Allan Stephensb4b56102011-05-27 11:00:51 -04001500 }
1501
Allan Stephens47361c82011-10-26 10:55:16 -04001502 link_state_event(l_ptr, RESET_MSG);
1503
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 /* fall thru' */
1505 case ACTIVATE_MSG:
1506 /* Update link settings according other endpoint's values */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1508
Allan Stephens2db99832010-12-31 18:59:33 +00001509 msg_tol = msg_link_tolerance(msg);
1510 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 link_set_supervision_props(l_ptr, msg_tol);
1512
1513 if (msg_linkprio(msg) > l_ptr->priority)
1514 l_ptr->priority = msg_linkprio(msg);
1515
1516 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001517 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 if (max_pkt_info < l_ptr->max_pkt_target)
1519 l_ptr->max_pkt_target = max_pkt_info;
1520 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1521 l_ptr->max_pkt = l_ptr->max_pkt_target;
1522 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001523 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525
Allan Stephens4d753132011-10-25 12:19:05 -04001526 /* Synchronize broadcast link info, if not done previously */
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001527 if (!tipc_node_is_up(l_ptr->owner)) {
1528 l_ptr->owner->bclink.last_sent =
1529 l_ptr->owner->bclink.last_in =
1530 msg_last_bcast(msg);
1531 l_ptr->owner->bclink.oos_state = 0;
1532 }
Allan Stephens4d753132011-10-25 12:19:05 -04001533
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 l_ptr->peer_session = msg_session(msg);
1535 l_ptr->peer_bearer_id = msg_bearer_id(msg);
Allan Stephens47361c82011-10-26 10:55:16 -04001536
1537 if (msg_type(msg) == ACTIVATE_MSG)
1538 link_state_event(l_ptr, ACTIVATE_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539 break;
1540 case STATE_MSG:
1541
Allan Stephens2db99832010-12-31 18:59:33 +00001542 msg_tol = msg_link_tolerance(msg);
1543 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001545
1546 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001547 (msg_linkprio(msg) != l_ptr->priority)) {
Erik Hugne3fa9cac2015-01-22 17:10:31 +01001548 pr_debug("%s<%s>, priority change %u->%u\n",
1549 link_rst_msg, l_ptr->name,
1550 l_ptr->priority, msg_linkprio(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01001552 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 break;
1554 }
Jon Paul Maloyec37dcd2014-05-14 05:39:10 -04001555
1556 /* Record reception; force mismatch at next timeout: */
1557 l_ptr->checkpoint--;
1558
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1560 l_ptr->stats.recv_states++;
1561 if (link_reset_unknown(l_ptr))
1562 break;
1563
1564 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001565 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566 mod(l_ptr->next_in_no));
1567 }
1568
1569 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001570 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001571 l_ptr->max_pkt = max_pkt_ack;
1572 l_ptr->max_pkt_probes = 0;
1573 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574
1575 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001576 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00001578 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001579 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001580 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581
1582 /* Protocol message before retransmits, reduce loss risk */
Ying Xue389dd9b2012-11-16 13:51:30 +08001583 if (l_ptr->owner->bclink.recv_permitted)
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001584 tipc_bclink_update_link_state(l_ptr->owner,
Allan Stephens7a54d4a2011-10-27 14:17:53 -04001585 msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586
1587 if (rec_gap || (msg_probe(msg))) {
Ying Xue247f0f32014-02-18 16:06:46 +08001588 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1589 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 }
1591 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001592 l_ptr->stats.recv_nacks++;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001593 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->transmq),
Per Liden4323add2006-01-18 00:38:21 +01001594 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 }
1596 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597 }
1598exit:
Allan Stephens5f6d9122011-11-04 13:24:29 -04001599 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600}
1601
1602
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001603/* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1604 * a different bearer. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001606static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1607 struct tipc_msg *tunnel_hdr,
1608 struct tipc_msg *msg,
1609 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610{
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001611 struct tipc_link *tunnel;
Ying Xuea6ca1092014-11-26 11:41:55 +08001612 struct sk_buff *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 u32 length = msg_size(msg);
1614
1615 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07001616 if (!tipc_link_is_up(tunnel)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001617 pr_warn("%stunnel link no longer available\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001619 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001621 skb = tipc_buf_acquire(length + INT_H_SIZE);
1622 if (!skb) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001623 pr_warn("%sunable to send tunnel msg\n", link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 return;
Allan Stephens5392d642006-06-25 23:52:50 -07001625 }
Ying Xuea6ca1092014-11-26 11:41:55 +08001626 skb_copy_to_linear_data(skb, tunnel_hdr, INT_H_SIZE);
1627 skb_copy_to_linear_data_offset(skb, INT_H_SIZE, msg, length);
1628 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629}
1630
1631
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001632/* tipc_link_failover_send_queue(): A link has gone down, but a second
1633 * link is still active. We can do failover. Tunnel the failing link's
1634 * whole send queue via the remaining link. This way, we don't lose
1635 * any packets, and sequence order is preserved for subsequent traffic
1636 * sent over the remaining link. Owner node is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 */
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001638void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639{
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001640 int msgcount;
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001641 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 struct tipc_msg tunnel_hdr;
Ying Xue58dc55f2014-11-26 11:41:52 +08001643 struct sk_buff *skb;
Allan Stephens5392d642006-06-25 23:52:50 -07001644 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645
1646 if (!tunnel)
1647 return;
1648
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001649 tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, TUNNEL_PROTOCOL,
1650 FAILOVER_MSG, INT_H_SIZE, l_ptr->addr);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001651 skb_queue_splice_tail_init(&l_ptr->backlogq, &l_ptr->transmq);
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04001652 tipc_link_purge_backlog(l_ptr);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001653 msgcount = skb_queue_len(&l_ptr->transmq);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1655 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07001656
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001657 if (skb_queue_empty(&l_ptr->transmq)) {
Ying Xue58dc55f2014-11-26 11:41:52 +08001658 skb = tipc_buf_acquire(INT_H_SIZE);
1659 if (skb) {
1660 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001662 __tipc_link_xmit_skb(tunnel, skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001664 pr_warn("%sunable to send changeover msg\n",
1665 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 }
1667 return;
1668 }
Allan Stephensf1310722006-06-25 23:51:37 -07001669
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001670 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07001671 l_ptr->owner->active_links[1]);
1672
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001673 skb_queue_walk(&l_ptr->transmq, skb) {
Ying Xue58dc55f2014-11-26 11:41:52 +08001674 struct tipc_msg *msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675
1676 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001678 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679
Florian Westphald788d802007-08-02 19:28:06 -07001680 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00001682 msg_set_seqno(m, msg_seqno(msg));
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001683 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1684 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685 pos += align(msg_size(m));
1686 m = (struct tipc_msg *)pos;
1687 }
1688 } else {
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001689 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1690 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 }
1693}
1694
Ying Xue247f0f32014-02-18 16:06:46 +08001695/* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
Jon Paul Maloy170b3922014-01-07 17:02:41 -05001696 * duplicate of the first link's send queue via the new link. This way, we
1697 * are guaranteed that currently queued packets from a socket are delivered
1698 * before future traffic from the same socket, even if this is using the
1699 * new link. The last arriving copy of each duplicate packet is dropped at
1700 * the receiving end by the regular protocol check, so packet cardinality
1701 * and sequence order is preserved per sender/receiver socket pair.
1702 * Owner node is locked.
1703 */
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001704void tipc_link_dup_queue_xmit(struct tipc_link *link,
1705 struct tipc_link *tnl)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706{
Ying Xue58dc55f2014-11-26 11:41:52 +08001707 struct sk_buff *skb;
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001708 struct tipc_msg tnl_hdr;
1709 struct sk_buff_head *queue = &link->transmq;
1710 int mcnt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001712 tipc_msg_init(link_own_addr(link), &tnl_hdr, TUNNEL_PROTOCOL,
1713 SYNCH_MSG, INT_H_SIZE, link->addr);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001714 mcnt = skb_queue_len(&link->transmq) + skb_queue_len(&link->backlogq);
1715 msg_set_msgcnt(&tnl_hdr, mcnt);
1716 msg_set_bearer_id(&tnl_hdr, link->peer_bearer_id);
1717
1718tunnel_queue:
1719 skb_queue_walk(queue, skb) {
Ying Xue58dc55f2014-11-26 11:41:52 +08001720 struct sk_buff *outskb;
1721 struct tipc_msg *msg = buf_msg(skb);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001722 u32 len = msg_size(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001724 msg_set_ack(msg, mod(link->next_in_no - 1));
1725 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
1726 msg_set_size(&tnl_hdr, len + INT_H_SIZE);
1727 outskb = tipc_buf_acquire(len + INT_H_SIZE);
Ying Xue58dc55f2014-11-26 11:41:52 +08001728 if (outskb == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001729 pr_warn("%sunable to send duplicate msg\n",
1730 link_co_err);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731 return;
1732 }
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001733 skb_copy_to_linear_data(outskb, &tnl_hdr, INT_H_SIZE);
1734 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE,
1735 skb->data, len);
1736 __tipc_link_xmit_skb(tnl, outskb);
1737 if (!tipc_link_is_up(link))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 return;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 }
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001740 if (queue == &link->backlogq)
1741 return;
1742 queue = &link->backlogq;
1743 goto tunnel_queue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744}
1745
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001746/* tipc_link_failover_rcv(): Receive a tunnelled FAILOVER_MSG packet
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001747 * Owner node is locked.
1748 */
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001749static bool tipc_link_failover_rcv(struct tipc_link *link,
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001750 struct sk_buff **skb)
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001751{
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001752 struct tipc_msg *msg = buf_msg(*skb);
1753 struct sk_buff *iskb = NULL;
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001754 struct tipc_link *pl = NULL;
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001755 int bearer_id = msg_bearer_id(msg);
Jon Paul Maloyc1336ee2015-03-13 16:08:08 -04001756 int pos = 0;
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001757
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001758 if (msg_type(msg) != FAILOVER_MSG) {
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001759 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
1760 goto exit;
Jon Paul Maloyf006c9c2014-02-13 17:29:11 -05001761 }
Dan Carpentercb4b102f2013-05-06 08:28:41 +00001762 if (bearer_id >= MAX_BEARERS)
1763 goto exit;
Jon Paul Maloy1dab3d52014-02-13 17:29:10 -05001764
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001765 if (bearer_id == link->bearer_id)
1766 goto exit;
1767
1768 pl = link->owner->links[bearer_id];
1769 if (pl && tipc_link_is_up(pl))
1770 tipc_link_reset(pl);
1771
1772 if (link->failover_pkts == FIRST_FAILOVER)
1773 link->failover_pkts = msg_msgcnt(msg);
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001774
1775 /* Should we expect an inner packet? */
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001776 if (!link->failover_pkts)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001779 if (!tipc_msg_extract(*skb, &iskb, &pos)) {
1780 pr_warn("%sno inner failover pkt\n", link_co_err);
1781 *skb = NULL;
1782 goto exit;
1783 }
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001784 link->failover_pkts--;
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001785 *skb = NULL;
1786
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001787 /* Was this packet already delivered? */
1788 if (less(buf_seqno(iskb), link->failover_checkpt)) {
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001789 kfree_skb(iskb);
1790 iskb = NULL;
1791 goto exit;
1792 }
1793 if (msg_user(buf_msg(iskb)) == MSG_FRAGMENTER) {
1794 link->stats.recv_fragments++;
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001795 tipc_buf_append(&link->failover_skb, &iskb);
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001796 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797exit:
Jon Paul Maloydff29b12015-04-02 09:33:01 -04001798 if (!link->failover_pkts && pl)
1799 pl->flags &= ~LINK_FAILINGOVER;
Jon Paul Maloy2da71422015-04-02 09:33:00 -04001800 kfree_skb(*skb);
1801 *skb = iskb;
1802 return *skb;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803}
1804
Ying Xue2f55c432015-01-09 15:27:00 +08001805static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806{
Ying Xue2f55c432015-01-09 15:27:00 +08001807 unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
1808
1809 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
Allan Stephens5413b4c2011-01-18 13:24:55 -05001810 return;
1811
Ying Xue2f55c432015-01-09 15:27:00 +08001812 l_ptr->tolerance = tol;
1813 l_ptr->cont_intv = msecs_to_jiffies(intv);
1814 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815}
1816
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -04001817void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818{
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -04001819 int max_bulk = TIPC_MAX_PUBLICATIONS / (l->max_pkt / ITEM_SIZE);
Jon Paul Maloy05dcc5a2015-03-13 16:08:10 -04001820
Jon Paul Maloye3eea1e2015-03-13 16:08:11 -04001821 l->window = win;
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04001822 l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
1823 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
1824 l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
1825 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
1826 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827}
1828
Jon Paul Maloye099e862014-02-13 17:29:18 -05001829/* tipc_link_find_owner - locate owner node of link by link's name
Ying Xuef2f98002015-01-09 15:27:05 +08001830 * @net: the applicable net namespace
Jon Paul Maloye099e862014-02-13 17:29:18 -05001831 * @name: pointer to link name string
1832 * @bearer_id: pointer to index in 'node->links' array where the link was found.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001833 *
Jon Paul Maloye099e862014-02-13 17:29:18 -05001834 * Returns pointer to node owning the link, or 0 if no matching link is found.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 */
Ying Xuef2f98002015-01-09 15:27:05 +08001836static struct tipc_node *tipc_link_find_owner(struct net *net,
1837 const char *link_name,
Jon Paul Maloye099e862014-02-13 17:29:18 -05001838 unsigned int *bearer_id)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839{
Ying Xuef2f98002015-01-09 15:27:05 +08001840 struct tipc_net *tn = net_generic(net, tipc_net_id);
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001841 struct tipc_link *l_ptr;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001842 struct tipc_node *n_ptr;
Fabian Frederick886eaa12014-12-25 12:05:50 +01001843 struct tipc_node *found_node = NULL;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001844 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845
Jon Paul Maloye099e862014-02-13 17:29:18 -05001846 *bearer_id = 0;
Ying Xue6c7a7622014-03-27 12:54:37 +08001847 rcu_read_lock();
Ying Xuef2f98002015-01-09 15:27:05 +08001848 list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001849 tipc_node_lock(n_ptr);
Erik Hugnebbfbe472013-10-18 07:23:21 +02001850 for (i = 0; i < MAX_BEARERS; i++) {
1851 l_ptr = n_ptr->links[i];
Jon Paul Maloye099e862014-02-13 17:29:18 -05001852 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
1853 *bearer_id = i;
1854 found_node = n_ptr;
1855 break;
1856 }
Erik Hugnebbfbe472013-10-18 07:23:21 +02001857 }
Jon Paul Maloya11607f2014-02-14 16:40:44 -05001858 tipc_node_unlock(n_ptr);
Jon Paul Maloye099e862014-02-13 17:29:18 -05001859 if (found_node)
1860 break;
Erik Hugnebbfbe472013-10-18 07:23:21 +02001861 }
Ying Xue6c7a7622014-03-27 12:54:37 +08001862 rcu_read_unlock();
1863
Jon Paul Maloye099e862014-02-13 17:29:18 -05001864 return found_node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865}
1866
Allan Stephens5c216e12011-10-18 11:34:29 -04001867/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 * link_reset_statistics - reset link statistics
1869 * @l_ptr: pointer to link
1870 */
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001871static void link_reset_statistics(struct tipc_link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872{
1873 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
1874 l_ptr->stats.sent_info = l_ptr->next_out_no;
1875 l_ptr->stats.recv_info = l_ptr->next_in_no;
1876}
1877
Paul Gortmakera18c4bc2011-12-29 20:58:42 -05001878static void link_print(struct tipc_link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879{
Ying Xue7f9f95d2015-01-09 15:27:06 +08001880 struct tipc_net *tn = net_generic(l_ptr->owner->net, tipc_net_id);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001881 struct tipc_bearer *b_ptr;
1882
1883 rcu_read_lock();
Ying Xue7f9f95d2015-01-09 15:27:06 +08001884 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
Ying Xue7a2f7d12014-04-21 10:55:46 +08001885 if (b_ptr)
1886 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
1887 rcu_read_unlock();
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001888
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 if (link_working_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04001890 pr_cont(":WU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001891 else if (link_reset_reset(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04001892 pr_cont(":RR\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001893 else if (link_reset_unknown(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04001894 pr_cont(":RU\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001895 else if (link_working_working(l_ptr))
Paul Gortmaker5deedde2012-07-11 19:27:56 -04001896 pr_cont(":WW\n");
1897 else
1898 pr_cont("\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899}
Richard Alpe0655f6a2014-11-20 10:29:07 +01001900
1901/* Parse and validate nested (link) properties valid for media, bearer and link
1902 */
1903int tipc_nl_parse_link_prop(struct nlattr *prop, struct nlattr *props[])
1904{
1905 int err;
1906
1907 err = nla_parse_nested(props, TIPC_NLA_PROP_MAX, prop,
1908 tipc_nl_prop_policy);
1909 if (err)
1910 return err;
1911
1912 if (props[TIPC_NLA_PROP_PRIO]) {
1913 u32 prio;
1914
1915 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1916 if (prio > TIPC_MAX_LINK_PRI)
1917 return -EINVAL;
1918 }
1919
1920 if (props[TIPC_NLA_PROP_TOL]) {
1921 u32 tol;
1922
1923 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1924 if ((tol < TIPC_MIN_LINK_TOL) || (tol > TIPC_MAX_LINK_TOL))
1925 return -EINVAL;
1926 }
1927
1928 if (props[TIPC_NLA_PROP_WIN]) {
1929 u32 win;
1930
1931 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
1932 if ((win < TIPC_MIN_LINK_WIN) || (win > TIPC_MAX_LINK_WIN))
1933 return -EINVAL;
1934 }
1935
1936 return 0;
1937}
Richard Alpe7be57fc2014-11-20 10:29:12 +01001938
Richard Alpef96ce7a2014-11-20 10:29:13 +01001939int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
1940{
1941 int err;
1942 int res = 0;
1943 int bearer_id;
1944 char *name;
1945 struct tipc_link *link;
1946 struct tipc_node *node;
1947 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Richard Alpe37e2d482015-02-09 09:50:08 +01001948 struct net *net = sock_net(skb->sk);
Richard Alpef96ce7a2014-11-20 10:29:13 +01001949
1950 if (!info->attrs[TIPC_NLA_LINK])
1951 return -EINVAL;
1952
1953 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
1954 info->attrs[TIPC_NLA_LINK],
1955 tipc_nl_link_policy);
1956 if (err)
1957 return err;
1958
1959 if (!attrs[TIPC_NLA_LINK_NAME])
1960 return -EINVAL;
1961
1962 name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
1963
Ying Xuef2f98002015-01-09 15:27:05 +08001964 node = tipc_link_find_owner(net, name, &bearer_id);
Richard Alpef96ce7a2014-11-20 10:29:13 +01001965 if (!node)
1966 return -EINVAL;
1967
1968 tipc_node_lock(node);
1969
1970 link = node->links[bearer_id];
1971 if (!link) {
1972 res = -EINVAL;
1973 goto out;
1974 }
1975
1976 if (attrs[TIPC_NLA_LINK_PROP]) {
1977 struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
1978
1979 err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
1980 props);
1981 if (err) {
1982 res = err;
1983 goto out;
1984 }
1985
1986 if (props[TIPC_NLA_PROP_TOL]) {
1987 u32 tol;
1988
1989 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
1990 link_set_supervision_props(link, tol);
1991 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0);
1992 }
1993 if (props[TIPC_NLA_PROP_PRIO]) {
1994 u32 prio;
1995
1996 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
1997 link->priority = prio;
1998 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0);
1999 }
2000 if (props[TIPC_NLA_PROP_WIN]) {
2001 u32 win;
2002
2003 win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
2004 tipc_link_set_queue_limits(link, win);
2005 }
2006 }
2007
2008out:
2009 tipc_node_unlock(node);
2010
2011 return res;
2012}
Richard Alped8182802014-11-24 11:10:29 +01002013
2014static int __tipc_nl_add_stats(struct sk_buff *skb, struct tipc_stats *s)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002015{
2016 int i;
2017 struct nlattr *stats;
2018
2019 struct nla_map {
2020 u32 key;
2021 u32 val;
2022 };
2023
2024 struct nla_map map[] = {
2025 {TIPC_NLA_STATS_RX_INFO, s->recv_info},
2026 {TIPC_NLA_STATS_RX_FRAGMENTS, s->recv_fragments},
2027 {TIPC_NLA_STATS_RX_FRAGMENTED, s->recv_fragmented},
2028 {TIPC_NLA_STATS_RX_BUNDLES, s->recv_bundles},
2029 {TIPC_NLA_STATS_RX_BUNDLED, s->recv_bundled},
2030 {TIPC_NLA_STATS_TX_INFO, s->sent_info},
2031 {TIPC_NLA_STATS_TX_FRAGMENTS, s->sent_fragments},
2032 {TIPC_NLA_STATS_TX_FRAGMENTED, s->sent_fragmented},
2033 {TIPC_NLA_STATS_TX_BUNDLES, s->sent_bundles},
2034 {TIPC_NLA_STATS_TX_BUNDLED, s->sent_bundled},
2035 {TIPC_NLA_STATS_MSG_PROF_TOT, (s->msg_length_counts) ?
2036 s->msg_length_counts : 1},
2037 {TIPC_NLA_STATS_MSG_LEN_CNT, s->msg_length_counts},
2038 {TIPC_NLA_STATS_MSG_LEN_TOT, s->msg_lengths_total},
2039 {TIPC_NLA_STATS_MSG_LEN_P0, s->msg_length_profile[0]},
2040 {TIPC_NLA_STATS_MSG_LEN_P1, s->msg_length_profile[1]},
2041 {TIPC_NLA_STATS_MSG_LEN_P2, s->msg_length_profile[2]},
2042 {TIPC_NLA_STATS_MSG_LEN_P3, s->msg_length_profile[3]},
2043 {TIPC_NLA_STATS_MSG_LEN_P4, s->msg_length_profile[4]},
2044 {TIPC_NLA_STATS_MSG_LEN_P5, s->msg_length_profile[5]},
2045 {TIPC_NLA_STATS_MSG_LEN_P6, s->msg_length_profile[6]},
2046 {TIPC_NLA_STATS_RX_STATES, s->recv_states},
2047 {TIPC_NLA_STATS_RX_PROBES, s->recv_probes},
2048 {TIPC_NLA_STATS_RX_NACKS, s->recv_nacks},
2049 {TIPC_NLA_STATS_RX_DEFERRED, s->deferred_recv},
2050 {TIPC_NLA_STATS_TX_STATES, s->sent_states},
2051 {TIPC_NLA_STATS_TX_PROBES, s->sent_probes},
2052 {TIPC_NLA_STATS_TX_NACKS, s->sent_nacks},
2053 {TIPC_NLA_STATS_TX_ACKS, s->sent_acks},
2054 {TIPC_NLA_STATS_RETRANSMITTED, s->retransmitted},
2055 {TIPC_NLA_STATS_DUPLICATES, s->duplicates},
2056 {TIPC_NLA_STATS_LINK_CONGS, s->link_congs},
2057 {TIPC_NLA_STATS_MAX_QUEUE, s->max_queue_sz},
2058 {TIPC_NLA_STATS_AVG_QUEUE, s->queue_sz_counts ?
2059 (s->accu_queue_sz / s->queue_sz_counts) : 0}
2060 };
2061
2062 stats = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
2063 if (!stats)
2064 return -EMSGSIZE;
2065
2066 for (i = 0; i < ARRAY_SIZE(map); i++)
2067 if (nla_put_u32(skb, map[i].key, map[i].val))
2068 goto msg_full;
2069
2070 nla_nest_end(skb, stats);
2071
2072 return 0;
2073msg_full:
2074 nla_nest_cancel(skb, stats);
2075
2076 return -EMSGSIZE;
2077}
2078
2079/* Caller should hold appropriate locks to protect the link */
Ying Xue34747532015-01-09 15:27:10 +08002080static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2081 struct tipc_link *link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002082{
2083 int err;
2084 void *hdr;
2085 struct nlattr *attrs;
2086 struct nlattr *prop;
Ying Xue34747532015-01-09 15:27:10 +08002087 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002088
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002089 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
Richard Alpe7be57fc2014-11-20 10:29:12 +01002090 NLM_F_MULTI, TIPC_NL_LINK_GET);
2091 if (!hdr)
2092 return -EMSGSIZE;
2093
2094 attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
2095 if (!attrs)
2096 goto msg_full;
2097
2098 if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, link->name))
2099 goto attr_msg_full;
2100 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
Ying Xue34747532015-01-09 15:27:10 +08002101 tipc_cluster_mask(tn->own_addr)))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002102 goto attr_msg_full;
2103 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt))
2104 goto attr_msg_full;
2105 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2106 goto attr_msg_full;
2107 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, link->next_out_no))
2108 goto attr_msg_full;
2109
2110 if (tipc_link_is_up(link))
2111 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
2112 goto attr_msg_full;
2113 if (tipc_link_is_active(link))
2114 if (nla_put_flag(msg->skb, TIPC_NLA_LINK_ACTIVE))
2115 goto attr_msg_full;
2116
2117 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
2118 if (!prop)
2119 goto attr_msg_full;
2120 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2121 goto prop_msg_full;
2122 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2123 goto prop_msg_full;
2124 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
Jon Paul Maloy1f66d162015-03-25 12:07:24 -04002125 link->window))
Richard Alpe7be57fc2014-11-20 10:29:12 +01002126 goto prop_msg_full;
2127 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2128 goto prop_msg_full;
2129 nla_nest_end(msg->skb, prop);
2130
2131 err = __tipc_nl_add_stats(msg->skb, &link->stats);
2132 if (err)
2133 goto attr_msg_full;
2134
2135 nla_nest_end(msg->skb, attrs);
2136 genlmsg_end(msg->skb, hdr);
2137
2138 return 0;
2139
2140prop_msg_full:
2141 nla_nest_cancel(msg->skb, prop);
2142attr_msg_full:
2143 nla_nest_cancel(msg->skb, attrs);
2144msg_full:
2145 genlmsg_cancel(msg->skb, hdr);
2146
2147 return -EMSGSIZE;
2148}
2149
2150/* Caller should hold node lock */
Ying Xue34747532015-01-09 15:27:10 +08002151static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
2152 struct tipc_node *node, u32 *prev_link)
Richard Alpe7be57fc2014-11-20 10:29:12 +01002153{
2154 u32 i;
2155 int err;
2156
2157 for (i = *prev_link; i < MAX_BEARERS; i++) {
2158 *prev_link = i;
2159
2160 if (!node->links[i])
2161 continue;
2162
Ying Xue34747532015-01-09 15:27:10 +08002163 err = __tipc_nl_add_link(net, msg, node->links[i]);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002164 if (err)
2165 return err;
2166 }
2167 *prev_link = 0;
2168
2169 return 0;
2170}
2171
2172int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2173{
Ying Xuef2f98002015-01-09 15:27:05 +08002174 struct net *net = sock_net(skb->sk);
2175 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002176 struct tipc_node *node;
2177 struct tipc_nl_msg msg;
2178 u32 prev_node = cb->args[0];
2179 u32 prev_link = cb->args[1];
2180 int done = cb->args[2];
2181 int err;
2182
2183 if (done)
2184 return 0;
2185
2186 msg.skb = skb;
2187 msg.portid = NETLINK_CB(cb->skb).portid;
2188 msg.seq = cb->nlh->nlmsg_seq;
2189
2190 rcu_read_lock();
Richard Alpe7be57fc2014-11-20 10:29:12 +01002191 if (prev_node) {
Ying Xuef2f98002015-01-09 15:27:05 +08002192 node = tipc_node_find(net, prev_node);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002193 if (!node) {
2194 /* We never set seq or call nl_dump_check_consistent()
2195 * this means that setting prev_seq here will cause the
2196 * consistence check to fail in the netlink callback
2197 * handler. Resulting in the last NLMSG_DONE message
2198 * having the NLM_F_DUMP_INTR flag set.
2199 */
2200 cb->prev_seq = 1;
2201 goto out;
2202 }
Ying Xue8a0f6eb2015-03-26 18:10:24 +08002203 tipc_node_put(node);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002204
Ying Xuef2f98002015-01-09 15:27:05 +08002205 list_for_each_entry_continue_rcu(node, &tn->node_list,
2206 list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01002207 tipc_node_lock(node);
Ying Xue34747532015-01-09 15:27:10 +08002208 err = __tipc_nl_add_node_links(net, &msg, node,
2209 &prev_link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002210 tipc_node_unlock(node);
Ying Xue8a0f6eb2015-03-26 18:10:24 +08002211 tipc_node_put(node);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002212 if (err)
2213 goto out;
2214
2215 prev_node = node->addr;
2216 }
2217 } else {
Ying Xue1da46562015-01-09 15:27:07 +08002218 err = tipc_nl_add_bc_link(net, &msg);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002219 if (err)
2220 goto out;
2221
Ying Xuef2f98002015-01-09 15:27:05 +08002222 list_for_each_entry_rcu(node, &tn->node_list, list) {
Richard Alpe7be57fc2014-11-20 10:29:12 +01002223 tipc_node_lock(node);
Ying Xue34747532015-01-09 15:27:10 +08002224 err = __tipc_nl_add_node_links(net, &msg, node,
2225 &prev_link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002226 tipc_node_unlock(node);
2227 if (err)
2228 goto out;
2229
2230 prev_node = node->addr;
2231 }
2232 }
2233 done = 1;
2234out:
2235 rcu_read_unlock();
2236
2237 cb->args[0] = prev_node;
2238 cb->args[1] = prev_link;
2239 cb->args[2] = done;
2240
2241 return skb->len;
2242}
2243
2244int tipc_nl_link_get(struct sk_buff *skb, struct genl_info *info)
2245{
Ying Xuef2f98002015-01-09 15:27:05 +08002246 struct net *net = genl_info_net(info);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002247 struct sk_buff *ans_skb;
2248 struct tipc_nl_msg msg;
2249 struct tipc_link *link;
2250 struct tipc_node *node;
2251 char *name;
2252 int bearer_id;
2253 int err;
2254
2255 if (!info->attrs[TIPC_NLA_LINK_NAME])
2256 return -EINVAL;
2257
2258 name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
Ying Xuef2f98002015-01-09 15:27:05 +08002259 node = tipc_link_find_owner(net, name, &bearer_id);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002260 if (!node)
2261 return -EINVAL;
2262
2263 ans_skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2264 if (!ans_skb)
2265 return -ENOMEM;
2266
2267 msg.skb = ans_skb;
2268 msg.portid = info->snd_portid;
2269 msg.seq = info->snd_seq;
2270
2271 tipc_node_lock(node);
2272 link = node->links[bearer_id];
2273 if (!link) {
2274 err = -EINVAL;
2275 goto err_out;
2276 }
2277
Ying Xue34747532015-01-09 15:27:10 +08002278 err = __tipc_nl_add_link(net, &msg, link);
Richard Alpe7be57fc2014-11-20 10:29:12 +01002279 if (err)
2280 goto err_out;
2281
2282 tipc_node_unlock(node);
2283
2284 return genlmsg_reply(ans_skb, info);
2285
2286err_out:
2287 tipc_node_unlock(node);
2288 nlmsg_free(ans_skb);
2289
2290 return err;
2291}
Richard Alpeae363422014-11-20 10:29:14 +01002292
2293int tipc_nl_link_reset_stats(struct sk_buff *skb, struct genl_info *info)
2294{
2295 int err;
2296 char *link_name;
2297 unsigned int bearer_id;
2298 struct tipc_link *link;
2299 struct tipc_node *node;
2300 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
Richard Alpe18178772015-02-09 09:50:09 +01002301 struct net *net = sock_net(skb->sk);
Richard Alpeae363422014-11-20 10:29:14 +01002302
2303 if (!info->attrs[TIPC_NLA_LINK])
2304 return -EINVAL;
2305
2306 err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
2307 info->attrs[TIPC_NLA_LINK],
2308 tipc_nl_link_policy);
2309 if (err)
2310 return err;
2311
2312 if (!attrs[TIPC_NLA_LINK_NAME])
2313 return -EINVAL;
2314
2315 link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
2316
2317 if (strcmp(link_name, tipc_bclink_name) == 0) {
Ying Xue1da46562015-01-09 15:27:07 +08002318 err = tipc_bclink_reset_stats(net);
Richard Alpeae363422014-11-20 10:29:14 +01002319 if (err)
2320 return err;
2321 return 0;
2322 }
2323
Ying Xuef2f98002015-01-09 15:27:05 +08002324 node = tipc_link_find_owner(net, link_name, &bearer_id);
Richard Alpeae363422014-11-20 10:29:14 +01002325 if (!node)
2326 return -EINVAL;
2327
2328 tipc_node_lock(node);
2329
2330 link = node->links[bearer_id];
2331 if (!link) {
2332 tipc_node_unlock(node);
2333 return -EINVAL;
2334 }
2335
2336 link_reset_statistics(link);
2337
2338 tipc_node_unlock(node);
2339
2340 return 0;
2341}