blob: ef203a1581cd42f5567f05f6e5b908c505ddd4c7 [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
5 * Copyright (c) 2004-2007, 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);
93static int link_send_sections_long(struct port *sender,
94 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
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116 max_pkt = (l_ptr->b_ptr->publ.mtu & ~3);
117 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;
190 if ((if_local = strchr(addr_local, ':')) == NULL)
191 return 0;
192 *(if_local++) = 0;
193 if ((addr_peer = strchr(if_local, '-')) == NULL)
194 return 0;
195 *(addr_peer++) = 0;
196 if_local_len = addr_peer - if_local;
197 if ((if_peer = strchr(addr_peer, ':')) == NULL)
198 return 0;
199 *(if_peer++) = 0;
200 if_peer_len = strlen(if_peer) + 1;
201
202 /* validate component parts of link name */
203
204 if ((sscanf(addr_local, "%u.%u.%u%c",
205 &z_local, &c_local, &n_local, &dummy) != 3) ||
206 (sscanf(addr_peer, "%u.%u.%u%c",
207 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
208 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
209 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900210 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
211 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
213 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
214 return 0;
215
216 /* return link name components, if necessary */
217
218 if (name_parts) {
219 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
220 strcpy(name_parts->if_local, if_local);
221 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
222 strcpy(name_parts->if_peer, if_peer);
223 }
224 return 1;
225}
226
227/**
228 * link_timeout - handle expiration of link timer
229 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900230 *
Per Liden4323add2006-01-18 00:38:21 +0100231 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
232 * with tipc_link_delete(). (There is no risk that the node will be deleted by
233 * another thread because tipc_link_delete() always cancels the link timer before
234 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 */
236
237static void link_timeout(struct link *l_ptr)
238{
Per Liden4323add2006-01-18 00:38:21 +0100239 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240
241 /* update counters used in statistical profiling of send traffic */
242
243 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
244 l_ptr->stats.queue_sz_counts++;
245
246 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
247 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
248
249 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
Per Liden4323add2006-01-18 00:38:21 +0100303struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer,
304 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;
317 if_name = strchr(b_ptr->publ.name, ':') + 1;
318 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{
406 struct port *p_ptr;
407
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;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 p_ptr->publ.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{
428 struct port *p_ptr;
429 struct port *temp_p_ptr;
430 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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 spin_lock_bh(p_ptr->publ.lock);
446 p_ptr->publ.congested = 0;
447 p_ptr->wakeup(&p_ptr->publ);
448 win -= p_ptr->waiting_pkts;
449 spin_unlock_bh(p_ptr->publ.lock);
450 }
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)) {
615 if (event == TIMEOUT_EVT) {
616 link_set_timer(l_ptr, cont_intv);
617 }
618 return; /* Changeover going on */
619 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620
621 switch (l_ptr->state) {
622 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 switch (event) {
624 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 break;
627 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 if (l_ptr->next_in_no != l_ptr->checkpoint) {
629 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100630 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900631 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100632 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 l_ptr->fsm_msg_cnt++;
634 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100636 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 l_ptr->fsm_msg_cnt++;
638 }
639 link_set_timer(l_ptr, cont_intv);
640 break;
641 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 l_ptr->state = WORKING_UNKNOWN;
643 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100644 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 l_ptr->fsm_msg_cnt++;
646 link_set_timer(l_ptr, cont_intv / 4);
647 break;
648 case RESET_MSG:
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900649 info("Resetting link <%s>, requested by peer\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700650 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100651 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 l_ptr->state = RESET_RESET;
653 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100654 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 l_ptr->fsm_msg_cnt++;
656 link_set_timer(l_ptr, cont_intv);
657 break;
658 default:
659 err("Unknown link event %u in WW state\n", event);
660 }
661 break;
662 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 switch (event) {
664 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 l_ptr->state = WORKING_WORKING;
667 l_ptr->fsm_msg_cnt = 0;
668 link_set_timer(l_ptr, cont_intv);
669 break;
670 case RESET_MSG:
Allan Stephensa10bd922006-06-25 23:52:17 -0700671 info("Resetting link <%s>, requested by peer "
672 "while probing\n", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100673 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 l_ptr->state = RESET_RESET;
675 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100676 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677 l_ptr->fsm_msg_cnt++;
678 link_set_timer(l_ptr, cont_intv);
679 break;
680 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 l_ptr->state = WORKING_WORKING;
683 l_ptr->fsm_msg_cnt = 0;
684 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100685 if (tipc_bclink_acks_missing(l_ptr->owner)) {
686 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
687 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 l_ptr->fsm_msg_cnt++;
689 }
690 link_set_timer(l_ptr, cont_intv);
691 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100693 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 l_ptr->fsm_msg_cnt++;
695 link_set_timer(l_ptr, cont_intv / 4);
696 } else { /* Link has failed */
Allan Stephensa10bd922006-06-25 23:52:17 -0700697 warn("Resetting link <%s>, peer not responding\n",
698 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100699 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 l_ptr->state = RESET_UNKNOWN;
701 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100702 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
703 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704 l_ptr->fsm_msg_cnt++;
705 link_set_timer(l_ptr, cont_intv);
706 }
707 break;
708 default:
709 err("Unknown link event %u in WU state\n", event);
710 }
711 break;
712 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 switch (event) {
714 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 break;
716 case ACTIVATE_MSG:
717 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000718 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 l_ptr->state = WORKING_WORKING;
721 l_ptr->fsm_msg_cnt = 0;
722 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100723 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 l_ptr->fsm_msg_cnt++;
725 link_set_timer(l_ptr, cont_intv);
726 break;
727 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 l_ptr->state = RESET_RESET;
729 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100730 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 l_ptr->fsm_msg_cnt++;
732 link_set_timer(l_ptr, cont_intv);
733 break;
734 case STARTING_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 l_ptr->started = 1;
736 /* fall through */
737 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100738 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 l_ptr->fsm_msg_cnt++;
740 link_set_timer(l_ptr, cont_intv);
741 break;
742 default:
743 err("Unknown link event %u in RU state\n", event);
744 }
745 break;
746 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 switch (event) {
748 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 case ACTIVATE_MSG:
750 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000751 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 l_ptr->state = WORKING_WORKING;
754 l_ptr->fsm_msg_cnt = 0;
755 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100756 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 l_ptr->fsm_msg_cnt++;
758 link_set_timer(l_ptr, cont_intv);
759 break;
760 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 break;
762 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100763 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 l_ptr->fsm_msg_cnt++;
765 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 break;
767 default:
768 err("Unknown link event %u in RR state\n", event);
769 }
770 break;
771 default:
772 err("Unknown link state %u/%u\n", l_ptr->state, event);
773 }
774}
775
776/*
777 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900778 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 */
780
781static int link_bundle_buf(struct link *l_ptr,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900782 struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 struct sk_buff *buf)
784{
785 struct tipc_msg *bundler_msg = buf_msg(bundler);
786 struct tipc_msg *msg = buf_msg(buf);
787 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700788 u32 bundle_size = msg_size(bundler_msg);
789 u32 to_pos = align(bundle_size);
790 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791
792 if (msg_user(bundler_msg) != MSG_BUNDLER)
793 return 0;
794 if (msg_type(bundler_msg) != OPEN_MSG)
795 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700796 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000798 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700799 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800
Allan Stephense49060c2006-06-29 12:32:46 -0700801 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300802 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 msg_set_size(bundler_msg, to_pos + size);
804 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 buf_discard(buf);
806 l_ptr->stats.sent_bundled++;
807 return 1;
808}
809
Sam Ravnborg05790c62006-03-20 22:37:04 -0800810static void link_add_to_outqueue(struct link *l_ptr,
811 struct sk_buff *buf,
812 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813{
814 u32 ack = mod(l_ptr->next_in_no - 1);
815 u32 seqno = mod(l_ptr->next_out_no++);
816
817 msg_set_word(msg, 2, ((ack << 16) | seqno));
818 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
819 buf->next = NULL;
820 if (l_ptr->first_out) {
821 l_ptr->last_out->next = buf;
822 l_ptr->last_out = buf;
823 } else
824 l_ptr->first_out = l_ptr->last_out = buf;
825 l_ptr->out_queue_size++;
826}
827
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828/*
829 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * inside TIPC when the 'fast path' in tipc_send_buf
831 * has failed, and from link_send()
832 */
833
Per Liden4323add2006-01-18 00:38:21 +0100834int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835{
836 struct tipc_msg *msg = buf_msg(buf);
837 u32 size = msg_size(msg);
838 u32 dsz = msg_data_sz(msg);
839 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000840 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000842 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843
844 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
845
846 /* Match msg importance against queue limits: */
847
848 if (unlikely(queue_size >= queue_limit)) {
849 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
850 return link_schedule_port(l_ptr, msg_origport(msg),
851 size);
852 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 buf_discard(buf);
854 if (imp > CONN_MANAGER) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700855 warn("Resetting link <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100856 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 }
858 return dsz;
859 }
860
861 /* Fragmentation needed ? */
862
863 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000864 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865
866 /* Packet can be queued or sent: */
867
868 if (queue_size > l_ptr->stats.max_queue_sz)
869 l_ptr->stats.max_queue_sz = queue_size;
870
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900871 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 !link_congested(l_ptr))) {
873 link_add_to_outqueue(l_ptr, buf, msg);
874
Per Liden4323add2006-01-18 00:38:21 +0100875 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 l_ptr->unacked_window = 0;
877 } else {
Per Liden4323add2006-01-18 00:38:21 +0100878 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 l_ptr->stats.bearer_congs++;
880 l_ptr->next_out = buf;
881 }
882 return dsz;
883 }
884 /* Congestion: can message be bundled ?: */
885
886 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
887 (msg_user(msg) != MSG_FRAGMENTER)) {
888
889 /* Try adding message to an existing bundle */
890
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900891 if (l_ptr->next_out &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +0100893 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 return dsz;
895 }
896
897 /* Try creating a new bundle */
898
899 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000900 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 struct tipc_msg bundler_hdr;
902
903 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000904 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700905 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300906 skb_copy_to_linear_data(bundler, &bundler_hdr,
907 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 skb_trim(bundler, INT_H_SIZE);
909 link_bundle_buf(l_ptr, bundler, buf);
910 buf = bundler;
911 msg = buf_msg(buf);
912 l_ptr->stats.sent_bundles++;
913 }
914 }
915 }
916 if (!l_ptr->next_out)
917 l_ptr->next_out = buf;
918 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +0100919 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 return dsz;
921}
922
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900923/*
924 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 * not been selected yet, and the the owner node is not locked
926 * Called by TIPC internal users, e.g. the name distributor
927 */
928
Per Liden4323add2006-01-18 00:38:21 +0100929int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930{
931 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700932 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 int res = -ELINKCONG;
934
Per Liden4323add2006-01-18 00:38:21 +0100935 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000936 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100938 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 l_ptr = n_ptr->active_links[selector & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 if (l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100941 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensc33d53b2006-06-25 23:50:30 -0700942 } else {
Allan Stephensc33d53b2006-06-25 23:50:30 -0700943 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944 }
Per Liden4323add2006-01-18 00:38:21 +0100945 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 buf_discard(buf);
948 }
Per Liden4323add2006-01-18 00:38:21 +0100949 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 return res;
951}
952
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900953/*
954 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 * destination link is known and the header is complete,
956 * inclusive total message length. Very time critical.
957 * Link is locked. Returns user data length.
958 */
959
Sam Ravnborg05790c62006-03-20 22:37:04 -0800960static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
961 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962{
963 struct tipc_msg *msg = buf_msg(buf);
964 int res = msg_data_sz(msg);
965
966 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +0000967 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
969 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +0100970 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
971 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 return res;
974 }
Per Liden4323add2006-01-18 00:38:21 +0100975 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 l_ptr->stats.bearer_congs++;
977 l_ptr->next_out = buf;
978 return res;
979 }
Allan Stephens0e659672010-12-31 18:59:32 +0000980 } else
Allan Stephens15e979d2010-05-11 14:30:10 +0000981 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 }
Per Liden4323add2006-01-18 00:38:21 +0100983 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984}
985
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986/*
987 * tipc_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 * destination node is known and the header is complete,
989 * inclusive total message length.
990 * Returns user data length.
991 */
992int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
993{
994 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700995 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 int res;
997 u32 selector = msg_origport(buf_msg(buf)) & 1;
998 u32 dummy;
999
1000 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001001 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002
Per Liden4323add2006-01-18 00:38:21 +01001003 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001004 n_ptr = tipc_node_find(destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001006 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 l_ptr = n_ptr->active_links[selector];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 if (likely(l_ptr)) {
1009 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001010 tipc_node_unlock(n_ptr);
1011 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 return res;
1013 }
Per Liden4323add2006-01-18 00:38:21 +01001014 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 }
Per Liden4323add2006-01-18 00:38:21 +01001016 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 res = msg_data_sz(buf_msg(buf));
1018 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1019 return res;
1020}
1021
1022
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023/*
1024 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 * Returns user data length or errno.
1028 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029int tipc_link_send_sections_fast(struct port *sender,
Per Liden4323add2006-01-18 00:38:21 +01001030 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 const u32 num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001032 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033{
1034 struct tipc_msg *hdr = &sender->publ.phdr;
1035 struct link *l_ptr;
1036 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -07001037 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038 int res;
1039 u32 selector = msg_origport(hdr) & 1;
1040
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041again:
1042 /*
1043 * Try building message using port's max_pkt hint.
1044 * (Must not hold any locks while building message.)
1045 */
1046
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001047 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->publ.max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 !sender->user_port, &buf);
1049
Per Liden4323add2006-01-18 00:38:21 +01001050 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001051 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001053 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 l_ptr = node->active_links[selector];
1055 if (likely(l_ptr)) {
1056 if (likely(buf)) {
1057 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens05646c92007-06-10 17:25:24 -07001058 &sender->publ.max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 if (unlikely(res < 0))
1060 buf_discard(buf);
1061exit:
Per Liden4323add2006-01-18 00:38:21 +01001062 tipc_node_unlock(node);
1063 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 return res;
1065 }
1066
1067 /* Exit if build request was invalid */
1068
1069 if (unlikely(res < 0))
1070 goto exit;
1071
1072 /* Exit if link (or bearer) is congested */
1073
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001074 if (link_congested(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 !list_empty(&l_ptr->b_ptr->cong_links)) {
1076 res = link_schedule_port(l_ptr,
1077 sender->publ.ref, res);
1078 goto exit;
1079 }
1080
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001081 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 * Message size exceeds max_pkt hint; update hint,
1083 * then re-try fast path or fragment the message
1084 */
1085
Allan Stephens15e979d2010-05-11 14:30:10 +00001086 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001087 tipc_node_unlock(node);
1088 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089
1090
Allan Stephens05646c92007-06-10 17:25:24 -07001091 if ((msg_hdr_sz(hdr) + res) <= sender->publ.max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 goto again;
1093
1094 return link_send_sections_long(sender, msg_sect,
1095 num_sect, destaddr);
1096 }
Per Liden4323add2006-01-18 00:38:21 +01001097 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 }
Per Liden4323add2006-01-18 00:38:21 +01001099 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100
1101 /* Couldn't find a link to the destination node */
1102
1103 if (buf)
1104 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1105 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001106 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1107 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 return res;
1109}
1110
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001111/*
1112 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001114 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 * Link and bearer congestion status have been checked to be ok,
1116 * and are ignored if they change.
1117 *
1118 * Note that fragments do not use the full link MTU so that they won't have
1119 * to undergo refragmentation if link changeover causes them to be sent
1120 * over another link with an additional tunnel header added as prefix.
1121 * (Refragmentation will still occur if the other link has a smaller MTU.)
1122 *
1123 * Returns user data length or errno.
1124 */
1125static int link_send_sections_long(struct port *sender,
1126 struct iovec const *msg_sect,
1127 u32 num_sect,
1128 u32 destaddr)
1129{
1130 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001131 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 struct tipc_msg *hdr = &sender->publ.phdr;
1133 u32 dsz = msg_data_sz(hdr);
Allan Stephens0e659672010-12-31 18:59:32 +00001134 u32 max_pkt, fragm_sz, rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 struct tipc_msg fragm_hdr;
Allan Stephens0e659672010-12-31 18:59:32 +00001136 struct sk_buff *buf, *buf_chain, *prev;
1137 u32 fragm_crs, fragm_rest, hsz, sect_rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 const unchar *sect_crs;
1139 int curr_sect;
1140 u32 fragm_no;
1141
1142again:
1143 fragm_no = 1;
Allan Stephens05646c92007-06-10 17:25:24 -07001144 max_pkt = sender->publ.max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001146 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 /* leave room for fragmentation header in each fragment */
1148 rest = dsz;
1149 fragm_crs = 0;
1150 fragm_rest = 0;
1151 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001152 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 curr_sect = -1;
1154
1155 /* Prepare reusable fragment header: */
1156
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001157 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001158 INT_H_SIZE, msg_destnode(hdr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 msg_set_link_selector(&fragm_hdr, sender->publ.ref);
1160 msg_set_size(&fragm_hdr, max_pkt);
1161 msg_set_fragm_no(&fragm_hdr, 1);
1162
1163 /* Prepare header of first fragment: */
1164
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001165 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 if (!buf)
1167 return -ENOMEM;
1168 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001169 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001171 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172
1173 /* Chop up message: */
1174
1175 fragm_crs = INT_H_SIZE + hsz;
1176 fragm_rest = fragm_sz - hsz;
1177
1178 do { /* For all sections */
1179 u32 sz;
1180
1181 if (!sect_rest) {
1182 sect_rest = msg_sect[++curr_sect].iov_len;
1183 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1184 }
1185
1186 if (sect_rest < fragm_rest)
1187 sz = sect_rest;
1188 else
1189 sz = fragm_rest;
1190
1191 if (likely(!sender->user_port)) {
1192 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1193error:
1194 for (; buf_chain; buf_chain = buf) {
1195 buf = buf_chain->next;
1196 buf_discard(buf_chain);
1197 }
1198 return -EFAULT;
1199 }
1200 } else
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001201 skb_copy_to_linear_data_offset(buf, fragm_crs,
1202 sect_crs, sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 sect_crs += sz;
1204 sect_rest -= sz;
1205 fragm_crs += sz;
1206 fragm_rest -= sz;
1207 rest -= sz;
1208
1209 if (!fragm_rest && rest) {
1210
1211 /* Initiate new fragment: */
1212 if (rest <= fragm_sz) {
1213 fragm_sz = rest;
Allan Stephens0e659672010-12-31 18:59:32 +00001214 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 } else {
1216 msg_set_type(&fragm_hdr, FRAGMENT);
1217 }
1218 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1219 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1220 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001221 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 if (!buf)
1223 goto error;
1224
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001225 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001227 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 fragm_crs = INT_H_SIZE;
1229 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 }
Allan Stephens0e659672010-12-31 18:59:32 +00001231 } while (rest > 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001233 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 * Now we have a buffer chain. Select a link and check
1235 * that packet size is still OK
1236 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001237 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001239 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 l_ptr = node->active_links[sender->publ.ref & 1];
1241 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001242 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 goto reject;
1244 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001245 if (l_ptr->max_pkt < max_pkt) {
1246 sender->publ.max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001247 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 for (; buf_chain; buf_chain = buf) {
1249 buf = buf_chain->next;
1250 buf_discard(buf_chain);
1251 }
1252 goto again;
1253 }
1254 } else {
1255reject:
1256 for (; buf_chain; buf_chain = buf) {
1257 buf = buf_chain->next;
1258 buf_discard(buf_chain);
1259 }
Per Liden4323add2006-01-18 00:38:21 +01001260 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1261 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 }
1263
1264 /* Append whole chain to send queue: */
1265
1266 buf = buf_chain;
1267 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1268 if (!l_ptr->next_out)
1269 l_ptr->next_out = buf_chain;
1270 l_ptr->stats.sent_fragmented++;
1271 while (buf) {
1272 struct sk_buff *next = buf->next;
1273 struct tipc_msg *msg = buf_msg(buf);
1274
1275 l_ptr->stats.sent_fragments++;
1276 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1277 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 buf = next;
1279 }
1280
1281 /* Send it, if possible: */
1282
Per Liden4323add2006-01-18 00:38:21 +01001283 tipc_link_push_queue(l_ptr);
1284 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 return dsz;
1286}
1287
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001288/*
Per Liden4323add2006-01-18 00:38:21 +01001289 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 */
Per Liden4323add2006-01-18 00:38:21 +01001291u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292{
1293 struct sk_buff *buf = l_ptr->first_out;
1294 u32 r_q_size = l_ptr->retransm_queue_size;
1295 u32 r_q_head = l_ptr->retransm_queue_head;
1296
1297 /* Step to position where retransmission failed, if any, */
1298 /* consider that buffers may have been released in meantime */
1299
1300 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001301 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 link_last_sent(l_ptr));
1303 u32 first = msg_seqno(buf_msg(buf));
1304
1305 while (buf && less(first, r_q_head)) {
1306 first = mod(first + 1);
1307 buf = buf->next;
1308 }
1309 l_ptr->retransm_queue_head = r_q_head = first;
1310 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1311 }
1312
1313 /* Continue retransmission now, if there is anything: */
1314
Neil Hormanca509102010-03-15 07:58:45 +00001315 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001317 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001318 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 l_ptr->retransm_queue_head = mod(++r_q_head);
1320 l_ptr->retransm_queue_size = --r_q_size;
1321 l_ptr->stats.retransmitted++;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001322 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 } else {
1324 l_ptr->stats.bearer_congs++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 return PUSH_FAILED;
1326 }
1327 }
1328
1329 /* Send deferred protocol message, if any: */
1330
1331 buf = l_ptr->proto_msg_queue;
1332 if (buf) {
1333 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001334 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001335 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 l_ptr->unacked_window = 0;
1337 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001338 l_ptr->proto_msg_queue = NULL;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001339 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 l_ptr->stats.bearer_congs++;
1342 return PUSH_FAILED;
1343 }
1344 }
1345
1346 /* Send one deferred data message, if send window not full: */
1347
1348 buf = l_ptr->next_out;
1349 if (buf) {
1350 struct tipc_msg *msg = buf_msg(buf);
1351 u32 next = msg_seqno(msg);
1352 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1353
1354 if (mod(next - first) < l_ptr->queue_limit[0]) {
1355 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001356 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001357 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 if (msg_user(msg) == MSG_BUNDLER)
1359 msg_set_type(msg, CLOSED_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 l_ptr->next_out = buf->next;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001361 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 l_ptr->stats.bearer_congs++;
1364 return PUSH_FAILED;
1365 }
1366 }
1367 }
1368 return PUSH_FINISHED;
1369}
1370
1371/*
1372 * push_queue(): push out the unsent messages of a link where
1373 * congestion has abated. Node is locked
1374 */
Per Liden4323add2006-01-18 00:38:21 +01001375void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376{
1377 u32 res;
1378
Per Liden4323add2006-01-18 00:38:21 +01001379 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 return;
1381
1382 do {
Per Liden4323add2006-01-18 00:38:21 +01001383 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001384 } while (!res);
1385
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001387 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388}
1389
Allan Stephensd356eeb2006-06-25 23:40:01 -07001390static void link_reset_all(unsigned long addr)
1391{
David S. Miller6c000552008-09-02 23:38:32 -07001392 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001393 char addr_string[16];
1394 u32 i;
1395
1396 read_lock_bh(&tipc_net_lock);
1397 n_ptr = tipc_node_find((u32)addr);
1398 if (!n_ptr) {
1399 read_unlock_bh(&tipc_net_lock);
1400 return; /* node no longer exists */
1401 }
1402
1403 tipc_node_lock(n_ptr);
1404
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001405 warn("Resetting all links to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001406 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001407
1408 for (i = 0; i < MAX_BEARERS; i++) {
1409 if (n_ptr->links[i]) {
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001410 link_print(n_ptr->links[i], "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001411 tipc_link_reset(n_ptr->links[i]);
1412 }
1413 }
1414
1415 tipc_node_unlock(n_ptr);
1416 read_unlock_bh(&tipc_net_lock);
1417}
1418
1419static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1420{
1421 struct tipc_msg *msg = buf_msg(buf);
1422
1423 warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001424
1425 if (l_ptr->addr) {
1426
1427 /* Handle failure on standard link */
1428
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001429 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001430 tipc_link_reset(l_ptr);
1431
1432 } else {
1433
1434 /* Handle failure on broadcast link */
1435
David S. Miller6c000552008-09-02 23:38:32 -07001436 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001437 char addr_string[16];
1438
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001439 info("Msg seq number: %u, ", msg_seqno(msg));
1440 info("Outstanding acks: %lu\n",
1441 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001442
Allan Stephensd356eeb2006-06-25 23:40:01 -07001443 n_ptr = l_ptr->owner->next;
1444 tipc_node_lock(n_ptr);
1445
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001446 tipc_addr_string_fill(addr_string, n_ptr->addr);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001447 info("Multicast link info for %s\n", addr_string);
1448 info("Supported: %d, ", n_ptr->bclink.supported);
1449 info("Acked: %u\n", n_ptr->bclink.acked);
1450 info("Last in: %u, ", n_ptr->bclink.last_in);
1451 info("Gap after: %u, ", n_ptr->bclink.gap_after);
1452 info("Gap to: %u\n", n_ptr->bclink.gap_to);
1453 info("Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001454
1455 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1456
1457 tipc_node_unlock(n_ptr);
1458
1459 l_ptr->stale_count = 0;
1460 }
1461}
1462
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001463void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001464 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465{
1466 struct tipc_msg *msg;
1467
Allan Stephensd356eeb2006-06-25 23:40:01 -07001468 if (!buf)
1469 return;
1470
1471 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001472
Allan Stephensd356eeb2006-06-25 23:40:01 -07001473 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Neil Hormanca509102010-03-15 07:58:45 +00001474 if (l_ptr->retransm_queue_size == 0) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001475 l_ptr->retransm_queue_head = msg_seqno(msg);
1476 l_ptr->retransm_queue_size = retransmits;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001477 } else {
Neil Hormanca509102010-03-15 07:58:45 +00001478 err("Unexpected retransmit on link %s (qsize=%d)\n",
1479 l_ptr->name, l_ptr->retransm_queue_size);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001480 }
Neil Hormanca509102010-03-15 07:58:45 +00001481 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001482 } else {
1483 /* Detect repeated retransmit failures on uncongested bearer */
1484
1485 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1486 if (++l_ptr->stale_count > 100) {
1487 link_retransmit_failure(l_ptr, buf);
1488 return;
1489 }
1490 } else {
1491 l_ptr->last_retransmitted = msg_seqno(msg);
1492 l_ptr->stale_count = 1;
1493 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001495
Neil Hormanca509102010-03-15 07:58:45 +00001496 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 msg = buf_msg(buf);
1498 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001499 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001500 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 buf = buf->next;
1502 retransmits--;
1503 l_ptr->stats.retransmitted++;
1504 } else {
Per Liden4323add2006-01-18 00:38:21 +01001505 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506 l_ptr->stats.bearer_congs++;
1507 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1508 l_ptr->retransm_queue_size = retransmits;
1509 return;
1510 }
1511 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001512
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1514}
1515
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001516/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517 * link_insert_deferred_queue - insert deferred messages back into receive chain
1518 */
1519
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001520static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 struct sk_buff *buf)
1522{
1523 u32 seq_no;
1524
1525 if (l_ptr->oldest_deferred_in == NULL)
1526 return buf;
1527
1528 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1529 if (seq_no == mod(l_ptr->next_in_no)) {
1530 l_ptr->newest_deferred_in->next = buf;
1531 buf = l_ptr->oldest_deferred_in;
1532 l_ptr->oldest_deferred_in = NULL;
1533 l_ptr->deferred_inqueue_sz = 0;
1534 }
1535 return buf;
1536}
1537
Allan Stephens85035562008-04-15 19:04:54 -07001538/**
1539 * link_recv_buf_validate - validate basic format of received message
1540 *
1541 * This routine ensures a TIPC message has an acceptable header, and at least
1542 * as much data as the header indicates it should. The routine also ensures
1543 * that the entire message header is stored in the main fragment of the message
1544 * buffer, to simplify future access to message header fields.
1545 *
1546 * Note: Having extra info present in the message header or data areas is OK.
1547 * TIPC will ignore the excess, under the assumption that it is optional info
1548 * introduced by a later release of the protocol.
1549 */
1550
1551static int link_recv_buf_validate(struct sk_buff *buf)
1552{
1553 static u32 min_data_hdr_size[8] = {
1554 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1555 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1556 };
1557
1558 struct tipc_msg *msg;
1559 u32 tipc_hdr[2];
1560 u32 size;
1561 u32 hdr_size;
1562 u32 min_hdr_size;
1563
1564 if (unlikely(buf->len < MIN_H_SIZE))
1565 return 0;
1566
1567 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1568 if (msg == NULL)
1569 return 0;
1570
1571 if (unlikely(msg_version(msg) != TIPC_VERSION))
1572 return 0;
1573
1574 size = msg_size(msg);
1575 hdr_size = msg_hdr_sz(msg);
1576 min_hdr_size = msg_isdata(msg) ?
1577 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1578
1579 if (unlikely((hdr_size < min_hdr_size) ||
1580 (size < hdr_size) ||
1581 (buf->len < size) ||
1582 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1583 return 0;
1584
1585 return pskb_may_pull(buf, hdr_size);
1586}
1587
Allan Stephensb02b69c2010-08-17 11:00:07 +00001588/**
1589 * tipc_recv_msg - process TIPC messages arriving from off-node
1590 * @head: pointer to message buffer chain
1591 * @tb_ptr: pointer to bearer message arrived on
1592 *
1593 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1594 * structure (i.e. cannot be NULL), but bearer can be inactive.
1595 */
1596
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr)
1598{
Per Liden4323add2006-01-18 00:38:21 +01001599 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 while (head) {
Allan Stephens1265a022008-06-04 17:32:35 -07001601 struct bearer *b_ptr = (struct bearer *)tb_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001602 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603 struct link *l_ptr;
1604 struct sk_buff *crs;
1605 struct sk_buff *buf = head;
Allan Stephens85035562008-04-15 19:04:54 -07001606 struct tipc_msg *msg;
1607 u32 seq_no;
1608 u32 ackd;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 u32 released = 0;
1610 int type;
1611
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612 head = head->next;
Allan Stephens85035562008-04-15 19:04:54 -07001613
Allan Stephensb02b69c2010-08-17 11:00:07 +00001614 /* Ensure bearer is still enabled */
1615
1616 if (unlikely(!b_ptr->active))
1617 goto cont;
1618
Allan Stephens85035562008-04-15 19:04:54 -07001619 /* Ensure message is well-formed */
1620
1621 if (unlikely(!link_recv_buf_validate(buf)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 goto cont;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623
Allan Stephensfe13dda2008-04-15 19:03:23 -07001624 /* Ensure message data is a single contiguous unit */
1625
1626 if (unlikely(buf_linearize(buf))) {
1627 goto cont;
1628 }
1629
Allan Stephens85035562008-04-15 19:04:54 -07001630 /* Handle arrival of a non-unicast link message */
1631
1632 msg = buf_msg(buf);
1633
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001635 if (msg_user(msg) == LINK_CONFIG)
1636 tipc_disc_recv_msg(buf, b_ptr);
1637 else
1638 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 continue;
1640 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001641
Allan Stephens26008242006-06-25 23:39:31 -07001642 if (unlikely(!msg_short(msg) &&
1643 (msg_destnode(msg) != tipc_own_addr)))
1644 goto cont;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001645
Neil Hormande586572010-03-08 12:43:56 -08001646 /* Discard non-routeable messages destined for another node */
1647
1648 if (unlikely(!msg_isdata(msg) &&
1649 (msg_destnode(msg) != tipc_own_addr))) {
1650 if ((msg_user(msg) != CONN_MANAGER) &&
1651 (msg_user(msg) != MSG_FRAGMENTER))
1652 goto cont;
1653 }
1654
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001655 /* Locate neighboring node that sent message */
Allan Stephens85035562008-04-15 19:04:54 -07001656
Per Liden4323add2006-01-18 00:38:21 +01001657 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 if (unlikely(!n_ptr))
1659 goto cont;
Per Liden4323add2006-01-18 00:38:21 +01001660 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001661
Allan Stephens5a68d5e2010-08-17 11:00:16 +00001662 /* Don't talk to neighbor during cleanup after last session */
1663
1664 if (n_ptr->cleanup_required) {
1665 tipc_node_unlock(n_ptr);
1666 goto cont;
1667 }
1668
1669 /* Locate unicast link endpoint that should handle message */
1670
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 l_ptr = n_ptr->links[b_ptr->identity];
1672 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001673 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 goto cont;
1675 }
Allan Stephens85035562008-04-15 19:04:54 -07001676
1677 /* Validate message sequence number info */
1678
1679 seq_no = msg_seqno(msg);
1680 ackd = msg_ack(msg);
1681
1682 /* Release acked messages */
1683
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001685 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1686 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 }
1688
1689 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001690 while ((crs != l_ptr->next_out) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1692 struct sk_buff *next = crs->next;
1693
1694 buf_discard(crs);
1695 crs = next;
1696 released++;
1697 }
1698 if (released) {
1699 l_ptr->first_out = crs;
1700 l_ptr->out_queue_size -= released;
1701 }
Allan Stephens85035562008-04-15 19:04:54 -07001702
1703 /* Try sending any messages link endpoint has pending */
1704
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001706 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001708 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1710 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001711 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 }
1713
Allan Stephens85035562008-04-15 19:04:54 -07001714 /* Now (finally!) process the incoming message */
1715
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716protocol_check:
1717 if (likely(link_working_working(l_ptr))) {
1718 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1719 l_ptr->next_in_no++;
1720 if (unlikely(l_ptr->oldest_deferred_in))
1721 head = link_insert_deferred_queue(l_ptr,
1722 head);
1723 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1724deliver:
1725 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001726 tipc_node_unlock(n_ptr);
1727 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 continue;
1729 }
1730 switch (msg_user(msg)) {
1731 case MSG_BUNDLER:
1732 l_ptr->stats.recv_bundles++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001733 l_ptr->stats.recv_bundled +=
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001735 tipc_node_unlock(n_ptr);
1736 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 continue;
1738 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001739 tipc_node_unlock(n_ptr);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001740 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 continue;
1742 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001743 tipc_node_unlock(n_ptr);
1744 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 continue;
1746 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001747 tipc_node_unlock(n_ptr);
1748 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 continue;
1750 case MSG_FRAGMENTER:
1751 l_ptr->stats.recv_fragments++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001752 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
Per Liden4323add2006-01-18 00:38:21 +01001753 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 l_ptr->stats.recv_fragmented++;
1755 goto deliver;
1756 }
1757 break;
1758 case CHANGEOVER_PROTOCOL:
1759 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001760 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001761 msg = buf_msg(buf);
1762 seq_no = msg_seqno(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763 if (type == ORIGINAL_MSG)
1764 goto deliver;
1765 goto protocol_check;
1766 }
1767 break;
1768 }
1769 }
Per Liden4323add2006-01-18 00:38:21 +01001770 tipc_node_unlock(n_ptr);
1771 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 continue;
1773 }
1774 link_handle_out_of_seq_msg(l_ptr, buf);
1775 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001776 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 continue;
1778 }
1779
1780 if (msg_user(msg) == LINK_PROTOCOL) {
1781 link_recv_proto_msg(l_ptr, buf);
1782 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001783 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 continue;
1785 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1787
1788 if (link_working_working(l_ptr)) {
1789 /* Re-insert in front of queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790 buf->next = head;
1791 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001792 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 continue;
1794 }
Per Liden4323add2006-01-18 00:38:21 +01001795 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796cont:
1797 buf_discard(buf);
1798 }
Per Liden4323add2006-01-18 00:38:21 +01001799 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800}
1801
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001802/*
1803 * link_defer_buf(): Sort a received out-of-sequence packet
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804 * into the deferred reception queue.
1805 * Returns the increase of the queue length,i.e. 0 or 1
1806 */
1807
Per Liden4323add2006-01-18 00:38:21 +01001808u32 tipc_link_defer_pkt(struct sk_buff **head,
1809 struct sk_buff **tail,
1810 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001812 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813 struct sk_buff *crs = *head;
1814 u32 seq_no = msg_seqno(buf_msg(buf));
1815
1816 buf->next = NULL;
1817
1818 /* Empty queue ? */
1819 if (*head == NULL) {
1820 *head = *tail = buf;
1821 return 1;
1822 }
1823
1824 /* Last ? */
1825 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1826 (*tail)->next = buf;
1827 *tail = buf;
1828 return 1;
1829 }
1830
1831 /* Scan through queue and sort it in */
1832 do {
1833 struct tipc_msg *msg = buf_msg(crs);
1834
1835 if (less(seq_no, msg_seqno(msg))) {
1836 buf->next = crs;
1837 if (prev)
1838 prev->next = buf;
1839 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001840 *head = buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 return 1;
1842 }
1843 if (seq_no == msg_seqno(msg)) {
1844 break;
1845 }
1846 prev = crs;
1847 crs = crs->next;
Allan Stephens0e659672010-12-31 18:59:32 +00001848 } while (crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849
1850 /* Message is a duplicate of an existing message */
1851
1852 buf_discard(buf);
1853 return 0;
1854}
1855
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001856/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1858 */
1859
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001860static void link_handle_out_of_seq_msg(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 struct sk_buff *buf)
1862{
1863 u32 seq_no = msg_seqno(buf_msg(buf));
1864
1865 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1866 link_recv_proto_msg(l_ptr, buf);
1867 return;
1868 }
1869
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 /* Record OOS packet arrival (force mismatch on next timeout) */
1871
1872 l_ptr->checkpoint--;
1873
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001874 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 * Discard packet if a duplicate; otherwise add it to deferred queue
1876 * and notify peer of gap as per protocol specification
1877 */
1878
1879 if (less(seq_no, mod(l_ptr->next_in_no))) {
1880 l_ptr->stats.duplicates++;
1881 buf_discard(buf);
1882 return;
1883 }
1884
Per Liden4323add2006-01-18 00:38:21 +01001885 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1886 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 l_ptr->deferred_inqueue_sz++;
1888 l_ptr->stats.deferred_recv++;
1889 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01001890 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 } else
1892 l_ptr->stats.duplicates++;
1893}
1894
1895/*
1896 * Send protocol message to the other endpoint.
1897 */
Per Liden4323add2006-01-18 00:38:21 +01001898void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
1899 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001901 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001903 u32 msg_size = sizeof(l_ptr->proto_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904
1905 if (link_blocked(l_ptr))
1906 return;
1907 msg_set_type(msg, msg_typ);
1908 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001909 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01001910 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911
1912 if (msg_typ == STATE_MSG) {
1913 u32 next_sent = mod(l_ptr->next_out_no);
1914
Per Liden4323add2006-01-18 00:38:21 +01001915 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 return;
1917 if (l_ptr->next_out)
1918 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
1919 msg_set_next_sent(msg, next_sent);
1920 if (l_ptr->oldest_deferred_in) {
1921 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1922 gap = mod(rec - mod(l_ptr->next_in_no));
1923 }
1924 msg_set_seq_gap(msg, gap);
1925 if (gap)
1926 l_ptr->stats.sent_nacks++;
1927 msg_set_link_tolerance(msg, tolerance);
1928 msg_set_linkprio(msg, priority);
1929 msg_set_max_pkt(msg, ack_mtu);
1930 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1931 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001932 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 u32 mtu = l_ptr->max_pkt;
1934
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001935 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936 link_working_working(l_ptr) &&
1937 l_ptr->fsm_msg_cnt) {
1938 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001939 if (l_ptr->max_pkt_probes == 10) {
1940 l_ptr->max_pkt_target = (msg_size - 4);
1941 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001943 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001944 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001945 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946
1947 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001948 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949 l_ptr->stats.sent_states++;
1950 } else { /* RESET_MSG or ACTIVATE_MSG */
1951 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1952 msg_set_seq_gap(msg, 0);
1953 msg_set_next_sent(msg, 1);
1954 msg_set_link_tolerance(msg, l_ptr->tolerance);
1955 msg_set_linkprio(msg, l_ptr->priority);
1956 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1957 }
1958
Per Liden4323add2006-01-18 00:38:21 +01001959 if (tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960 msg_set_redundant_link(msg);
1961 } else {
1962 msg_clear_redundant_link(msg);
1963 }
1964 msg_set_linkprio(msg, l_ptr->priority);
1965
1966 /* Ensure sequence number will not fit : */
1967
1968 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1969
1970 /* Congestion? */
1971
Per Liden4323add2006-01-18 00:38:21 +01001972 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973 if (!l_ptr->proto_msg_queue) {
1974 l_ptr->proto_msg_queue =
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001975 tipc_buf_acquire(sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976 }
1977 buf = l_ptr->proto_msg_queue;
1978 if (!buf)
1979 return;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001980 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981 return;
1982 }
1983 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
1984
1985 /* Message can be sent */
1986
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001987 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988 if (!buf)
1989 return;
1990
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001991 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001992 msg_set_size(buf_msg(buf), msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001993
Per Liden4323add2006-01-18 00:38:21 +01001994 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001995 l_ptr->unacked_window = 0;
1996 buf_discard(buf);
1997 return;
1998 }
1999
2000 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01002001 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002002 l_ptr->proto_msg_queue = buf;
2003 l_ptr->stats.bearer_congs++;
2004}
2005
2006/*
2007 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002008 * Note that network plane id propagates through the network, and may
2009 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01002010 */
2011
2012static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2013{
2014 u32 rec_gap = 0;
2015 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002016 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002017 u32 msg_tol;
2018 struct tipc_msg *msg = buf_msg(buf);
2019
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 if (link_blocked(l_ptr))
2021 goto exit;
2022
2023 /* record unnumbered packet arrival (force mismatch on next timeout) */
2024
2025 l_ptr->checkpoint--;
2026
2027 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2028 if (tipc_own_addr > msg_prevnode(msg))
2029 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2030
2031 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2032
2033 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002034
Per Lidenb97bf3f2006-01-02 19:04:38 +01002035 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07002036 if (!link_working_unknown(l_ptr) &&
2037 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002038 if (msg_session(msg) == l_ptr->peer_session)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 break; /* duplicate: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040 }
2041 /* fall thru' */
2042 case ACTIVATE_MSG:
2043 /* Update link settings according other endpoint's values */
2044
2045 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2046
2047 if ((msg_tol = msg_link_tolerance(msg)) &&
2048 (msg_tol > l_ptr->tolerance))
2049 link_set_supervision_props(l_ptr, msg_tol);
2050
2051 if (msg_linkprio(msg) > l_ptr->priority)
2052 l_ptr->priority = msg_linkprio(msg);
2053
2054 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002055 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056 if (max_pkt_info < l_ptr->max_pkt_target)
2057 l_ptr->max_pkt_target = max_pkt_info;
2058 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2059 l_ptr->max_pkt = l_ptr->max_pkt_target;
2060 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002061 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062 }
2063 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2064
2065 link_state_event(l_ptr, msg_type(msg));
2066
2067 l_ptr->peer_session = msg_session(msg);
2068 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2069
2070 /* Synchronize broadcast sequence numbers */
Per Liden4323add2006-01-18 00:38:21 +01002071 if (!tipc_node_has_redundant_links(l_ptr->owner)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2073 }
2074 break;
2075 case STATE_MSG:
2076
2077 if ((msg_tol = msg_link_tolerance(msg)))
2078 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002079
2080 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081 (msg_linkprio(msg) != l_ptr->priority)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002082 warn("Resetting link <%s>, priority change %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2084 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002085 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086 break;
2087 }
2088 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2089 l_ptr->stats.recv_states++;
2090 if (link_reset_unknown(l_ptr))
2091 break;
2092
2093 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002094 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 mod(l_ptr->next_in_no));
2096 }
2097
2098 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002099 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002100 l_ptr->max_pkt = max_pkt_ack;
2101 l_ptr->max_pkt_probes = 0;
2102 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103
2104 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002105 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 l_ptr->stats.recv_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002107 if (msg_size(msg) > sizeof(l_ptr->proto_msg)) {
2108 max_pkt_ack = msg_size(msg);
2109 }
2110 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002111
2112 /* Protocol message before retransmits, reduce loss risk */
2113
Per Liden4323add2006-01-18 00:38:21 +01002114 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115
2116 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002117 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2118 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119 }
2120 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002122 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2123 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 }
2125 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 }
2127exit:
2128 buf_discard(buf);
2129}
2130
2131
2132/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002133 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134 * another bearer. Owner node is locked.
2135 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002136static void tipc_link_tunnel(struct link *l_ptr,
2137 struct tipc_msg *tunnel_hdr,
2138 struct tipc_msg *msg,
2139 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140{
2141 struct link *tunnel;
2142 struct sk_buff *buf;
2143 u32 length = msg_size(msg);
2144
2145 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002146 if (!tipc_link_is_up(tunnel)) {
2147 warn("Link changeover error, "
2148 "tunnel link no longer available\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002150 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002151 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002152 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002153 if (!buf) {
2154 warn("Link changeover error, "
2155 "unable to send tunnel msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002156 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002157 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002158 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2159 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01002160 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161}
2162
2163
2164
2165/*
2166 * changeover(): Send whole message queue via the remaining link
2167 * Owner node is locked.
2168 */
2169
Per Liden4323add2006-01-18 00:38:21 +01002170void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171{
2172 u32 msgcount = l_ptr->out_queue_size;
2173 struct sk_buff *crs = l_ptr->first_out;
2174 struct link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002176 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002177
2178 if (!tunnel)
2179 return;
2180
Allan Stephens5392d642006-06-25 23:52:50 -07002181 if (!l_ptr->owner->permit_changeover) {
2182 warn("Link changeover error, "
2183 "peer did not permit changeover\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002185 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002186
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002187 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002188 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002189 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2190 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002191
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192 if (!l_ptr->first_out) {
2193 struct sk_buff *buf;
2194
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002195 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002197 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002198 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002199 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002200 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002201 warn("Link changeover error, "
2202 "unable to send changeover msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203 }
2204 return;
2205 }
Allan Stephensf1310722006-06-25 23:51:37 -07002206
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002207 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002208 l_ptr->owner->active_links[1]);
2209
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210 while (crs) {
2211 struct tipc_msg *msg = buf_msg(crs);
2212
2213 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00002215 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002216
Florian Westphald788d802007-08-02 19:28:06 -07002217 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002218 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00002219 msg_set_seqno(m, msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002220 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2221 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002222 pos += align(msg_size(m));
2223 m = (struct tipc_msg *)pos;
2224 }
2225 } else {
Per Liden4323add2006-01-18 00:38:21 +01002226 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2227 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002228 }
2229 crs = crs->next;
2230 }
2231}
2232
Per Liden4323add2006-01-18 00:38:21 +01002233void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002234{
2235 struct sk_buff *iter;
2236 struct tipc_msg tunnel_hdr;
2237
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002238 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002239 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2241 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2242 iter = l_ptr->first_out;
2243 while (iter) {
2244 struct sk_buff *outbuf;
2245 struct tipc_msg *msg = buf_msg(iter);
2246 u32 length = msg_size(msg);
2247
2248 if (msg_user(msg) == MSG_BUNDLER)
2249 msg_set_type(msg, CLOSED_MSG);
2250 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002251 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002252 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002253 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002254 if (outbuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002255 warn("Link changeover error, "
2256 "unable to send duplicate msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002257 return;
2258 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002259 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2260 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2261 length);
Per Liden4323add2006-01-18 00:38:21 +01002262 tipc_link_send_buf(tunnel, outbuf);
2263 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002264 return;
2265 iter = iter->next;
2266 }
2267}
2268
2269
2270
2271/**
2272 * buf_extract - extracts embedded TIPC message from another message
2273 * @skb: encapsulating message buffer
2274 * @from_pos: offset to extract from
2275 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002276 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002277 * encapsulating message itself is left unchanged.
2278 */
2279
2280static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2281{
2282 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2283 u32 size = msg_size(msg);
2284 struct sk_buff *eb;
2285
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002286 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002287 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002288 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002289 return eb;
2290}
2291
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002292/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002293 * link_recv_changeover_msg(): Receive tunneled packet sent
2294 * via other link. Node is locked. Return extracted buffer.
2295 */
2296
2297static int link_recv_changeover_msg(struct link **l_ptr,
2298 struct sk_buff **buf)
2299{
2300 struct sk_buff *tunnel_buf = *buf;
2301 struct link *dest_link;
2302 struct tipc_msg *msg;
2303 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2304 u32 msg_typ = msg_type(tunnel_msg);
2305 u32 msg_count = msg_msgcnt(tunnel_msg);
2306
2307 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Allan Stephensb29f1422010-12-31 18:59:25 +00002308 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002309 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002310 if (dest_link == *l_ptr) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002311 err("Unexpected changeover message on link <%s>\n",
Allan Stephensf1310722006-06-25 23:51:37 -07002312 (*l_ptr)->name);
2313 goto exit;
2314 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315 *l_ptr = dest_link;
2316 msg = msg_get_wrapped(tunnel_msg);
2317
2318 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002319 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002320 goto exit;
Allan Stephens0e659672010-12-31 18:59:32 +00002321 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002322 if (*buf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002323 warn("Link changeover error, duplicate msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002324 goto exit;
2325 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002326 buf_discard(tunnel_buf);
2327 return 1;
2328 }
2329
2330 /* First original message ?: */
2331
Per Liden4323add2006-01-18 00:38:21 +01002332 if (tipc_link_is_up(dest_link)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002333 info("Resetting link <%s>, changeover initiated by peer\n",
2334 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002335 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336 dest_link->exp_msg_count = msg_count;
2337 if (!msg_count)
2338 goto exit;
2339 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340 dest_link->exp_msg_count = msg_count;
2341 if (!msg_count)
2342 goto exit;
2343 }
2344
2345 /* Receive original message */
2346
2347 if (dest_link->exp_msg_count == 0) {
Allan Stephens5392d642006-06-25 23:52:50 -07002348 warn("Link switchover error, "
2349 "got too many tunnelled messages\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 goto exit;
2351 }
2352 dest_link->exp_msg_count--;
2353 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002354 goto exit;
2355 } else {
2356 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2357 if (*buf != NULL) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002358 buf_discard(tunnel_buf);
2359 return 1;
2360 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002361 warn("Link changeover error, original msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 }
2363 }
2364exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002365 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002366 buf_discard(tunnel_buf);
2367 return 0;
2368}
2369
2370/*
2371 * Bundler functionality:
2372 */
Per Liden4323add2006-01-18 00:38:21 +01002373void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002374{
2375 u32 msgcount = msg_msgcnt(buf_msg(buf));
2376 u32 pos = INT_H_SIZE;
2377 struct sk_buff *obuf;
2378
Per Lidenb97bf3f2006-01-02 19:04:38 +01002379 while (msgcount--) {
2380 obuf = buf_extract(buf, pos);
2381 if (obuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002382 warn("Link unable to unbundle message(s)\n");
2383 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002384 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002386 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 }
2388 buf_discard(buf);
2389}
2390
2391/*
2392 * Fragmentation/defragmentation:
2393 */
2394
2395
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002396/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002397 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002398 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 * Returns user data length.
2400 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002401static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002402{
2403 struct tipc_msg *inmsg = buf_msg(buf);
2404 struct tipc_msg fragm_hdr;
2405 u32 insize = msg_size(inmsg);
2406 u32 dsz = msg_data_sz(inmsg);
2407 unchar *crs = buf->data;
2408 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002409 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002410 u32 fragm_sz = pack_sz - INT_H_SIZE;
2411 u32 fragm_no = 1;
Allan Stephens9c396a72008-06-04 17:36:58 -07002412 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002413
2414 if (msg_short(inmsg))
2415 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002416 else
2417 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002418
2419 if (msg_routed(inmsg))
2420 msg_set_prevnode(inmsg, tipc_own_addr);
2421
2422 /* Prepare reusable fragment header: */
2423
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002424 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002425 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002426 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2427 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2428 msg_set_fragm_no(&fragm_hdr, fragm_no);
2429 l_ptr->stats.sent_fragmented++;
2430
2431 /* Chop up message: */
2432
2433 while (rest > 0) {
2434 struct sk_buff *fragm;
2435
2436 if (rest <= fragm_sz) {
2437 fragm_sz = rest;
2438 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2439 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002440 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002441 if (fragm == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002442 warn("Link unable to fragment message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002443 dsz = -ENOMEM;
2444 goto exit;
2445 }
2446 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002447 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2448 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2449 fragm_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450 /* Send queued messages first, if any: */
2451
2452 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002453 tipc_link_send_buf(l_ptr, fragm);
2454 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002455 return dsz;
2456 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2457 rest -= fragm_sz;
2458 crs += fragm_sz;
2459 msg_set_type(&fragm_hdr, FRAGMENT);
2460 }
2461exit:
2462 buf_discard(buf);
2463 return dsz;
2464}
2465
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002466/*
2467 * A pending message being re-assembled must store certain values
2468 * to handle subsequent fragments correctly. The following functions
Per Lidenb97bf3f2006-01-02 19:04:38 +01002469 * help storing these values in unused, available fields in the
2470 * pending message. This makes dynamic memory allocation unecessary.
2471 */
2472
Sam Ravnborg05790c62006-03-20 22:37:04 -08002473static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002474{
2475 msg_set_seqno(buf_msg(buf), seqno);
2476}
2477
Sam Ravnborg05790c62006-03-20 22:37:04 -08002478static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002479{
2480 return msg_ack(buf_msg(buf));
2481}
2482
Sam Ravnborg05790c62006-03-20 22:37:04 -08002483static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002484{
2485 msg_set_ack(buf_msg(buf), sz);
2486}
2487
Sam Ravnborg05790c62006-03-20 22:37:04 -08002488static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002489{
2490 return msg_bcast_ack(buf_msg(buf));
2491}
2492
Sam Ravnborg05790c62006-03-20 22:37:04 -08002493static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002494{
2495 msg_set_bcast_ack(buf_msg(buf), exp);
2496}
2497
Sam Ravnborg05790c62006-03-20 22:37:04 -08002498static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002499{
2500 return msg_reroute_cnt(buf_msg(buf));
2501}
2502
Sam Ravnborg05790c62006-03-20 22:37:04 -08002503static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002504{
2505 msg_incr_reroute_cnt(buf_msg(buf));
2506}
2507
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002508/*
2509 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002510 * the reassembled buffer if message is complete.
2511 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002512int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
Per Liden4323add2006-01-18 00:38:21 +01002513 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002514{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002515 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002516 struct sk_buff *fbuf = *fb;
2517 struct tipc_msg *fragm = buf_msg(fbuf);
2518 struct sk_buff *pbuf = *pending;
2519 u32 long_msg_seq_no = msg_long_msgno(fragm);
2520
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002521 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002522
2523 /* Is there an incomplete message waiting for this fragment? */
2524
Joe Perchesf64f9e72009-11-29 16:55:45 -08002525 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2526 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527 prev = pbuf;
2528 pbuf = pbuf->next;
2529 }
2530
2531 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2532 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2533 u32 msg_sz = msg_size(imsg);
2534 u32 fragm_sz = msg_data_sz(fragm);
2535 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2536 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2537 if (msg_type(imsg) == TIPC_MCAST_MSG)
2538 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2539 if (msg_size(imsg) > max) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002540 buf_discard(fbuf);
2541 return 0;
2542 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002543 pbuf = tipc_buf_acquire(msg_size(imsg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002544 if (pbuf != NULL) {
2545 pbuf->next = *pending;
2546 *pending = pbuf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002547 skb_copy_to_linear_data(pbuf, imsg,
2548 msg_data_sz(fragm));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002549 /* Prepare buffer for subsequent fragments. */
2550
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002551 set_long_msg_seqno(pbuf, long_msg_seq_no);
Allan Stephens0e659672010-12-31 18:59:32 +00002552 set_fragm_size(pbuf, fragm_sz);
2553 set_expected_frags(pbuf, exp_fragm_cnt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002555 warn("Link unable to reassemble fragmented message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002556 }
2557 buf_discard(fbuf);
2558 return 0;
2559 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2560 u32 dsz = msg_data_sz(fragm);
2561 u32 fsz = get_fragm_size(pbuf);
2562 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2563 u32 exp_frags = get_expected_frags(pbuf) - 1;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002564 skb_copy_to_linear_data_offset(pbuf, crs,
2565 msg_data(fragm), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002566 buf_discard(fbuf);
2567
2568 /* Is message complete? */
2569
2570 if (exp_frags == 0) {
2571 if (prev)
2572 prev->next = pbuf->next;
2573 else
2574 *pending = pbuf->next;
2575 msg_reset_reroute_cnt(buf_msg(pbuf));
2576 *fb = pbuf;
2577 *m = buf_msg(pbuf);
2578 return 1;
2579 }
Allan Stephens0e659672010-12-31 18:59:32 +00002580 set_expected_frags(pbuf, exp_frags);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002581 return 0;
2582 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002583 buf_discard(fbuf);
2584 return 0;
2585}
2586
2587/**
2588 * link_check_defragm_bufs - flush stale incoming message fragments
2589 * @l_ptr: pointer to link
2590 */
2591
2592static void link_check_defragm_bufs(struct link *l_ptr)
2593{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002594 struct sk_buff *prev = NULL;
2595 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002596 struct sk_buff *buf = l_ptr->defragm_buf;
2597
2598 if (!buf)
2599 return;
2600 if (!link_working_working(l_ptr))
2601 return;
2602 while (buf) {
2603 u32 cnt = get_timer_cnt(buf);
2604
2605 next = buf->next;
2606 if (cnt < 4) {
2607 incr_timer_cnt(buf);
2608 prev = buf;
2609 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610 if (prev)
2611 prev->next = buf->next;
2612 else
2613 l_ptr->defragm_buf = buf->next;
2614 buf_discard(buf);
2615 }
2616 buf = next;
2617 }
2618}
2619
2620
2621
2622static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2623{
2624 l_ptr->tolerance = tolerance;
2625 l_ptr->continuity_interval =
2626 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2627 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2628}
2629
2630
Per Liden4323add2006-01-18 00:38:21 +01002631void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632{
2633 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002634 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2635 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2636 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2637 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002638 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002639 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2640 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2641 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2642 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002643 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2645 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2646 /* FRAGMENT and LAST_FRAGMENT packets */
2647 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2648}
2649
2650/**
2651 * link_find_link - locate link by name
2652 * @name - ptr to link name string
2653 * @node - ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002654 *
Per Liden4323add2006-01-18 00:38:21 +01002655 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002656 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002657 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002658 * Returns pointer to link (or 0 if invalid link name).
2659 */
2660
David S. Miller6c000552008-09-02 23:38:32 -07002661static struct link *link_find_link(const char *name, struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002662{
2663 struct link_name link_name_parts;
2664 struct bearer *b_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002665 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002666
2667 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002668 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002669
Per Liden4323add2006-01-18 00:38:21 +01002670 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002671 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002672 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002673
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002674 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002675 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002676 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002677
2678 l_ptr = (*node)->links[b_ptr->identity];
2679 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002680 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002681
2682 return l_ptr;
2683}
2684
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002685struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002686 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002687{
2688 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002689 u32 new_value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002690 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002691 struct tipc_node *node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002692 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002693
2694 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002695 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002696
2697 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2698 new_value = ntohl(args->value);
2699
Per Liden4323add2006-01-18 00:38:21 +01002700 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002701 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002702 (tipc_bclink_set_queue_limits(new_value) == 0))
2703 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002704 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002705 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002706 }
2707
Per Liden4323add2006-01-18 00:38:21 +01002708 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002709 l_ptr = link_find_link(args->name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002711 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002712 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002713 }
2714
Per Liden4323add2006-01-18 00:38:21 +01002715 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716 res = -EINVAL;
2717 switch (cmd) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002718 case TIPC_CMD_SET_LINK_TOL:
2719 if ((new_value >= TIPC_MIN_LINK_TOL) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002720 (new_value <= TIPC_MAX_LINK_TOL)) {
2721 link_set_supervision_props(l_ptr, new_value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002722 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002723 0, 0, new_value, 0, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002724 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002725 }
2726 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002727 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002728 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2729 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002730 l_ptr->priority = new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002731 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002732 0, 0, 0, new_value, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002733 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002734 }
2735 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002736 case TIPC_CMD_SET_LINK_WINDOW:
2737 if ((new_value >= TIPC_MIN_LINK_WIN) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002738 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002739 tipc_link_set_queue_limits(l_ptr, new_value);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002740 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002741 }
2742 break;
2743 }
Per Liden4323add2006-01-18 00:38:21 +01002744 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002745
Per Liden4323add2006-01-18 00:38:21 +01002746 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002747 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002748 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002749
Per Liden4323add2006-01-18 00:38:21 +01002750 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002751}
2752
2753/**
2754 * link_reset_statistics - reset link statistics
2755 * @l_ptr: pointer to link
2756 */
2757
2758static void link_reset_statistics(struct link *l_ptr)
2759{
2760 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2761 l_ptr->stats.sent_info = l_ptr->next_out_no;
2762 l_ptr->stats.recv_info = l_ptr->next_in_no;
2763}
2764
Per Liden4323add2006-01-18 00:38:21 +01002765struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002766{
2767 char *link_name;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002768 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002769 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002770
2771 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002772 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002773
2774 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002775 if (!strcmp(link_name, tipc_bclink_name)) {
2776 if (tipc_bclink_reset_stats())
2777 return tipc_cfg_reply_error_string("link not found");
2778 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002779 }
2780
Per Liden4323add2006-01-18 00:38:21 +01002781 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002782 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002783 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002784 read_unlock_bh(&tipc_net_lock);
2785 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002786 }
2787
Per Liden4323add2006-01-18 00:38:21 +01002788 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002789 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002790 tipc_node_unlock(node);
2791 read_unlock_bh(&tipc_net_lock);
2792 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002793}
2794
2795/**
2796 * percent - convert count to a percentage of total (rounding up or down)
2797 */
2798
2799static u32 percent(u32 count, u32 total)
2800{
2801 return (count * 100 + (total / 2)) / total;
2802}
2803
2804/**
Per Liden4323add2006-01-18 00:38:21 +01002805 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002806 * @name: link name
2807 * @buf: print buffer area
2808 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002809 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002810 * Returns length of print buffer data string (or 0 if error)
2811 */
2812
Per Liden4323add2006-01-18 00:38:21 +01002813static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002814{
2815 struct print_buf pb;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002816 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002817 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002818 char *status;
2819 u32 profile_total = 0;
2820
Per Liden4323add2006-01-18 00:38:21 +01002821 if (!strcmp(name, tipc_bclink_name))
2822 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002823
Per Liden4323add2006-01-18 00:38:21 +01002824 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002825
Per Liden4323add2006-01-18 00:38:21 +01002826 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002827 l_ptr = link_find_link(name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002828 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002829 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002830 return 0;
2831 }
Per Liden4323add2006-01-18 00:38:21 +01002832 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002833
Per Liden4323add2006-01-18 00:38:21 +01002834 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002835 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01002836 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002837 status = "STANDBY";
2838 else
2839 status = "DEFUNCT";
2840 tipc_printf(&pb, "Link <%s>\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002841 " %s MTU:%u Priority:%u Tolerance:%u ms"
2842 " Window:%u packets\n",
Allan Stephens15e979d2010-05-11 14:30:10 +00002843 l_ptr->name, status, l_ptr->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002844 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002845 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002846 l_ptr->next_in_no - l_ptr->stats.recv_info,
2847 l_ptr->stats.recv_fragments,
2848 l_ptr->stats.recv_fragmented,
2849 l_ptr->stats.recv_bundles,
2850 l_ptr->stats.recv_bundled);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002851 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002852 l_ptr->next_out_no - l_ptr->stats.sent_info,
2853 l_ptr->stats.sent_fragments,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002854 l_ptr->stats.sent_fragmented,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002855 l_ptr->stats.sent_bundles,
2856 l_ptr->stats.sent_bundled);
2857 profile_total = l_ptr->stats.msg_length_counts;
2858 if (!profile_total)
2859 profile_total = 1;
2860 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002861 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2862 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002863 l_ptr->stats.msg_length_counts,
2864 l_ptr->stats.msg_lengths_total / profile_total,
2865 percent(l_ptr->stats.msg_length_profile[0], profile_total),
2866 percent(l_ptr->stats.msg_length_profile[1], profile_total),
2867 percent(l_ptr->stats.msg_length_profile[2], profile_total),
2868 percent(l_ptr->stats.msg_length_profile[3], profile_total),
2869 percent(l_ptr->stats.msg_length_profile[4], profile_total),
2870 percent(l_ptr->stats.msg_length_profile[5], profile_total),
2871 percent(l_ptr->stats.msg_length_profile[6], profile_total));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002872 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002873 l_ptr->stats.recv_states,
2874 l_ptr->stats.recv_probes,
2875 l_ptr->stats.recv_nacks,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002876 l_ptr->stats.deferred_recv,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002877 l_ptr->stats.duplicates);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002878 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
2879 l_ptr->stats.sent_states,
2880 l_ptr->stats.sent_probes,
2881 l_ptr->stats.sent_nacks,
2882 l_ptr->stats.sent_acks,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002883 l_ptr->stats.retransmitted);
2884 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
2885 l_ptr->stats.bearer_congs,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002886 l_ptr->stats.link_congs,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002887 l_ptr->stats.max_queue_sz,
2888 l_ptr->stats.queue_sz_counts
2889 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
2890 : 0);
2891
Per Liden4323add2006-01-18 00:38:21 +01002892 tipc_node_unlock(node);
2893 read_unlock_bh(&tipc_net_lock);
2894 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002895}
2896
2897#define MAX_LINK_STATS_INFO 2000
2898
Per Liden4323add2006-01-18 00:38:21 +01002899struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002900{
2901 struct sk_buff *buf;
2902 struct tlv_desc *rep_tlv;
2903 int str_len;
2904
2905 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002906 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002907
Per Liden4323add2006-01-18 00:38:21 +01002908 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002909 if (!buf)
2910 return NULL;
2911
2912 rep_tlv = (struct tlv_desc *)buf->data;
2913
Per Liden4323add2006-01-18 00:38:21 +01002914 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
2915 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002916 if (!str_len) {
2917 buf_discard(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002918 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002919 }
2920
2921 skb_put(buf, TLV_SPACE(str_len));
2922 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2923
2924 return buf;
2925}
2926
Per Lidenb97bf3f2006-01-02 19:04:38 +01002927/**
Per Liden4323add2006-01-18 00:38:21 +01002928 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002929 * @dest: network address of destination node
2930 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002931 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002932 * If no active link can be found, uses default maximum packet size.
2933 */
2934
Per Liden4323add2006-01-18 00:38:21 +01002935u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002936{
David S. Miller6c000552008-09-02 23:38:32 -07002937 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002938 struct link *l_ptr;
2939 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002940
Per Lidenb97bf3f2006-01-02 19:04:38 +01002941 if (dest == tipc_own_addr)
2942 return MAX_MSG_SIZE;
2943
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002944 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002945 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002946 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002947 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002948 l_ptr = n_ptr->active_links[selector & 1];
2949 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002950 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002951 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002952 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002953 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002954 return res;
2955}
2956
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002957static void link_print(struct link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002958{
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002959 char print_area[256];
2960 struct print_buf pb;
2961 struct print_buf *buf = &pb;
2962
2963 tipc_printbuf_init(buf, print_area, sizeof(print_area));
2964
Per Lidenb97bf3f2006-01-02 19:04:38 +01002965 tipc_printf(buf, str);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002966 tipc_printf(buf, "Link %x<%s>:",
2967 l_ptr->addr, l_ptr->b_ptr->publ.name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002968
2969#ifdef CONFIG_TIPC_DEBUG
2970 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
2971 goto print_state;
2972
Per Lidenb97bf3f2006-01-02 19:04:38 +01002973 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
2974 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
2975 tipc_printf(buf, "SQUE");
2976 if (l_ptr->first_out) {
2977 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
2978 if (l_ptr->next_out)
2979 tipc_printf(buf, "%u..",
2980 msg_seqno(buf_msg(l_ptr->next_out)));
Allan Stephensb82834e2010-05-11 14:30:04 +00002981 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002982 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
2983 msg_seqno(buf_msg(l_ptr->first_out)))
Joe Perchesf64f9e72009-11-29 16:55:45 -08002984 != (l_ptr->out_queue_size - 1)) ||
2985 (l_ptr->last_out->next != NULL)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002986 tipc_printf(buf, "\nSend queue inconsistency\n");
2987 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
2988 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
2989 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002990 }
2991 } else
2992 tipc_printf(buf, "[]");
2993 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
2994 if (l_ptr->oldest_deferred_in) {
2995 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2996 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
2997 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
2998 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
2999 tipc_printf(buf, ":RQSIZ(%u)",
3000 l_ptr->deferred_inqueue_sz);
3001 }
3002 }
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003003print_state:
3004#endif
3005
Per Lidenb97bf3f2006-01-02 19:04:38 +01003006 if (link_working_unknown(l_ptr))
3007 tipc_printf(buf, ":WU");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003008 else if (link_reset_reset(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003009 tipc_printf(buf, ":RR");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003010 else if (link_reset_unknown(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003011 tipc_printf(buf, ":RU");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003012 else if (link_working_working(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003013 tipc_printf(buf, ":WW");
3014 tipc_printf(buf, "\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003015
3016 tipc_printbuf_validate(buf);
3017 info("%s", print_area);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003018}
3019