blob: d1818fbc19a3dc8e23a121fd74126e31c7a6ace3 [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 *
Allan Stephens05646c92007-06-10 17:25:24 -07004 * Copyright (c) 1996-2007, Ericsson AB
Allan Stephens23dd4cc2011-01-07 11:43:40 -05005 * Copyright (c) 2004-2007, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "discover.h"
42#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
44
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090045/*
Allan Stephensa686e682008-06-04 17:29:39 -070046 * Out-of-range value for link session numbers
47 */
48
49#define INVALID_SESSION 0x10000
50
51/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090052 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010053 */
54
55#define STARTING_EVT 856384768 /* link processing trigger */
56#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
57#define TIMEOUT_EVT 560817u /* link timer expired */
58
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090059/*
60 * The following two 'message types' is really just implementation
61 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010062 * They must not be considered part of the protocol
63 */
64#define OPEN_MSG 0
65#define CLOSED_MSG 1
66
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090067/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010068 * State value stored in 'exp_msg_count'
69 */
70
71#define START_CHANGEOVER 100000u
72
73/**
74 * struct link_name - deconstructed link name
75 * @addr_local: network address of node at this end
76 * @if_local: name of interface at this end
77 * @addr_peer: network address of node at far end
78 * @if_peer: name of interface at far end
79 */
80
81struct link_name {
82 u32 addr_local;
83 char if_local[TIPC_MAX_IF_NAME];
84 u32 addr_peer;
85 char if_peer[TIPC_MAX_IF_NAME];
86};
87
Per Lidenb97bf3f2006-01-02 19:04:38 +010088static void link_handle_out_of_seq_msg(struct link *l_ptr,
89 struct sk_buff *buf);
90static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
91static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
92static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050093static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 struct iovec const *msg_sect,
95 u32 num_sect, u32 destnode);
96static void link_check_defragm_bufs(struct link *l_ptr);
97static void link_state_event(struct link *l_ptr, u32 event);
98static void link_reset_statistics(struct link *l_ptr);
Allan Stephens8d64a5b2010-12-31 18:59:27 +000099static void link_print(struct link *l_ptr, const char *str);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000100static void link_start(struct link *l_ptr);
101static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf);
102
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800104 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 */
106
Sam Ravnborg05790c62006-03-20 22:37:04 -0800107static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108{
109 return (i + 3) & ~3u;
110}
111
Sam Ravnborg05790c62006-03-20 22:37:04 -0800112static void link_init_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113{
114 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900115
Allan Stephens2d627b92011-01-07 13:00:11 -0500116 max_pkt = (l_ptr->b_ptr->mtu & ~3);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117 if (max_pkt > MAX_MSG_SIZE)
118 max_pkt = MAX_MSG_SIZE;
119
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900120 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
122 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 l_ptr->max_pkt = MAX_PKT_DEFAULT;
125
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900126 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127}
128
Sam Ravnborg05790c62006-03-20 22:37:04 -0800129static u32 link_next_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130{
131 if (l_ptr->next_out)
132 return msg_seqno(buf_msg(l_ptr->next_out));
133 return mod(l_ptr->next_out_no);
134}
135
Sam Ravnborg05790c62006-03-20 22:37:04 -0800136static u32 link_last_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137{
138 return mod(link_next_sent(l_ptr) - 1);
139}
140
141/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800142 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 */
144
Per Liden4323add2006-01-18 00:38:21 +0100145int tipc_link_is_up(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146{
147 if (!l_ptr)
148 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000149 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150}
151
Per Liden4323add2006-01-18 00:38:21 +0100152int tipc_link_is_active(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000154 return (l_ptr->owner->active_links[0] == l_ptr) ||
155 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156}
157
158/**
159 * link_name_validate - validate & (optionally) deconstruct link name
160 * @name - ptr to link name string
161 * @name_parts - ptr to area for link name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900162 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 * Returns 1 if link name is valid, otherwise 0.
164 */
165
166static int link_name_validate(const char *name, struct link_name *name_parts)
167{
168 char name_copy[TIPC_MAX_LINK_NAME];
169 char *addr_local;
170 char *if_local;
171 char *addr_peer;
172 char *if_peer;
173 char dummy;
174 u32 z_local, c_local, n_local;
175 u32 z_peer, c_peer, n_peer;
176 u32 if_local_len;
177 u32 if_peer_len;
178
179 /* copy link name & ensure length is OK */
180
181 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
182 /* need above in case non-Posix strncpy() doesn't pad with nulls */
183 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
184 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
185 return 0;
186
187 /* ensure all component parts of link name are present */
188
189 addr_local = name_copy;
Allan Stephens2db99832010-12-31 18:59:33 +0000190 if_local = strchr(addr_local, ':');
191 if (if_local == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 return 0;
193 *(if_local++) = 0;
Allan Stephens2db99832010-12-31 18:59:33 +0000194 addr_peer = strchr(if_local, '-');
195 if (addr_peer == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 return 0;
197 *(addr_peer++) = 0;
198 if_local_len = addr_peer - if_local;
Allan Stephens2db99832010-12-31 18:59:33 +0000199 if_peer = strchr(addr_peer, ':');
200 if (if_peer == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 return 0;
202 *(if_peer++) = 0;
203 if_peer_len = strlen(if_peer) + 1;
204
205 /* validate component parts of link name */
206
207 if ((sscanf(addr_local, "%u.%u.%u%c",
208 &z_local, &c_local, &n_local, &dummy) != 3) ||
209 (sscanf(addr_peer, "%u.%u.%u%c",
210 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
211 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
212 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900213 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
214 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
216 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
217 return 0;
218
219 /* return link name components, if necessary */
220
221 if (name_parts) {
222 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
223 strcpy(name_parts->if_local, if_local);
224 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
225 strcpy(name_parts->if_peer, if_peer);
226 }
227 return 1;
228}
229
230/**
231 * link_timeout - handle expiration of link timer
232 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900233 *
Per Liden4323add2006-01-18 00:38:21 +0100234 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
235 * with tipc_link_delete(). (There is no risk that the node will be deleted by
236 * another thread because tipc_link_delete() always cancels the link timer before
237 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 */
239
240static void link_timeout(struct link *l_ptr)
241{
Per Liden4323add2006-01-18 00:38:21 +0100242 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
244 /* update counters used in statistical profiling of send traffic */
245
246 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
247 l_ptr->stats.queue_sz_counts++;
248
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 if (l_ptr->first_out) {
250 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
251 u32 length = msg_size(msg);
252
Joe Perchesf64f9e72009-11-29 16:55:45 -0800253 if ((msg_user(msg) == MSG_FRAGMENTER) &&
254 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 length = msg_size(msg_get_wrapped(msg));
256 }
257 if (length) {
258 l_ptr->stats.msg_lengths_total += length;
259 l_ptr->stats.msg_length_counts++;
260 if (length <= 64)
261 l_ptr->stats.msg_length_profile[0]++;
262 else if (length <= 256)
263 l_ptr->stats.msg_length_profile[1]++;
264 else if (length <= 1024)
265 l_ptr->stats.msg_length_profile[2]++;
266 else if (length <= 4096)
267 l_ptr->stats.msg_length_profile[3]++;
268 else if (length <= 16384)
269 l_ptr->stats.msg_length_profile[4]++;
270 else if (length <= 32768)
271 l_ptr->stats.msg_length_profile[5]++;
272 else
273 l_ptr->stats.msg_length_profile[6]++;
274 }
275 }
276
277 /* do all other link processing performed on a periodic basis */
278
279 link_check_defragm_bufs(l_ptr);
280
281 link_state_event(l_ptr, TIMEOUT_EVT);
282
283 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100284 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287}
288
Sam Ravnborg05790c62006-03-20 22:37:04 -0800289static void link_set_timer(struct link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290{
291 k_start_timer(&l_ptr->timer, time);
292}
293
294/**
Per Liden4323add2006-01-18 00:38:21 +0100295 * tipc_link_create - create a new link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 * @b_ptr: pointer to associated bearer
297 * @peer: network address of node at other end of link
298 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 * Returns pointer to link.
301 */
302
Allan Stephens2d627b92011-01-07 13:00:11 -0500303struct link *tipc_link_create(struct tipc_bearer *b_ptr, const u32 peer,
Per Liden4323add2006-01-18 00:38:21 +0100304 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305{
306 struct link *l_ptr;
307 struct tipc_msg *msg;
308 char *if_name;
309
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700310 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 if (!l_ptr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700312 warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 return NULL;
314 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315
316 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500317 if_name = strchr(b_ptr->name, ':') + 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
319 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 if_name,
322 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
323 /* note: peer i/f is appended to link name by reset/activate */
324 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 l_ptr->checkpoint = 1;
326 l_ptr->b_ptr = b_ptr;
327 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
328 l_ptr->state = RESET_UNKNOWN;
329
330 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
331 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000332 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700334 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 msg_set_bearer_id(msg, b_ptr->identity);
336 strcpy((char *)msg_data(msg), if_name);
337
338 l_ptr->priority = b_ptr->priority;
Per Liden4323add2006-01-18 00:38:21 +0100339 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340
341 link_init_max_pkt(l_ptr);
342
343 l_ptr->next_out_no = 1;
344 INIT_LIST_HEAD(&l_ptr->waiting_ports);
345
346 link_reset_statistics(l_ptr);
347
Per Liden4323add2006-01-18 00:38:21 +0100348 l_ptr->owner = tipc_node_attach_link(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 if (!l_ptr->owner) {
350 kfree(l_ptr);
351 return NULL;
352 }
353
Florian Westphal945710652007-07-26 00:05:07 -0700354 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
355 list_add_tail(&l_ptr->link_list, &b_ptr->links);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000356 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 return l_ptr;
359}
360
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900361/**
Per Liden4323add2006-01-18 00:38:21 +0100362 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900364 *
Per Liden4323add2006-01-18 00:38:21 +0100365 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900367 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 */
369
Per Liden4323add2006-01-18 00:38:21 +0100370void tipc_link_delete(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371{
372 if (!l_ptr) {
373 err("Attempt to delete non-existent link\n");
374 return;
375 }
376
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378
Per Liden4323add2006-01-18 00:38:21 +0100379 tipc_node_lock(l_ptr->owner);
380 tipc_link_reset(l_ptr);
381 tipc_node_detach_link(l_ptr->owner, l_ptr);
382 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 list_del_init(&l_ptr->link_list);
Per Liden4323add2006-01-18 00:38:21 +0100384 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 k_term_timer(&l_ptr->timer);
386 kfree(l_ptr);
387}
388
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000389static void link_start(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 link_state_event(l_ptr, STARTING_EVT);
392}
393
394/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900395 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396 * @l_ptr: pointer to link
397 * @origport: reference to sending port
398 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399 *
400 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 * has abated.
402 */
403
404static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
405{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500406 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407
Per Liden4323add2006-01-18 00:38:21 +0100408 spin_lock_bh(&tipc_port_list_lock);
409 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 if (p_ptr) {
411 if (!p_ptr->wakeup)
412 goto exit;
413 if (!list_empty(&p_ptr->wait_list))
414 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500415 p_ptr->congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000416 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
418 l_ptr->stats.link_congs++;
419exit:
Per Liden4323add2006-01-18 00:38:21 +0100420 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 }
Per Liden4323add2006-01-18 00:38:21 +0100422 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 return -ELINKCONG;
424}
425
Per Liden4323add2006-01-18 00:38:21 +0100426void tipc_link_wakeup_ports(struct link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500428 struct tipc_port *p_ptr;
429 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
431
432 if (all)
433 win = 100000;
434 if (win <= 0)
435 return;
Per Liden4323add2006-01-18 00:38:21 +0100436 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 return;
438 if (link_congested(l_ptr))
439 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900440 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 wait_list) {
442 if (win <= 0)
443 break;
444 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500445 spin_lock_bh(p_ptr->lock);
446 p_ptr->congested = 0;
447 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500449 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 }
451
452exit:
Per Liden4323add2006-01-18 00:38:21 +0100453 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454}
455
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900456/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 * link_release_outqueue - purge link's outbound message queue
458 * @l_ptr: pointer to link
459 */
460
461static void link_release_outqueue(struct link *l_ptr)
462{
463 struct sk_buff *buf = l_ptr->first_out;
464 struct sk_buff *next;
465
466 while (buf) {
467 next = buf->next;
468 buf_discard(buf);
469 buf = next;
470 }
471 l_ptr->first_out = NULL;
472 l_ptr->out_queue_size = 0;
473}
474
475/**
Per Liden4323add2006-01-18 00:38:21 +0100476 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 * @l_ptr: pointer to link
478 */
479
Per Liden4323add2006-01-18 00:38:21 +0100480void tipc_link_reset_fragments(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481{
482 struct sk_buff *buf = l_ptr->defragm_buf;
483 struct sk_buff *next;
484
485 while (buf) {
486 next = buf->next;
487 buf_discard(buf);
488 buf = next;
489 }
490 l_ptr->defragm_buf = NULL;
491}
492
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900493/**
Per Liden4323add2006-01-18 00:38:21 +0100494 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 * @l_ptr: pointer to link
496 */
497
Per Liden4323add2006-01-18 00:38:21 +0100498void tipc_link_stop(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499{
500 struct sk_buff *buf;
501 struct sk_buff *next;
502
503 buf = l_ptr->oldest_deferred_in;
504 while (buf) {
505 next = buf->next;
506 buf_discard(buf);
507 buf = next;
508 }
509
510 buf = l_ptr->first_out;
511 while (buf) {
512 next = buf->next;
513 buf_discard(buf);
514 buf = next;
515 }
516
Per Liden4323add2006-01-18 00:38:21 +0100517 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518
519 buf_discard(l_ptr->proto_msg_queue);
520 l_ptr->proto_msg_queue = NULL;
521}
522
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524#define link_send_event(fcn, l_ptr, up) do { } while (0)
525
Per Liden4323add2006-01-18 00:38:21 +0100526void tipc_link_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527{
528 struct sk_buff *buf;
529 u32 prev_state = l_ptr->state;
530 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700531 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900532
Allan Stephensa686e682008-06-04 17:29:39 -0700533 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534
Allan Stephensa686e682008-06-04 17:29:39 -0700535 /* Link is down, accept any session */
536 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900538 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900540
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542
543 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
544 return;
545
Per Liden4323add2006-01-18 00:38:21 +0100546 tipc_node_link_down(l_ptr->owner, l_ptr);
547 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000548
Allan Stephens5392d642006-06-25 23:52:50 -0700549 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 l_ptr->owner->permit_changeover) {
551 l_ptr->reset_checkpoint = checkpoint;
552 l_ptr->exp_msg_count = START_CHANGEOVER;
553 }
554
555 /* Clean up all queues: */
556
557 link_release_outqueue(l_ptr);
558 buf_discard(l_ptr->proto_msg_queue);
559 l_ptr->proto_msg_queue = NULL;
560 buf = l_ptr->oldest_deferred_in;
561 while (buf) {
562 struct sk_buff *next = buf->next;
563 buf_discard(buf);
564 buf = next;
565 }
566 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100567 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568
569 l_ptr->retransm_queue_head = 0;
570 l_ptr->retransm_queue_size = 0;
571 l_ptr->last_out = NULL;
572 l_ptr->first_out = NULL;
573 l_ptr->next_out = NULL;
574 l_ptr->unacked_window = 0;
575 l_ptr->checkpoint = 1;
576 l_ptr->next_out_no = 1;
577 l_ptr->deferred_inqueue_sz = 0;
578 l_ptr->oldest_deferred_in = NULL;
579 l_ptr->newest_deferred_in = NULL;
580 l_ptr->fsm_msg_cnt = 0;
581 l_ptr->stale_count = 0;
582 link_reset_statistics(l_ptr);
583
Per Liden4323add2006-01-18 00:38:21 +0100584 link_send_event(tipc_cfg_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100586 link_send_event(tipc_disc_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587}
588
589
590static void link_activate(struct link *l_ptr)
591{
Allan Stephens5392d642006-06-25 23:52:50 -0700592 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100593 tipc_node_link_up(l_ptr->owner, l_ptr);
594 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
595 link_send_event(tipc_cfg_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100597 link_send_event(tipc_disc_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598}
599
600/**
601 * link_state_event - link finite state machine
602 * @l_ptr: pointer to link
603 * @event: state machine event to process
604 */
605
606static void link_state_event(struct link *l_ptr, unsigned event)
607{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 struct link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 u32 cont_intv = l_ptr->continuity_interval;
610
611 if (!l_ptr->started && (event != STARTING_EVT))
612 return; /* Not yet. */
613
614 if (link_blocked(l_ptr)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000615 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 return; /* Changeover going on */
618 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619
620 switch (l_ptr->state) {
621 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 switch (event) {
623 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 break;
626 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 if (l_ptr->next_in_no != l_ptr->checkpoint) {
628 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100629 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100631 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 l_ptr->fsm_msg_cnt++;
633 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900634 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100635 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 l_ptr->fsm_msg_cnt++;
637 }
638 link_set_timer(l_ptr, cont_intv);
639 break;
640 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 l_ptr->state = WORKING_UNKNOWN;
642 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100643 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 l_ptr->fsm_msg_cnt++;
645 link_set_timer(l_ptr, cont_intv / 4);
646 break;
647 case RESET_MSG:
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900648 info("Resetting link <%s>, requested by peer\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700649 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100650 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 l_ptr->state = RESET_RESET;
652 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100653 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 l_ptr->fsm_msg_cnt++;
655 link_set_timer(l_ptr, cont_intv);
656 break;
657 default:
658 err("Unknown link event %u in WW state\n", event);
659 }
660 break;
661 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 switch (event) {
663 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 l_ptr->state = WORKING_WORKING;
666 l_ptr->fsm_msg_cnt = 0;
667 link_set_timer(l_ptr, cont_intv);
668 break;
669 case RESET_MSG:
Allan Stephensa10bd922006-06-25 23:52:17 -0700670 info("Resetting link <%s>, requested by peer "
671 "while probing\n", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100672 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 l_ptr->state = RESET_RESET;
674 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100675 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 l_ptr->fsm_msg_cnt++;
677 link_set_timer(l_ptr, cont_intv);
678 break;
679 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 l_ptr->state = WORKING_WORKING;
682 l_ptr->fsm_msg_cnt = 0;
683 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100684 if (tipc_bclink_acks_missing(l_ptr->owner)) {
685 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
686 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 l_ptr->fsm_msg_cnt++;
688 }
689 link_set_timer(l_ptr, cont_intv);
690 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900691 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100692 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 l_ptr->fsm_msg_cnt++;
694 link_set_timer(l_ptr, cont_intv / 4);
695 } else { /* Link has failed */
Allan Stephensa10bd922006-06-25 23:52:17 -0700696 warn("Resetting link <%s>, peer not responding\n",
697 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100698 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 l_ptr->state = RESET_UNKNOWN;
700 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100701 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
702 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 l_ptr->fsm_msg_cnt++;
704 link_set_timer(l_ptr, cont_intv);
705 }
706 break;
707 default:
708 err("Unknown link event %u in WU state\n", event);
709 }
710 break;
711 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 switch (event) {
713 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714 break;
715 case ACTIVATE_MSG:
716 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000717 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 l_ptr->state = WORKING_WORKING;
720 l_ptr->fsm_msg_cnt = 0;
721 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100722 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 l_ptr->fsm_msg_cnt++;
724 link_set_timer(l_ptr, cont_intv);
725 break;
726 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 l_ptr->state = RESET_RESET;
728 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100729 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 l_ptr->fsm_msg_cnt++;
731 link_set_timer(l_ptr, cont_intv);
732 break;
733 case STARTING_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 l_ptr->started = 1;
735 /* fall through */
736 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100737 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 l_ptr->fsm_msg_cnt++;
739 link_set_timer(l_ptr, cont_intv);
740 break;
741 default:
742 err("Unknown link event %u in RU state\n", event);
743 }
744 break;
745 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 switch (event) {
747 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 case ACTIVATE_MSG:
749 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000750 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 l_ptr->state = WORKING_WORKING;
753 l_ptr->fsm_msg_cnt = 0;
754 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100755 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756 l_ptr->fsm_msg_cnt++;
757 link_set_timer(l_ptr, cont_intv);
758 break;
759 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 break;
761 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100762 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 l_ptr->fsm_msg_cnt++;
764 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 break;
766 default:
767 err("Unknown link event %u in RR state\n", event);
768 }
769 break;
770 default:
771 err("Unknown link state %u/%u\n", l_ptr->state, event);
772 }
773}
774
775/*
776 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900777 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 */
779
780static int link_bundle_buf(struct link *l_ptr,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900781 struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 struct sk_buff *buf)
783{
784 struct tipc_msg *bundler_msg = buf_msg(bundler);
785 struct tipc_msg *msg = buf_msg(buf);
786 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700787 u32 bundle_size = msg_size(bundler_msg);
788 u32 to_pos = align(bundle_size);
789 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790
791 if (msg_user(bundler_msg) != MSG_BUNDLER)
792 return 0;
793 if (msg_type(bundler_msg) != OPEN_MSG)
794 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700795 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000797 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700798 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799
Allan Stephense49060c2006-06-29 12:32:46 -0700800 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300801 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 msg_set_size(bundler_msg, to_pos + size);
803 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 buf_discard(buf);
805 l_ptr->stats.sent_bundled++;
806 return 1;
807}
808
Sam Ravnborg05790c62006-03-20 22:37:04 -0800809static void link_add_to_outqueue(struct link *l_ptr,
810 struct sk_buff *buf,
811 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812{
813 u32 ack = mod(l_ptr->next_in_no - 1);
814 u32 seqno = mod(l_ptr->next_out_no++);
815
816 msg_set_word(msg, 2, ((ack << 16) | seqno));
817 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
818 buf->next = NULL;
819 if (l_ptr->first_out) {
820 l_ptr->last_out->next = buf;
821 l_ptr->last_out = buf;
822 } else
823 l_ptr->first_out = l_ptr->last_out = buf;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500824
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 l_ptr->out_queue_size++;
Allan Stephens9bd80b62011-01-18 15:02:50 -0500826 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
827 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828}
829
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900830/*
831 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 * inside TIPC when the 'fast path' in tipc_send_buf
833 * has failed, and from link_send()
834 */
835
Per Liden4323add2006-01-18 00:38:21 +0100836int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837{
838 struct tipc_msg *msg = buf_msg(buf);
839 u32 size = msg_size(msg);
840 u32 dsz = msg_data_sz(msg);
841 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000842 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000844 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845
846 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
847
848 /* Match msg importance against queue limits: */
849
850 if (unlikely(queue_size >= queue_limit)) {
851 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
852 return link_schedule_port(l_ptr, msg_origport(msg),
853 size);
854 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 buf_discard(buf);
856 if (imp > CONN_MANAGER) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700857 warn("Resetting link <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100858 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 }
860 return dsz;
861 }
862
863 /* Fragmentation needed ? */
864
865 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000866 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867
868 /* Packet can be queued or sent: */
869
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 !link_congested(l_ptr))) {
872 link_add_to_outqueue(l_ptr, buf, msg);
873
Per Liden4323add2006-01-18 00:38:21 +0100874 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 l_ptr->unacked_window = 0;
876 } else {
Per Liden4323add2006-01-18 00:38:21 +0100877 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 l_ptr->stats.bearer_congs++;
879 l_ptr->next_out = buf;
880 }
881 return dsz;
882 }
883 /* Congestion: can message be bundled ?: */
884
885 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
886 (msg_user(msg) != MSG_FRAGMENTER)) {
887
888 /* Try adding message to an existing bundle */
889
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900890 if (l_ptr->next_out &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +0100892 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 return dsz;
894 }
895
896 /* Try creating a new bundle */
897
898 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000899 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 struct tipc_msg bundler_hdr;
901
902 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000903 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700904 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300905 skb_copy_to_linear_data(bundler, &bundler_hdr,
906 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 skb_trim(bundler, INT_H_SIZE);
908 link_bundle_buf(l_ptr, bundler, buf);
909 buf = bundler;
910 msg = buf_msg(buf);
911 l_ptr->stats.sent_bundles++;
912 }
913 }
914 }
915 if (!l_ptr->next_out)
916 l_ptr->next_out = buf;
917 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +0100918 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 return dsz;
920}
921
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900922/*
923 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 * not been selected yet, and the the owner node is not locked
925 * Called by TIPC internal users, e.g. the name distributor
926 */
927
Per Liden4323add2006-01-18 00:38:21 +0100928int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929{
930 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700931 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 int res = -ELINKCONG;
933
Per Liden4323add2006-01-18 00:38:21 +0100934 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000935 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100937 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000939 if (l_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100940 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000941 else
Allan Stephensc33d53b2006-06-25 23:50:30 -0700942 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100943 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 buf_discard(buf);
946 }
Per Liden4323add2006-01-18 00:38:21 +0100947 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 return res;
949}
950
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900951/*
952 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953 * destination link is known and the header is complete,
954 * inclusive total message length. Very time critical.
955 * Link is locked. Returns user data length.
956 */
957
Sam Ravnborg05790c62006-03-20 22:37:04 -0800958static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
959 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960{
961 struct tipc_msg *msg = buf_msg(buf);
962 int res = msg_data_sz(msg);
963
964 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +0000965 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
967 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +0100968 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
969 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 return res;
972 }
Per Liden4323add2006-01-18 00:38:21 +0100973 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 l_ptr->stats.bearer_congs++;
975 l_ptr->next_out = buf;
976 return res;
977 }
Allan Stephens0e659672010-12-31 18:59:32 +0000978 } else
Allan Stephens15e979d2010-05-11 14:30:10 +0000979 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 }
Per Liden4323add2006-01-18 00:38:21 +0100981 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982}
983
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900984/*
985 * tipc_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986 * destination node is known and the header is complete,
987 * inclusive total message length.
988 * Returns user data length.
989 */
990int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
991{
992 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700993 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 int res;
995 u32 selector = msg_origport(buf_msg(buf)) & 1;
996 u32 dummy;
997
998 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +0100999 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000
Per Liden4323add2006-01-18 00:38:21 +01001001 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001002 n_ptr = tipc_node_find(destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001004 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 l_ptr = n_ptr->active_links[selector];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 if (likely(l_ptr)) {
1007 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001008 tipc_node_unlock(n_ptr);
1009 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 return res;
1011 }
Per Liden4323add2006-01-18 00:38:21 +01001012 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 }
Per Liden4323add2006-01-18 00:38:21 +01001014 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 res = msg_data_sz(buf_msg(buf));
1016 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1017 return res;
1018}
1019
1020
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021/*
1022 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 * Returns user data length or errno.
1026 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001027int tipc_link_send_sections_fast(struct tipc_port *sender,
Per Liden4323add2006-01-18 00:38:21 +01001028 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 const u32 num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001030 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001032 struct tipc_msg *hdr = &sender->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 struct link *l_ptr;
1034 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -07001035 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 int res;
1037 u32 selector = msg_origport(hdr) & 1;
1038
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039again:
1040 /*
1041 * Try building message using port's max_pkt hint.
1042 * (Must not hold any locks while building message.)
1043 */
1044
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001045 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 !sender->user_port, &buf);
1047
Per Liden4323add2006-01-18 00:38:21 +01001048 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001049 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001051 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 l_ptr = node->active_links[selector];
1053 if (likely(l_ptr)) {
1054 if (likely(buf)) {
1055 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001056 &sender->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 if (unlikely(res < 0))
1058 buf_discard(buf);
1059exit:
Per Liden4323add2006-01-18 00:38:21 +01001060 tipc_node_unlock(node);
1061 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 return res;
1063 }
1064
1065 /* Exit if build request was invalid */
1066
1067 if (unlikely(res < 0))
1068 goto exit;
1069
1070 /* Exit if link (or bearer) is congested */
1071
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001072 if (link_congested(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 !list_empty(&l_ptr->b_ptr->cong_links)) {
1074 res = link_schedule_port(l_ptr,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001075 sender->ref, res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 goto exit;
1077 }
1078
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001079 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080 * Message size exceeds max_pkt hint; update hint,
1081 * then re-try fast path or fragment the message
1082 */
1083
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001084 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001085 tipc_node_unlock(node);
1086 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087
1088
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001089 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 goto again;
1091
1092 return link_send_sections_long(sender, msg_sect,
1093 num_sect, destaddr);
1094 }
Per Liden4323add2006-01-18 00:38:21 +01001095 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 }
Per Liden4323add2006-01-18 00:38:21 +01001097 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098
1099 /* Couldn't find a link to the destination node */
1100
1101 if (buf)
1102 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1103 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001104 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1105 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 return res;
1107}
1108
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001109/*
1110 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001112 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113 * Link and bearer congestion status have been checked to be ok,
1114 * and are ignored if they change.
1115 *
1116 * Note that fragments do not use the full link MTU so that they won't have
1117 * to undergo refragmentation if link changeover causes them to be sent
1118 * over another link with an additional tunnel header added as prefix.
1119 * (Refragmentation will still occur if the other link has a smaller MTU.)
1120 *
1121 * Returns user data length or errno.
1122 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001123static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 struct iovec const *msg_sect,
1125 u32 num_sect,
1126 u32 destaddr)
1127{
1128 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001129 struct tipc_node *node;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001130 struct tipc_msg *hdr = &sender->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 u32 dsz = msg_data_sz(hdr);
Allan Stephens0e659672010-12-31 18:59:32 +00001132 u32 max_pkt, fragm_sz, rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 struct tipc_msg fragm_hdr;
Allan Stephens0e659672010-12-31 18:59:32 +00001134 struct sk_buff *buf, *buf_chain, *prev;
1135 u32 fragm_crs, fragm_rest, hsz, sect_rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 const unchar *sect_crs;
1137 int curr_sect;
1138 u32 fragm_no;
1139
1140again:
1141 fragm_no = 1;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001142 max_pkt = sender->max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001144 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 /* leave room for fragmentation header in each fragment */
1146 rest = dsz;
1147 fragm_crs = 0;
1148 fragm_rest = 0;
1149 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001150 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 curr_sect = -1;
1152
1153 /* Prepare reusable fragment header: */
1154
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001155 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001156 INT_H_SIZE, msg_destnode(hdr));
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001157 msg_set_link_selector(&fragm_hdr, sender->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 msg_set_size(&fragm_hdr, max_pkt);
1159 msg_set_fragm_no(&fragm_hdr, 1);
1160
1161 /* Prepare header of first fragment: */
1162
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001163 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 if (!buf)
1165 return -ENOMEM;
1166 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001167 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001169 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170
1171 /* Chop up message: */
1172
1173 fragm_crs = INT_H_SIZE + hsz;
1174 fragm_rest = fragm_sz - hsz;
1175
1176 do { /* For all sections */
1177 u32 sz;
1178
1179 if (!sect_rest) {
1180 sect_rest = msg_sect[++curr_sect].iov_len;
1181 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1182 }
1183
1184 if (sect_rest < fragm_rest)
1185 sz = sect_rest;
1186 else
1187 sz = fragm_rest;
1188
1189 if (likely(!sender->user_port)) {
1190 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1191error:
1192 for (; buf_chain; buf_chain = buf) {
1193 buf = buf_chain->next;
1194 buf_discard(buf_chain);
1195 }
1196 return -EFAULT;
1197 }
1198 } else
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001199 skb_copy_to_linear_data_offset(buf, fragm_crs,
1200 sect_crs, sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 sect_crs += sz;
1202 sect_rest -= sz;
1203 fragm_crs += sz;
1204 fragm_rest -= sz;
1205 rest -= sz;
1206
1207 if (!fragm_rest && rest) {
1208
1209 /* Initiate new fragment: */
1210 if (rest <= fragm_sz) {
1211 fragm_sz = rest;
Allan Stephens0e659672010-12-31 18:59:32 +00001212 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 } else {
1214 msg_set_type(&fragm_hdr, FRAGMENT);
1215 }
1216 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1217 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1218 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001219 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 if (!buf)
1221 goto error;
1222
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001223 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001225 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 fragm_crs = INT_H_SIZE;
1227 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 }
Allan Stephens0e659672010-12-31 18:59:32 +00001229 } while (rest > 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001231 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 * Now we have a buffer chain. Select a link and check
1233 * that packet size is still OK
1234 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001235 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001237 tipc_node_lock(node);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001238 l_ptr = node->active_links[sender->ref & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001240 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 goto reject;
1242 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001243 if (l_ptr->max_pkt < max_pkt) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001244 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001245 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 for (; buf_chain; buf_chain = buf) {
1247 buf = buf_chain->next;
1248 buf_discard(buf_chain);
1249 }
1250 goto again;
1251 }
1252 } else {
1253reject:
1254 for (; buf_chain; buf_chain = buf) {
1255 buf = buf_chain->next;
1256 buf_discard(buf_chain);
1257 }
Per Liden4323add2006-01-18 00:38:21 +01001258 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1259 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 }
1261
1262 /* Append whole chain to send queue: */
1263
1264 buf = buf_chain;
1265 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1266 if (!l_ptr->next_out)
1267 l_ptr->next_out = buf_chain;
1268 l_ptr->stats.sent_fragmented++;
1269 while (buf) {
1270 struct sk_buff *next = buf->next;
1271 struct tipc_msg *msg = buf_msg(buf);
1272
1273 l_ptr->stats.sent_fragments++;
1274 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1275 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 buf = next;
1277 }
1278
1279 /* Send it, if possible: */
1280
Per Liden4323add2006-01-18 00:38:21 +01001281 tipc_link_push_queue(l_ptr);
1282 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 return dsz;
1284}
1285
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001286/*
Per Liden4323add2006-01-18 00:38:21 +01001287 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 */
Per Liden4323add2006-01-18 00:38:21 +01001289u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290{
1291 struct sk_buff *buf = l_ptr->first_out;
1292 u32 r_q_size = l_ptr->retransm_queue_size;
1293 u32 r_q_head = l_ptr->retransm_queue_head;
1294
1295 /* Step to position where retransmission failed, if any, */
1296 /* consider that buffers may have been released in meantime */
1297
1298 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001299 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 link_last_sent(l_ptr));
1301 u32 first = msg_seqno(buf_msg(buf));
1302
1303 while (buf && less(first, r_q_head)) {
1304 first = mod(first + 1);
1305 buf = buf->next;
1306 }
1307 l_ptr->retransm_queue_head = r_q_head = first;
1308 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1309 }
1310
1311 /* Continue retransmission now, if there is anything: */
1312
Neil Hormanca509102010-03-15 07:58:45 +00001313 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001315 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001316 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 l_ptr->retransm_queue_head = mod(++r_q_head);
1318 l_ptr->retransm_queue_size = --r_q_size;
1319 l_ptr->stats.retransmitted++;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001320 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 } else {
1322 l_ptr->stats.bearer_congs++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 return PUSH_FAILED;
1324 }
1325 }
1326
1327 /* Send deferred protocol message, if any: */
1328
1329 buf = l_ptr->proto_msg_queue;
1330 if (buf) {
1331 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001332 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001333 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 l_ptr->unacked_window = 0;
1335 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001336 l_ptr->proto_msg_queue = NULL;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001337 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 l_ptr->stats.bearer_congs++;
1340 return PUSH_FAILED;
1341 }
1342 }
1343
1344 /* Send one deferred data message, if send window not full: */
1345
1346 buf = l_ptr->next_out;
1347 if (buf) {
1348 struct tipc_msg *msg = buf_msg(buf);
1349 u32 next = msg_seqno(msg);
1350 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1351
1352 if (mod(next - first) < l_ptr->queue_limit[0]) {
1353 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001354 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001355 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 if (msg_user(msg) == MSG_BUNDLER)
1357 msg_set_type(msg, CLOSED_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 l_ptr->next_out = buf->next;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001359 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 l_ptr->stats.bearer_congs++;
1362 return PUSH_FAILED;
1363 }
1364 }
1365 }
1366 return PUSH_FINISHED;
1367}
1368
1369/*
1370 * push_queue(): push out the unsent messages of a link where
1371 * congestion has abated. Node is locked
1372 */
Per Liden4323add2006-01-18 00:38:21 +01001373void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374{
1375 u32 res;
1376
Per Liden4323add2006-01-18 00:38:21 +01001377 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 return;
1379
1380 do {
Per Liden4323add2006-01-18 00:38:21 +01001381 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001382 } while (!res);
1383
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001385 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386}
1387
Allan Stephensd356eeb2006-06-25 23:40:01 -07001388static void link_reset_all(unsigned long addr)
1389{
David S. Miller6c000552008-09-02 23:38:32 -07001390 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001391 char addr_string[16];
1392 u32 i;
1393
1394 read_lock_bh(&tipc_net_lock);
1395 n_ptr = tipc_node_find((u32)addr);
1396 if (!n_ptr) {
1397 read_unlock_bh(&tipc_net_lock);
1398 return; /* node no longer exists */
1399 }
1400
1401 tipc_node_lock(n_ptr);
1402
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001403 warn("Resetting all links to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001404 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001405
1406 for (i = 0; i < MAX_BEARERS; i++) {
1407 if (n_ptr->links[i]) {
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001408 link_print(n_ptr->links[i], "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001409 tipc_link_reset(n_ptr->links[i]);
1410 }
1411 }
1412
1413 tipc_node_unlock(n_ptr);
1414 read_unlock_bh(&tipc_net_lock);
1415}
1416
1417static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1418{
1419 struct tipc_msg *msg = buf_msg(buf);
1420
1421 warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001422
1423 if (l_ptr->addr) {
1424
1425 /* Handle failure on standard link */
1426
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001427 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001428 tipc_link_reset(l_ptr);
1429
1430 } else {
1431
1432 /* Handle failure on broadcast link */
1433
David S. Miller6c000552008-09-02 23:38:32 -07001434 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001435 char addr_string[16];
1436
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001437 info("Msg seq number: %u, ", msg_seqno(msg));
1438 info("Outstanding acks: %lu\n",
1439 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001440
Allan Stephens01d83ed2011-01-18 13:53:16 -05001441 n_ptr = tipc_bclink_retransmit_to();
Allan Stephensd356eeb2006-06-25 23:40:01 -07001442 tipc_node_lock(n_ptr);
1443
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001444 tipc_addr_string_fill(addr_string, n_ptr->addr);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001445 info("Multicast link info for %s\n", addr_string);
1446 info("Supported: %d, ", n_ptr->bclink.supported);
1447 info("Acked: %u\n", n_ptr->bclink.acked);
1448 info("Last in: %u, ", n_ptr->bclink.last_in);
1449 info("Gap after: %u, ", n_ptr->bclink.gap_after);
1450 info("Gap to: %u\n", n_ptr->bclink.gap_to);
1451 info("Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001452
1453 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1454
1455 tipc_node_unlock(n_ptr);
1456
1457 l_ptr->stale_count = 0;
1458 }
1459}
1460
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001461void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001462 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463{
1464 struct tipc_msg *msg;
1465
Allan Stephensd356eeb2006-06-25 23:40:01 -07001466 if (!buf)
1467 return;
1468
1469 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001470
Allan Stephensd356eeb2006-06-25 23:40:01 -07001471 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Neil Hormanca509102010-03-15 07:58:45 +00001472 if (l_ptr->retransm_queue_size == 0) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001473 l_ptr->retransm_queue_head = msg_seqno(msg);
1474 l_ptr->retransm_queue_size = retransmits;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001475 } else {
Neil Hormanca509102010-03-15 07:58:45 +00001476 err("Unexpected retransmit on link %s (qsize=%d)\n",
1477 l_ptr->name, l_ptr->retransm_queue_size);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001478 }
Neil Hormanca509102010-03-15 07:58:45 +00001479 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001480 } else {
1481 /* Detect repeated retransmit failures on uncongested bearer */
1482
1483 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1484 if (++l_ptr->stale_count > 100) {
1485 link_retransmit_failure(l_ptr, buf);
1486 return;
1487 }
1488 } else {
1489 l_ptr->last_retransmitted = msg_seqno(msg);
1490 l_ptr->stale_count = 1;
1491 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001493
Neil Hormanca509102010-03-15 07:58:45 +00001494 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 msg = buf_msg(buf);
1496 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001497 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001498 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499 buf = buf->next;
1500 retransmits--;
1501 l_ptr->stats.retransmitted++;
1502 } else {
Per Liden4323add2006-01-18 00:38:21 +01001503 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 l_ptr->stats.bearer_congs++;
1505 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1506 l_ptr->retransm_queue_size = retransmits;
1507 return;
1508 }
1509 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001510
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1512}
1513
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001514/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515 * link_insert_deferred_queue - insert deferred messages back into receive chain
1516 */
1517
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001518static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519 struct sk_buff *buf)
1520{
1521 u32 seq_no;
1522
1523 if (l_ptr->oldest_deferred_in == NULL)
1524 return buf;
1525
1526 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1527 if (seq_no == mod(l_ptr->next_in_no)) {
1528 l_ptr->newest_deferred_in->next = buf;
1529 buf = l_ptr->oldest_deferred_in;
1530 l_ptr->oldest_deferred_in = NULL;
1531 l_ptr->deferred_inqueue_sz = 0;
1532 }
1533 return buf;
1534}
1535
Allan Stephens85035562008-04-15 19:04:54 -07001536/**
1537 * link_recv_buf_validate - validate basic format of received message
1538 *
1539 * This routine ensures a TIPC message has an acceptable header, and at least
1540 * as much data as the header indicates it should. The routine also ensures
1541 * that the entire message header is stored in the main fragment of the message
1542 * buffer, to simplify future access to message header fields.
1543 *
1544 * Note: Having extra info present in the message header or data areas is OK.
1545 * TIPC will ignore the excess, under the assumption that it is optional info
1546 * introduced by a later release of the protocol.
1547 */
1548
1549static int link_recv_buf_validate(struct sk_buff *buf)
1550{
1551 static u32 min_data_hdr_size[8] = {
1552 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1553 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1554 };
1555
1556 struct tipc_msg *msg;
1557 u32 tipc_hdr[2];
1558 u32 size;
1559 u32 hdr_size;
1560 u32 min_hdr_size;
1561
1562 if (unlikely(buf->len < MIN_H_SIZE))
1563 return 0;
1564
1565 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1566 if (msg == NULL)
1567 return 0;
1568
1569 if (unlikely(msg_version(msg) != TIPC_VERSION))
1570 return 0;
1571
1572 size = msg_size(msg);
1573 hdr_size = msg_hdr_sz(msg);
1574 min_hdr_size = msg_isdata(msg) ?
1575 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1576
1577 if (unlikely((hdr_size < min_hdr_size) ||
1578 (size < hdr_size) ||
1579 (buf->len < size) ||
1580 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1581 return 0;
1582
1583 return pskb_may_pull(buf, hdr_size);
1584}
1585
Allan Stephensb02b69c2010-08-17 11:00:07 +00001586/**
1587 * tipc_recv_msg - process TIPC messages arriving from off-node
1588 * @head: pointer to message buffer chain
1589 * @tb_ptr: pointer to bearer message arrived on
1590 *
1591 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1592 * structure (i.e. cannot be NULL), but bearer can be inactive.
1593 */
1594
Allan Stephens2d627b92011-01-07 13:00:11 -05001595void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596{
Per Liden4323add2006-01-18 00:38:21 +01001597 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 while (head) {
David S. Miller6c000552008-09-02 23:38:32 -07001599 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 struct link *l_ptr;
1601 struct sk_buff *crs;
1602 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001603 struct tipc_msg *msg;
1604 u32 seq_no;
1605 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 u32 released = 0;
1607 int type;
1608
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 head = head->next;
Allan Stephens85035562008-04-15 19:04:54 -07001610
Allan Stephensb02b69c2010-08-17 11:00:07 +00001611 /* Ensure bearer is still enabled */
1612
1613 if (unlikely(!b_ptr->active))
1614 goto cont;
1615
Allan Stephens85035562008-04-15 19:04:54 -07001616 /* Ensure message is well-formed */
1617
1618 if (unlikely(!link_recv_buf_validate(buf)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 goto cont;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620
Allan Stephensfe13dda2008-04-15 19:03:23 -07001621 /* Ensure message data is a single contiguous unit */
1622
Allan Stephensa0168922010-12-31 18:59:35 +00001623 if (unlikely(buf_linearize(buf)))
Allan Stephensfe13dda2008-04-15 19:03:23 -07001624 goto cont;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001625
Allan Stephens85035562008-04-15 19:04:54 -07001626 /* Handle arrival of a non-unicast link message */
1627
1628 msg = buf_msg(buf);
1629
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001631 if (msg_user(msg) == LINK_CONFIG)
1632 tipc_disc_recv_msg(buf, b_ptr);
1633 else
1634 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 continue;
1636 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001637
Allan Stephens26008242006-06-25 23:39:31 -07001638 if (unlikely(!msg_short(msg) &&
1639 (msg_destnode(msg) != tipc_own_addr)))
1640 goto cont;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001641
Neil Hormande586572010-03-08 12:43:56 -08001642 /* Discard non-routeable messages destined for another node */
1643
1644 if (unlikely(!msg_isdata(msg) &&
1645 (msg_destnode(msg) != tipc_own_addr))) {
1646 if ((msg_user(msg) != CONN_MANAGER) &&
1647 (msg_user(msg) != MSG_FRAGMENTER))
1648 goto cont;
1649 }
1650
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001651 /* Locate neighboring node that sent message */
Allan Stephens85035562008-04-15 19:04:54 -07001652
Per Liden4323add2006-01-18 00:38:21 +01001653 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 if (unlikely(!n_ptr))
1655 goto cont;
Per Liden4323add2006-01-18 00:38:21 +01001656 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001657
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001658 /* Don't talk to neighbor during cleanup after last session */
1659
1660 if (n_ptr->cleanup_required) {
1661 tipc_node_unlock(n_ptr);
1662 goto cont;
1663 }
1664
1665 /* Locate unicast link endpoint that should handle message */
1666
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 l_ptr = n_ptr->links[b_ptr->identity];
1668 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001669 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 goto cont;
1671 }
Allan Stephens85035562008-04-15 19:04:54 -07001672
1673 /* Validate message sequence number info */
1674
1675 seq_no = msg_seqno(msg);
1676 ackd = msg_ack(msg);
1677
1678 /* Release acked messages */
1679
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001681 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1682 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 }
1684
1685 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001686 while ((crs != l_ptr->next_out) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1688 struct sk_buff *next = crs->next;
1689
1690 buf_discard(crs);
1691 crs = next;
1692 released++;
1693 }
1694 if (released) {
1695 l_ptr->first_out = crs;
1696 l_ptr->out_queue_size -= released;
1697 }
Allan Stephens85035562008-04-15 19:04:54 -07001698
1699 /* Try sending any messages link endpoint has pending */
1700
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001702 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001704 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1706 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001707 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 }
1709
Allan Stephens85035562008-04-15 19:04:54 -07001710 /* Now (finally!) process the incoming message */
1711
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712protocol_check:
1713 if (likely(link_working_working(l_ptr))) {
1714 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1715 l_ptr->next_in_no++;
1716 if (unlikely(l_ptr->oldest_deferred_in))
1717 head = link_insert_deferred_queue(l_ptr,
1718 head);
1719 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1720deliver:
1721 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001722 tipc_node_unlock(n_ptr);
1723 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724 continue;
1725 }
1726 switch (msg_user(msg)) {
1727 case MSG_BUNDLER:
1728 l_ptr->stats.recv_bundles++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001729 l_ptr->stats.recv_bundled +=
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001731 tipc_node_unlock(n_ptr);
1732 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 continue;
1734 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001735 tipc_node_unlock(n_ptr);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001736 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 continue;
1738 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001739 tipc_node_unlock(n_ptr);
1740 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 continue;
1742 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001743 tipc_node_unlock(n_ptr);
1744 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 continue;
1746 case MSG_FRAGMENTER:
1747 l_ptr->stats.recv_fragments++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001748 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
Per Liden4323add2006-01-18 00:38:21 +01001749 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 l_ptr->stats.recv_fragmented++;
1751 goto deliver;
1752 }
1753 break;
1754 case CHANGEOVER_PROTOCOL:
1755 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001756 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757 msg = buf_msg(buf);
1758 seq_no = msg_seqno(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759 if (type == ORIGINAL_MSG)
1760 goto deliver;
1761 goto protocol_check;
1762 }
1763 break;
1764 }
1765 }
Per Liden4323add2006-01-18 00:38:21 +01001766 tipc_node_unlock(n_ptr);
1767 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 continue;
1769 }
1770 link_handle_out_of_seq_msg(l_ptr, buf);
1771 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001772 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001773 continue;
1774 }
1775
1776 if (msg_user(msg) == LINK_PROTOCOL) {
1777 link_recv_proto_msg(l_ptr, buf);
1778 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001779 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 continue;
1781 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1783
1784 if (link_working_working(l_ptr)) {
1785 /* Re-insert in front of queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 buf->next = head;
1787 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001788 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 continue;
1790 }
Per Liden4323add2006-01-18 00:38:21 +01001791 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792cont:
1793 buf_discard(buf);
1794 }
Per Liden4323add2006-01-18 00:38:21 +01001795 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796}
1797
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001798/*
1799 * link_defer_buf(): Sort a received out-of-sequence packet
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 * into the deferred reception queue.
1801 * Returns the increase of the queue length,i.e. 0 or 1
1802 */
1803
Per Liden4323add2006-01-18 00:38:21 +01001804u32 tipc_link_defer_pkt(struct sk_buff **head,
1805 struct sk_buff **tail,
1806 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001808 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 struct sk_buff *crs = *head;
1810 u32 seq_no = msg_seqno(buf_msg(buf));
1811
1812 buf->next = NULL;
1813
1814 /* Empty queue ? */
1815 if (*head == NULL) {
1816 *head = *tail = buf;
1817 return 1;
1818 }
1819
1820 /* Last ? */
1821 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1822 (*tail)->next = buf;
1823 *tail = buf;
1824 return 1;
1825 }
1826
1827 /* Scan through queue and sort it in */
1828 do {
1829 struct tipc_msg *msg = buf_msg(crs);
1830
1831 if (less(seq_no, msg_seqno(msg))) {
1832 buf->next = crs;
1833 if (prev)
1834 prev->next = buf;
1835 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001836 *head = buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 return 1;
1838 }
Allan Stephensa0168922010-12-31 18:59:35 +00001839 if (seq_no == msg_seqno(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 prev = crs;
1842 crs = crs->next;
Allan Stephens0e659672010-12-31 18:59:32 +00001843 } while (crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844
1845 /* Message is a duplicate of an existing message */
1846
1847 buf_discard(buf);
1848 return 0;
1849}
1850
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001851/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1853 */
1854
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001855static void link_handle_out_of_seq_msg(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 struct sk_buff *buf)
1857{
1858 u32 seq_no = msg_seqno(buf_msg(buf));
1859
1860 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1861 link_recv_proto_msg(l_ptr, buf);
1862 return;
1863 }
1864
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 /* Record OOS packet arrival (force mismatch on next timeout) */
1866
1867 l_ptr->checkpoint--;
1868
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001869 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 * Discard packet if a duplicate; otherwise add it to deferred queue
1871 * and notify peer of gap as per protocol specification
1872 */
1873
1874 if (less(seq_no, mod(l_ptr->next_in_no))) {
1875 l_ptr->stats.duplicates++;
1876 buf_discard(buf);
1877 return;
1878 }
1879
Per Liden4323add2006-01-18 00:38:21 +01001880 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1881 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 l_ptr->deferred_inqueue_sz++;
1883 l_ptr->stats.deferred_recv++;
1884 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01001885 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 } else
1887 l_ptr->stats.duplicates++;
1888}
1889
1890/*
1891 * Send protocol message to the other endpoint.
1892 */
Per Liden4323add2006-01-18 00:38:21 +01001893void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
1894 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001896 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001898 u32 msg_size = sizeof(l_ptr->proto_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899
1900 if (link_blocked(l_ptr))
1901 return;
1902 msg_set_type(msg, msg_typ);
1903 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001904 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01001905 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906
1907 if (msg_typ == STATE_MSG) {
1908 u32 next_sent = mod(l_ptr->next_out_no);
1909
Per Liden4323add2006-01-18 00:38:21 +01001910 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 return;
1912 if (l_ptr->next_out)
1913 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
1914 msg_set_next_sent(msg, next_sent);
1915 if (l_ptr->oldest_deferred_in) {
1916 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1917 gap = mod(rec - mod(l_ptr->next_in_no));
1918 }
1919 msg_set_seq_gap(msg, gap);
1920 if (gap)
1921 l_ptr->stats.sent_nacks++;
1922 msg_set_link_tolerance(msg, tolerance);
1923 msg_set_linkprio(msg, priority);
1924 msg_set_max_pkt(msg, ack_mtu);
1925 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1926 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001927 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928 u32 mtu = l_ptr->max_pkt;
1929
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001930 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931 link_working_working(l_ptr) &&
1932 l_ptr->fsm_msg_cnt) {
1933 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001934 if (l_ptr->max_pkt_probes == 10) {
1935 l_ptr->max_pkt_target = (msg_size - 4);
1936 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001938 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001940 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941
1942 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001943 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001944 l_ptr->stats.sent_states++;
1945 } else { /* RESET_MSG or ACTIVATE_MSG */
1946 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1947 msg_set_seq_gap(msg, 0);
1948 msg_set_next_sent(msg, 1);
1949 msg_set_link_tolerance(msg, l_ptr->tolerance);
1950 msg_set_linkprio(msg, l_ptr->priority);
1951 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1952 }
1953
Allan Stephensa0168922010-12-31 18:59:35 +00001954 if (tipc_node_has_redundant_links(l_ptr->owner))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955 msg_set_redundant_link(msg);
Allan Stephensa0168922010-12-31 18:59:35 +00001956 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957 msg_clear_redundant_link(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958 msg_set_linkprio(msg, l_ptr->priority);
1959
1960 /* Ensure sequence number will not fit : */
1961
1962 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1963
1964 /* Congestion? */
1965
Per Liden4323add2006-01-18 00:38:21 +01001966 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967 if (!l_ptr->proto_msg_queue) {
1968 l_ptr->proto_msg_queue =
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001969 tipc_buf_acquire(sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970 }
1971 buf = l_ptr->proto_msg_queue;
1972 if (!buf)
1973 return;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001974 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 return;
1976 }
1977 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
1978
1979 /* Message can be sent */
1980
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001981 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982 if (!buf)
1983 return;
1984
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001985 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001986 msg_set_size(buf_msg(buf), msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987
Per Liden4323add2006-01-18 00:38:21 +01001988 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989 l_ptr->unacked_window = 0;
1990 buf_discard(buf);
1991 return;
1992 }
1993
1994 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01001995 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001996 l_ptr->proto_msg_queue = buf;
1997 l_ptr->stats.bearer_congs++;
1998}
1999
2000/*
2001 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002002 * Note that network plane id propagates through the network, and may
2003 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004 */
2005
2006static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2007{
2008 u32 rec_gap = 0;
2009 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002010 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 u32 msg_tol;
2012 struct tipc_msg *msg = buf_msg(buf);
2013
Per Lidenb97bf3f2006-01-02 19:04:38 +01002014 if (link_blocked(l_ptr))
2015 goto exit;
2016
2017 /* record unnumbered packet arrival (force mismatch on next timeout) */
2018
2019 l_ptr->checkpoint--;
2020
2021 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2022 if (tipc_own_addr > msg_prevnode(msg))
2023 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2024
2025 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2026
2027 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002028
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07002030 if (!link_working_unknown(l_ptr) &&
2031 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002032 if (msg_session(msg) == l_ptr->peer_session)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033 break; /* duplicate: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034 }
2035 /* fall thru' */
2036 case ACTIVATE_MSG:
2037 /* Update link settings according other endpoint's values */
2038
2039 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2040
Allan Stephens2db99832010-12-31 18:59:33 +00002041 msg_tol = msg_link_tolerance(msg);
2042 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002043 link_set_supervision_props(l_ptr, msg_tol);
2044
2045 if (msg_linkprio(msg) > l_ptr->priority)
2046 l_ptr->priority = msg_linkprio(msg);
2047
2048 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002049 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002050 if (max_pkt_info < l_ptr->max_pkt_target)
2051 l_ptr->max_pkt_target = max_pkt_info;
2052 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2053 l_ptr->max_pkt = l_ptr->max_pkt_target;
2054 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002055 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056 }
2057 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2058
2059 link_state_event(l_ptr, msg_type(msg));
2060
2061 l_ptr->peer_session = msg_session(msg);
2062 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2063
2064 /* Synchronize broadcast sequence numbers */
Allan Stephensa0168922010-12-31 18:59:35 +00002065 if (!tipc_node_has_redundant_links(l_ptr->owner))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002067 break;
2068 case STATE_MSG:
2069
Allan Stephens2db99832010-12-31 18:59:33 +00002070 msg_tol = msg_link_tolerance(msg);
2071 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002073
2074 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075 (msg_linkprio(msg) != l_ptr->priority)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002076 warn("Resetting link <%s>, priority change %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2078 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002079 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080 break;
2081 }
2082 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2083 l_ptr->stats.recv_states++;
2084 if (link_reset_unknown(l_ptr))
2085 break;
2086
2087 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002088 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 mod(l_ptr->next_in_no));
2090 }
2091
2092 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002093 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002094 l_ptr->max_pkt = max_pkt_ack;
2095 l_ptr->max_pkt_probes = 0;
2096 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097
2098 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002099 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00002101 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002102 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002103 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104
2105 /* Protocol message before retransmits, reduce loss risk */
2106
Per Liden4323add2006-01-18 00:38:21 +01002107 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108
2109 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002110 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2111 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112 }
2113 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002115 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2116 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 }
2118 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119 }
2120exit:
2121 buf_discard(buf);
2122}
2123
2124
2125/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002126 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127 * another bearer. Owner node is locked.
2128 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002129static void tipc_link_tunnel(struct link *l_ptr,
2130 struct tipc_msg *tunnel_hdr,
2131 struct tipc_msg *msg,
2132 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002133{
2134 struct link *tunnel;
2135 struct sk_buff *buf;
2136 u32 length = msg_size(msg);
2137
2138 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002139 if (!tipc_link_is_up(tunnel)) {
2140 warn("Link changeover error, "
2141 "tunnel link no longer available\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002143 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002145 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002146 if (!buf) {
2147 warn("Link changeover error, "
2148 "unable to send tunnel msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002150 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002151 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2152 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01002153 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002154}
2155
2156
2157
2158/*
2159 * changeover(): Send whole message queue via the remaining link
2160 * Owner node is locked.
2161 */
2162
Per Liden4323add2006-01-18 00:38:21 +01002163void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164{
2165 u32 msgcount = l_ptr->out_queue_size;
2166 struct sk_buff *crs = l_ptr->first_out;
2167 struct link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002169 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002170
2171 if (!tunnel)
2172 return;
2173
Allan Stephens5392d642006-06-25 23:52:50 -07002174 if (!l_ptr->owner->permit_changeover) {
2175 warn("Link changeover error, "
2176 "peer did not permit changeover\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002177 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002178 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002180 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002181 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002182 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2183 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002184
Per Lidenb97bf3f2006-01-02 19:04:38 +01002185 if (!l_ptr->first_out) {
2186 struct sk_buff *buf;
2187
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002188 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002189 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002190 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002191 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002192 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002194 warn("Link changeover error, "
2195 "unable to send changeover msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 }
2197 return;
2198 }
Allan Stephensf1310722006-06-25 23:51:37 -07002199
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002200 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002201 l_ptr->owner->active_links[1]);
2202
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203 while (crs) {
2204 struct tipc_msg *msg = buf_msg(crs);
2205
2206 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002207 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00002208 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002209
Florian Westphald788d802007-08-02 19:28:06 -07002210 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002211 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00002212 msg_set_seqno(m, msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002213 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2214 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215 pos += align(msg_size(m));
2216 m = (struct tipc_msg *)pos;
2217 }
2218 } else {
Per Liden4323add2006-01-18 00:38:21 +01002219 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2220 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 }
2222 crs = crs->next;
2223 }
2224}
2225
Per Liden4323add2006-01-18 00:38:21 +01002226void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002227{
2228 struct sk_buff *iter;
2229 struct tipc_msg tunnel_hdr;
2230
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002231 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002232 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002233 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2234 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2235 iter = l_ptr->first_out;
2236 while (iter) {
2237 struct sk_buff *outbuf;
2238 struct tipc_msg *msg = buf_msg(iter);
2239 u32 length = msg_size(msg);
2240
2241 if (msg_user(msg) == MSG_BUNDLER)
2242 msg_set_type(msg, CLOSED_MSG);
2243 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002244 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002246 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002247 if (outbuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002248 warn("Link changeover error, "
2249 "unable to send duplicate msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002250 return;
2251 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002252 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2253 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2254 length);
Per Liden4323add2006-01-18 00:38:21 +01002255 tipc_link_send_buf(tunnel, outbuf);
2256 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002257 return;
2258 iter = iter->next;
2259 }
2260}
2261
2262
2263
2264/**
2265 * buf_extract - extracts embedded TIPC message from another message
2266 * @skb: encapsulating message buffer
2267 * @from_pos: offset to extract from
2268 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002269 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002270 * encapsulating message itself is left unchanged.
2271 */
2272
2273static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2274{
2275 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2276 u32 size = msg_size(msg);
2277 struct sk_buff *eb;
2278
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002279 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002280 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002281 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002282 return eb;
2283}
2284
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002285/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286 * link_recv_changeover_msg(): Receive tunneled packet sent
2287 * via other link. Node is locked. Return extracted buffer.
2288 */
2289
2290static int link_recv_changeover_msg(struct link **l_ptr,
2291 struct sk_buff **buf)
2292{
2293 struct sk_buff *tunnel_buf = *buf;
2294 struct link *dest_link;
2295 struct tipc_msg *msg;
2296 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2297 u32 msg_typ = msg_type(tunnel_msg);
2298 u32 msg_count = msg_msgcnt(tunnel_msg);
2299
2300 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Allan Stephensb29f1422010-12-31 18:59:25 +00002301 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002303 if (dest_link == *l_ptr) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002304 err("Unexpected changeover message on link <%s>\n",
Allan Stephensf1310722006-06-25 23:51:37 -07002305 (*l_ptr)->name);
2306 goto exit;
2307 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002308 *l_ptr = dest_link;
2309 msg = msg_get_wrapped(tunnel_msg);
2310
2311 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002312 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002313 goto exit;
Allan Stephens0e659672010-12-31 18:59:32 +00002314 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315 if (*buf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002316 warn("Link changeover error, duplicate msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002317 goto exit;
2318 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002319 buf_discard(tunnel_buf);
2320 return 1;
2321 }
2322
2323 /* First original message ?: */
2324
Per Liden4323add2006-01-18 00:38:21 +01002325 if (tipc_link_is_up(dest_link)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002326 info("Resetting link <%s>, changeover initiated by peer\n",
2327 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002328 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002329 dest_link->exp_msg_count = msg_count;
2330 if (!msg_count)
2331 goto exit;
2332 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002333 dest_link->exp_msg_count = msg_count;
2334 if (!msg_count)
2335 goto exit;
2336 }
2337
2338 /* Receive original message */
2339
2340 if (dest_link->exp_msg_count == 0) {
Allan Stephens5392d642006-06-25 23:52:50 -07002341 warn("Link switchover error, "
2342 "got too many tunnelled messages\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002343 goto exit;
2344 }
2345 dest_link->exp_msg_count--;
2346 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002347 goto exit;
2348 } else {
2349 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2350 if (*buf != NULL) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002351 buf_discard(tunnel_buf);
2352 return 1;
2353 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002354 warn("Link changeover error, original msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002355 }
2356 }
2357exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002358 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359 buf_discard(tunnel_buf);
2360 return 0;
2361}
2362
2363/*
2364 * Bundler functionality:
2365 */
Per Liden4323add2006-01-18 00:38:21 +01002366void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002367{
2368 u32 msgcount = msg_msgcnt(buf_msg(buf));
2369 u32 pos = INT_H_SIZE;
2370 struct sk_buff *obuf;
2371
Per Lidenb97bf3f2006-01-02 19:04:38 +01002372 while (msgcount--) {
2373 obuf = buf_extract(buf, pos);
2374 if (obuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002375 warn("Link unable to unbundle message(s)\n");
2376 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002377 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002378 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002379 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380 }
2381 buf_discard(buf);
2382}
2383
2384/*
2385 * Fragmentation/defragmentation:
2386 */
2387
2388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002389/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002390 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002391 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002392 * Returns user data length.
2393 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002394static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002395{
2396 struct tipc_msg *inmsg = buf_msg(buf);
2397 struct tipc_msg fragm_hdr;
2398 u32 insize = msg_size(inmsg);
2399 u32 dsz = msg_data_sz(inmsg);
2400 unchar *crs = buf->data;
2401 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002402 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002403 u32 fragm_sz = pack_sz - INT_H_SIZE;
2404 u32 fragm_no = 1;
Allan Stephens9c396a72008-06-04 17:36:58 -07002405 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002406
2407 if (msg_short(inmsg))
2408 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002409 else
2410 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002411
2412 if (msg_routed(inmsg))
2413 msg_set_prevnode(inmsg, tipc_own_addr);
2414
2415 /* Prepare reusable fragment header: */
2416
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002417 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002418 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002419 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2420 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2421 msg_set_fragm_no(&fragm_hdr, fragm_no);
2422 l_ptr->stats.sent_fragmented++;
2423
2424 /* Chop up message: */
2425
2426 while (rest > 0) {
2427 struct sk_buff *fragm;
2428
2429 if (rest <= fragm_sz) {
2430 fragm_sz = rest;
2431 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2432 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002433 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002434 if (fragm == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002435 warn("Link unable to fragment message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002436 dsz = -ENOMEM;
2437 goto exit;
2438 }
2439 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002440 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2441 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2442 fragm_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002443 /* Send queued messages first, if any: */
2444
2445 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002446 tipc_link_send_buf(l_ptr, fragm);
2447 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448 return dsz;
2449 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2450 rest -= fragm_sz;
2451 crs += fragm_sz;
2452 msg_set_type(&fragm_hdr, FRAGMENT);
2453 }
2454exit:
2455 buf_discard(buf);
2456 return dsz;
2457}
2458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002459/*
2460 * A pending message being re-assembled must store certain values
2461 * to handle subsequent fragments correctly. The following functions
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462 * help storing these values in unused, available fields in the
2463 * pending message. This makes dynamic memory allocation unecessary.
2464 */
2465
Sam Ravnborg05790c62006-03-20 22:37:04 -08002466static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002467{
2468 msg_set_seqno(buf_msg(buf), seqno);
2469}
2470
Sam Ravnborg05790c62006-03-20 22:37:04 -08002471static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472{
2473 return msg_ack(buf_msg(buf));
2474}
2475
Sam Ravnborg05790c62006-03-20 22:37:04 -08002476static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477{
2478 msg_set_ack(buf_msg(buf), sz);
2479}
2480
Sam Ravnborg05790c62006-03-20 22:37:04 -08002481static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002482{
2483 return msg_bcast_ack(buf_msg(buf));
2484}
2485
Sam Ravnborg05790c62006-03-20 22:37:04 -08002486static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002487{
2488 msg_set_bcast_ack(buf_msg(buf), exp);
2489}
2490
Sam Ravnborg05790c62006-03-20 22:37:04 -08002491static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002492{
2493 return msg_reroute_cnt(buf_msg(buf));
2494}
2495
Sam Ravnborg05790c62006-03-20 22:37:04 -08002496static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002497{
2498 msg_incr_reroute_cnt(buf_msg(buf));
2499}
2500
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002501/*
2502 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002503 * the reassembled buffer if message is complete.
2504 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002505int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
Per Liden4323add2006-01-18 00:38:21 +01002506 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002507{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002508 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002509 struct sk_buff *fbuf = *fb;
2510 struct tipc_msg *fragm = buf_msg(fbuf);
2511 struct sk_buff *pbuf = *pending;
2512 u32 long_msg_seq_no = msg_long_msgno(fragm);
2513
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002514 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002515
2516 /* Is there an incomplete message waiting for this fragment? */
2517
Joe Perchesf64f9e72009-11-29 16:55:45 -08002518 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2519 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002520 prev = pbuf;
2521 pbuf = pbuf->next;
2522 }
2523
2524 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2525 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2526 u32 msg_sz = msg_size(imsg);
2527 u32 fragm_sz = msg_data_sz(fragm);
2528 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2529 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2530 if (msg_type(imsg) == TIPC_MCAST_MSG)
2531 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2532 if (msg_size(imsg) > max) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002533 buf_discard(fbuf);
2534 return 0;
2535 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002536 pbuf = tipc_buf_acquire(msg_size(imsg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002537 if (pbuf != NULL) {
2538 pbuf->next = *pending;
2539 *pending = pbuf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002540 skb_copy_to_linear_data(pbuf, imsg,
2541 msg_data_sz(fragm));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002542 /* Prepare buffer for subsequent fragments. */
2543
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002544 set_long_msg_seqno(pbuf, long_msg_seq_no);
Allan Stephens0e659672010-12-31 18:59:32 +00002545 set_fragm_size(pbuf, fragm_sz);
2546 set_expected_frags(pbuf, exp_fragm_cnt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002547 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002548 warn("Link unable to reassemble fragmented message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002549 }
2550 buf_discard(fbuf);
2551 return 0;
2552 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2553 u32 dsz = msg_data_sz(fragm);
2554 u32 fsz = get_fragm_size(pbuf);
2555 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2556 u32 exp_frags = get_expected_frags(pbuf) - 1;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002557 skb_copy_to_linear_data_offset(pbuf, crs,
2558 msg_data(fragm), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559 buf_discard(fbuf);
2560
2561 /* Is message complete? */
2562
2563 if (exp_frags == 0) {
2564 if (prev)
2565 prev->next = pbuf->next;
2566 else
2567 *pending = pbuf->next;
2568 msg_reset_reroute_cnt(buf_msg(pbuf));
2569 *fb = pbuf;
2570 *m = buf_msg(pbuf);
2571 return 1;
2572 }
Allan Stephens0e659672010-12-31 18:59:32 +00002573 set_expected_frags(pbuf, exp_frags);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002574 return 0;
2575 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002576 buf_discard(fbuf);
2577 return 0;
2578}
2579
2580/**
2581 * link_check_defragm_bufs - flush stale incoming message fragments
2582 * @l_ptr: pointer to link
2583 */
2584
2585static void link_check_defragm_bufs(struct link *l_ptr)
2586{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002587 struct sk_buff *prev = NULL;
2588 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589 struct sk_buff *buf = l_ptr->defragm_buf;
2590
2591 if (!buf)
2592 return;
2593 if (!link_working_working(l_ptr))
2594 return;
2595 while (buf) {
2596 u32 cnt = get_timer_cnt(buf);
2597
2598 next = buf->next;
2599 if (cnt < 4) {
2600 incr_timer_cnt(buf);
2601 prev = buf;
2602 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002603 if (prev)
2604 prev->next = buf->next;
2605 else
2606 l_ptr->defragm_buf = buf->next;
2607 buf_discard(buf);
2608 }
2609 buf = next;
2610 }
2611}
2612
2613
2614
2615static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2616{
Allan Stephens5413b4c2011-01-18 13:24:55 -05002617 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2618 return;
2619
Per Lidenb97bf3f2006-01-02 19:04:38 +01002620 l_ptr->tolerance = tolerance;
2621 l_ptr->continuity_interval =
2622 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2623 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2624}
2625
2626
Per Liden4323add2006-01-18 00:38:21 +01002627void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002628{
2629 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002630 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2631 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2632 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2633 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002635 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2636 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2637 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2638 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002639 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002640 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2641 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2642 /* FRAGMENT and LAST_FRAGMENT packets */
2643 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2644}
2645
2646/**
2647 * link_find_link - locate link by name
2648 * @name - ptr to link name string
2649 * @node - ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002650 *
Per Liden4323add2006-01-18 00:38:21 +01002651 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002652 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002653 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002654 * Returns pointer to link (or 0 if invalid link name).
2655 */
2656
David S. Miller6c000552008-09-02 23:38:32 -07002657static struct link *link_find_link(const char *name, struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002658{
2659 struct link_name link_name_parts;
Allan Stephens2d627b92011-01-07 13:00:11 -05002660 struct tipc_bearer *b_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002661 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002662
2663 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002664 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002665
Per Liden4323add2006-01-18 00:38:21 +01002666 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002667 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002668 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002669
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002670 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002671 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002672 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002673
2674 l_ptr = (*node)->links[b_ptr->identity];
2675 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002676 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002677
2678 return l_ptr;
2679}
2680
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002681struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002682 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002683{
2684 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002685 u32 new_value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002686 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002687 struct tipc_node *node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002688 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002689
2690 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002691 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002692
2693 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2694 new_value = ntohl(args->value);
2695
Per Liden4323add2006-01-18 00:38:21 +01002696 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002697 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002698 (tipc_bclink_set_queue_limits(new_value) == 0))
2699 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002700 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002701 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002702 }
2703
Per Liden4323add2006-01-18 00:38:21 +01002704 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002705 l_ptr = link_find_link(args->name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002706 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002707 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002708 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002709 }
2710
Per Liden4323add2006-01-18 00:38:21 +01002711 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002712 res = -EINVAL;
2713 switch (cmd) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002714 case TIPC_CMD_SET_LINK_TOL:
2715 if ((new_value >= TIPC_MIN_LINK_TOL) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716 (new_value <= TIPC_MAX_LINK_TOL)) {
2717 link_set_supervision_props(l_ptr, new_value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002718 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002719 0, 0, new_value, 0, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002720 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002721 }
2722 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002723 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002724 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2725 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002726 l_ptr->priority = new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002727 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002728 0, 0, 0, new_value, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002729 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002730 }
2731 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002732 case TIPC_CMD_SET_LINK_WINDOW:
2733 if ((new_value >= TIPC_MIN_LINK_WIN) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002734 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002735 tipc_link_set_queue_limits(l_ptr, new_value);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002736 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002737 }
2738 break;
2739 }
Per Liden4323add2006-01-18 00:38:21 +01002740 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002741
Per Liden4323add2006-01-18 00:38:21 +01002742 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002743 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002744 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002745
Per Liden4323add2006-01-18 00:38:21 +01002746 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002747}
2748
2749/**
2750 * link_reset_statistics - reset link statistics
2751 * @l_ptr: pointer to link
2752 */
2753
2754static void link_reset_statistics(struct link *l_ptr)
2755{
2756 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2757 l_ptr->stats.sent_info = l_ptr->next_out_no;
2758 l_ptr->stats.recv_info = l_ptr->next_in_no;
2759}
2760
Per Liden4323add2006-01-18 00:38:21 +01002761struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002762{
2763 char *link_name;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002764 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002765 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002766
2767 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002768 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002769
2770 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002771 if (!strcmp(link_name, tipc_bclink_name)) {
2772 if (tipc_bclink_reset_stats())
2773 return tipc_cfg_reply_error_string("link not found");
2774 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002775 }
2776
Per Liden4323add2006-01-18 00:38:21 +01002777 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002778 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002779 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002780 read_unlock_bh(&tipc_net_lock);
2781 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002782 }
2783
Per Liden4323add2006-01-18 00:38:21 +01002784 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002785 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002786 tipc_node_unlock(node);
2787 read_unlock_bh(&tipc_net_lock);
2788 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002789}
2790
2791/**
2792 * percent - convert count to a percentage of total (rounding up or down)
2793 */
2794
2795static u32 percent(u32 count, u32 total)
2796{
2797 return (count * 100 + (total / 2)) / total;
2798}
2799
2800/**
Per Liden4323add2006-01-18 00:38:21 +01002801 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002802 * @name: link name
2803 * @buf: print buffer area
2804 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002805 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002806 * Returns length of print buffer data string (or 0 if error)
2807 */
2808
Per Liden4323add2006-01-18 00:38:21 +01002809static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002810{
2811 struct print_buf pb;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002812 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002813 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002814 char *status;
2815 u32 profile_total = 0;
2816
Per Liden4323add2006-01-18 00:38:21 +01002817 if (!strcmp(name, tipc_bclink_name))
2818 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002819
Per Liden4323add2006-01-18 00:38:21 +01002820 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002821
Per Liden4323add2006-01-18 00:38:21 +01002822 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002823 l_ptr = link_find_link(name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002824 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002825 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002826 return 0;
2827 }
Per Liden4323add2006-01-18 00:38:21 +01002828 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002829
Per Liden4323add2006-01-18 00:38:21 +01002830 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002831 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01002832 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002833 status = "STANDBY";
2834 else
2835 status = "DEFUNCT";
2836 tipc_printf(&pb, "Link <%s>\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002837 " %s MTU:%u Priority:%u Tolerance:%u ms"
2838 " Window:%u packets\n",
Allan Stephens15e979d2010-05-11 14:30:10 +00002839 l_ptr->name, status, l_ptr->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002840 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002841 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002842 l_ptr->next_in_no - l_ptr->stats.recv_info,
2843 l_ptr->stats.recv_fragments,
2844 l_ptr->stats.recv_fragmented,
2845 l_ptr->stats.recv_bundles,
2846 l_ptr->stats.recv_bundled);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002847 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002848 l_ptr->next_out_no - l_ptr->stats.sent_info,
2849 l_ptr->stats.sent_fragments,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002850 l_ptr->stats.sent_fragmented,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002851 l_ptr->stats.sent_bundles,
2852 l_ptr->stats.sent_bundled);
2853 profile_total = l_ptr->stats.msg_length_counts;
2854 if (!profile_total)
2855 profile_total = 1;
2856 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002857 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2858 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002859 l_ptr->stats.msg_length_counts,
2860 l_ptr->stats.msg_lengths_total / profile_total,
2861 percent(l_ptr->stats.msg_length_profile[0], profile_total),
2862 percent(l_ptr->stats.msg_length_profile[1], profile_total),
2863 percent(l_ptr->stats.msg_length_profile[2], profile_total),
2864 percent(l_ptr->stats.msg_length_profile[3], profile_total),
2865 percent(l_ptr->stats.msg_length_profile[4], profile_total),
2866 percent(l_ptr->stats.msg_length_profile[5], profile_total),
2867 percent(l_ptr->stats.msg_length_profile[6], profile_total));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002868 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002869 l_ptr->stats.recv_states,
2870 l_ptr->stats.recv_probes,
2871 l_ptr->stats.recv_nacks,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002872 l_ptr->stats.deferred_recv,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002873 l_ptr->stats.duplicates);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002874 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
2875 l_ptr->stats.sent_states,
2876 l_ptr->stats.sent_probes,
2877 l_ptr->stats.sent_nacks,
2878 l_ptr->stats.sent_acks,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002879 l_ptr->stats.retransmitted);
2880 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
2881 l_ptr->stats.bearer_congs,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002882 l_ptr->stats.link_congs,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002883 l_ptr->stats.max_queue_sz,
2884 l_ptr->stats.queue_sz_counts
2885 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
2886 : 0);
2887
Per Liden4323add2006-01-18 00:38:21 +01002888 tipc_node_unlock(node);
2889 read_unlock_bh(&tipc_net_lock);
2890 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002891}
2892
2893#define MAX_LINK_STATS_INFO 2000
2894
Per Liden4323add2006-01-18 00:38:21 +01002895struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002896{
2897 struct sk_buff *buf;
2898 struct tlv_desc *rep_tlv;
2899 int str_len;
2900
2901 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002902 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002903
Per Liden4323add2006-01-18 00:38:21 +01002904 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002905 if (!buf)
2906 return NULL;
2907
2908 rep_tlv = (struct tlv_desc *)buf->data;
2909
Per Liden4323add2006-01-18 00:38:21 +01002910 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
2911 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002912 if (!str_len) {
2913 buf_discard(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002914 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002915 }
2916
2917 skb_put(buf, TLV_SPACE(str_len));
2918 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2919
2920 return buf;
2921}
2922
Per Lidenb97bf3f2006-01-02 19:04:38 +01002923/**
Per Liden4323add2006-01-18 00:38:21 +01002924 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002925 * @dest: network address of destination node
2926 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002927 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002928 * If no active link can be found, uses default maximum packet size.
2929 */
2930
Per Liden4323add2006-01-18 00:38:21 +01002931u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002932{
David S. Miller6c000552008-09-02 23:38:32 -07002933 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002934 struct link *l_ptr;
2935 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002936
Per Lidenb97bf3f2006-01-02 19:04:38 +01002937 if (dest == tipc_own_addr)
2938 return MAX_MSG_SIZE;
2939
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002940 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002941 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002942 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002943 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002944 l_ptr = n_ptr->active_links[selector & 1];
2945 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002946 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002947 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002948 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002949 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002950 return res;
2951}
2952
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002953static void link_print(struct link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002954{
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002955 char print_area[256];
2956 struct print_buf pb;
2957 struct print_buf *buf = &pb;
2958
2959 tipc_printbuf_init(buf, print_area, sizeof(print_area));
2960
Per Lidenb97bf3f2006-01-02 19:04:38 +01002961 tipc_printf(buf, str);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002962 tipc_printf(buf, "Link %x<%s>:",
Allan Stephens2d627b92011-01-07 13:00:11 -05002963 l_ptr->addr, l_ptr->b_ptr->name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002964
2965#ifdef CONFIG_TIPC_DEBUG
2966 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
2967 goto print_state;
2968
Per Lidenb97bf3f2006-01-02 19:04:38 +01002969 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
2970 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
2971 tipc_printf(buf, "SQUE");
2972 if (l_ptr->first_out) {
2973 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
2974 if (l_ptr->next_out)
2975 tipc_printf(buf, "%u..",
2976 msg_seqno(buf_msg(l_ptr->next_out)));
Allan Stephensb82834e2010-05-11 14:30:04 +00002977 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002978 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
2979 msg_seqno(buf_msg(l_ptr->first_out)))
Joe Perchesf64f9e72009-11-29 16:55:45 -08002980 != (l_ptr->out_queue_size - 1)) ||
2981 (l_ptr->last_out->next != NULL)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002982 tipc_printf(buf, "\nSend queue inconsistency\n");
Allan Stephensc8a61b52011-01-18 13:31:32 -05002983 tipc_printf(buf, "first_out= %p ", l_ptr->first_out);
2984 tipc_printf(buf, "next_out= %p ", l_ptr->next_out);
2985 tipc_printf(buf, "last_out= %p ", l_ptr->last_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002986 }
2987 } else
2988 tipc_printf(buf, "[]");
2989 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
2990 if (l_ptr->oldest_deferred_in) {
2991 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2992 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
2993 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
2994 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
2995 tipc_printf(buf, ":RQSIZ(%u)",
2996 l_ptr->deferred_inqueue_sz);
2997 }
2998 }
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002999print_state:
3000#endif
3001
Per Lidenb97bf3f2006-01-02 19:04:38 +01003002 if (link_working_unknown(l_ptr))
3003 tipc_printf(buf, ":WU");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003004 else if (link_reset_reset(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003005 tipc_printf(buf, ":RR");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003006 else if (link_reset_unknown(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003007 tipc_printf(buf, ":RU");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003008 else if (link_working_working(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003009 tipc_printf(buf, ":WW");
3010 tipc_printf(buf, "\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003011
3012 tipc_printbuf_validate(buf);
3013 info("%s", print_area);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003014}
3015