blob: 1c5c53a8153175dd3a58c229bbeb8e33397261f6 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/link.c: TIPC link code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Allan Stephens05646c92007-06-10 17:25:24 -07004 * Copyright (c) 1996-2007, Ericsson AB
Allan Stephens23dd4cc2011-01-07 11:43:40 -05005 * Copyright (c) 2004-2007, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "link.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "discover.h"
42#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
44
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090045/*
Allan Stephensa686e682008-06-04 17:29:39 -070046 * Out-of-range value for link session numbers
47 */
48
49#define INVALID_SESSION 0x10000
50
51/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090052 * Link state events:
Per Lidenb97bf3f2006-01-02 19:04:38 +010053 */
54
55#define STARTING_EVT 856384768 /* link processing trigger */
56#define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
57#define TIMEOUT_EVT 560817u /* link timer expired */
58
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090059/*
60 * The following two 'message types' is really just implementation
61 * data conveniently stored in the message header.
Per Lidenb97bf3f2006-01-02 19:04:38 +010062 * They must not be considered part of the protocol
63 */
64#define OPEN_MSG 0
65#define CLOSED_MSG 1
66
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090067/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010068 * State value stored in 'exp_msg_count'
69 */
70
71#define START_CHANGEOVER 100000u
72
73/**
74 * struct link_name - deconstructed link name
75 * @addr_local: network address of node at this end
76 * @if_local: name of interface at this end
77 * @addr_peer: network address of node at far end
78 * @if_peer: name of interface at far end
79 */
80
81struct link_name {
82 u32 addr_local;
83 char if_local[TIPC_MAX_IF_NAME];
84 u32 addr_peer;
85 char if_peer[TIPC_MAX_IF_NAME];
86};
87
Per Lidenb97bf3f2006-01-02 19:04:38 +010088static void link_handle_out_of_seq_msg(struct link *l_ptr,
89 struct sk_buff *buf);
90static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
91static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
92static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
Allan Stephens23dd4cc2011-01-07 11:43:40 -050093static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 struct iovec const *msg_sect,
95 u32 num_sect, u32 destnode);
96static void link_check_defragm_bufs(struct link *l_ptr);
97static void link_state_event(struct link *l_ptr, u32 event);
98static void link_reset_statistics(struct link *l_ptr);
Allan Stephens8d64a5b2010-12-31 18:59:27 +000099static void link_print(struct link *l_ptr, const char *str);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000100static void link_start(struct link *l_ptr);
101static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf);
102
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800104 * Simple link routines
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 */
106
Sam Ravnborg05790c62006-03-20 22:37:04 -0800107static unsigned int align(unsigned int i)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108{
109 return (i + 3) & ~3u;
110}
111
Sam Ravnborg05790c62006-03-20 22:37:04 -0800112static void link_init_max_pkt(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113{
114 u32 max_pkt;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900115
Allan Stephens2d627b92011-01-07 13:00:11 -0500116 max_pkt = (l_ptr->b_ptr->mtu & ~3);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117 if (max_pkt > MAX_MSG_SIZE)
118 max_pkt = MAX_MSG_SIZE;
119
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900120 l_ptr->max_pkt_target = max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
122 l_ptr->max_pkt = l_ptr->max_pkt_target;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123 else
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 l_ptr->max_pkt = MAX_PKT_DEFAULT;
125
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900126 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127}
128
Sam Ravnborg05790c62006-03-20 22:37:04 -0800129static u32 link_next_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130{
131 if (l_ptr->next_out)
132 return msg_seqno(buf_msg(l_ptr->next_out));
133 return mod(l_ptr->next_out_no);
134}
135
Sam Ravnborg05790c62006-03-20 22:37:04 -0800136static u32 link_last_sent(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137{
138 return mod(link_next_sent(l_ptr) - 1);
139}
140
141/*
Sam Ravnborg05790c62006-03-20 22:37:04 -0800142 * Simple non-static link routines (i.e. referenced outside this file)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 */
144
Per Liden4323add2006-01-18 00:38:21 +0100145int tipc_link_is_up(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146{
147 if (!l_ptr)
148 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +0000149 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150}
151
Per Liden4323add2006-01-18 00:38:21 +0100152int tipc_link_is_active(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000154 return (l_ptr->owner->active_links[0] == l_ptr) ||
155 (l_ptr->owner->active_links[1] == l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156}
157
158/**
159 * link_name_validate - validate & (optionally) deconstruct link name
160 * @name - ptr to link name string
161 * @name_parts - ptr to area for link name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900162 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 * Returns 1 if link name is valid, otherwise 0.
164 */
165
166static int link_name_validate(const char *name, struct link_name *name_parts)
167{
168 char name_copy[TIPC_MAX_LINK_NAME];
169 char *addr_local;
170 char *if_local;
171 char *addr_peer;
172 char *if_peer;
173 char dummy;
174 u32 z_local, c_local, n_local;
175 u32 z_peer, c_peer, n_peer;
176 u32 if_local_len;
177 u32 if_peer_len;
178
179 /* copy link name & ensure length is OK */
180
181 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
182 /* need above in case non-Posix strncpy() doesn't pad with nulls */
183 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
184 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
185 return 0;
186
187 /* ensure all component parts of link name are present */
188
189 addr_local = name_copy;
Allan Stephens2db99832010-12-31 18:59:33 +0000190 if_local = strchr(addr_local, ':');
191 if (if_local == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 return 0;
193 *(if_local++) = 0;
Allan Stephens2db99832010-12-31 18:59:33 +0000194 addr_peer = strchr(if_local, '-');
195 if (addr_peer == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 return 0;
197 *(addr_peer++) = 0;
198 if_local_len = addr_peer - if_local;
Allan Stephens2db99832010-12-31 18:59:33 +0000199 if_peer = strchr(addr_peer, ':');
200 if (if_peer == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 return 0;
202 *(if_peer++) = 0;
203 if_peer_len = strlen(if_peer) + 1;
204
205 /* validate component parts of link name */
206
207 if ((sscanf(addr_local, "%u.%u.%u%c",
208 &z_local, &c_local, &n_local, &dummy) != 3) ||
209 (sscanf(addr_peer, "%u.%u.%u%c",
210 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
211 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
212 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900213 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
214 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
216 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
217 return 0;
218
219 /* return link name components, if necessary */
220
221 if (name_parts) {
222 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
223 strcpy(name_parts->if_local, if_local);
224 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
225 strcpy(name_parts->if_peer, if_peer);
226 }
227 return 1;
228}
229
230/**
231 * link_timeout - handle expiration of link timer
232 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900233 *
Per Liden4323add2006-01-18 00:38:21 +0100234 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
235 * with tipc_link_delete(). (There is no risk that the node will be deleted by
236 * another thread because tipc_link_delete() always cancels the link timer before
237 * tipc_node_delete() is called.)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 */
239
240static void link_timeout(struct link *l_ptr)
241{
Per Liden4323add2006-01-18 00:38:21 +0100242 tipc_node_lock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
244 /* update counters used in statistical profiling of send traffic */
245
246 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
247 l_ptr->stats.queue_sz_counts++;
248
249 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
250 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
251
252 if (l_ptr->first_out) {
253 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
254 u32 length = msg_size(msg);
255
Joe Perchesf64f9e72009-11-29 16:55:45 -0800256 if ((msg_user(msg) == MSG_FRAGMENTER) &&
257 (msg_type(msg) == FIRST_FRAGMENT)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 length = msg_size(msg_get_wrapped(msg));
259 }
260 if (length) {
261 l_ptr->stats.msg_lengths_total += length;
262 l_ptr->stats.msg_length_counts++;
263 if (length <= 64)
264 l_ptr->stats.msg_length_profile[0]++;
265 else if (length <= 256)
266 l_ptr->stats.msg_length_profile[1]++;
267 else if (length <= 1024)
268 l_ptr->stats.msg_length_profile[2]++;
269 else if (length <= 4096)
270 l_ptr->stats.msg_length_profile[3]++;
271 else if (length <= 16384)
272 l_ptr->stats.msg_length_profile[4]++;
273 else if (length <= 32768)
274 l_ptr->stats.msg_length_profile[5]++;
275 else
276 l_ptr->stats.msg_length_profile[6]++;
277 }
278 }
279
280 /* do all other link processing performed on a periodic basis */
281
282 link_check_defragm_bufs(l_ptr);
283
284 link_state_event(l_ptr, TIMEOUT_EVT);
285
286 if (l_ptr->next_out)
Per Liden4323add2006-01-18 00:38:21 +0100287 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288
Per Liden4323add2006-01-18 00:38:21 +0100289 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290}
291
Sam Ravnborg05790c62006-03-20 22:37:04 -0800292static void link_set_timer(struct link *l_ptr, u32 time)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293{
294 k_start_timer(&l_ptr->timer, time);
295}
296
297/**
Per Liden4323add2006-01-18 00:38:21 +0100298 * tipc_link_create - create a new link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 * @b_ptr: pointer to associated bearer
300 * @peer: network address of node at other end of link
301 * @media_addr: media address to use when sending messages over link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900302 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 * Returns pointer to link.
304 */
305
Allan Stephens2d627b92011-01-07 13:00:11 -0500306struct link *tipc_link_create(struct tipc_bearer *b_ptr, const u32 peer,
Per Liden4323add2006-01-18 00:38:21 +0100307 const struct tipc_media_addr *media_addr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308{
309 struct link *l_ptr;
310 struct tipc_msg *msg;
311 char *if_name;
312
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700313 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 if (!l_ptr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700315 warn("Link creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 return NULL;
317 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318
319 l_ptr->addr = peer;
Allan Stephens2d627b92011-01-07 13:00:11 -0500320 if_name = strchr(b_ptr->name, ':') + 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
322 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900323 tipc_node(tipc_own_addr),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 if_name,
325 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
326 /* note: peer i/f is appended to link name by reset/activate */
327 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 l_ptr->checkpoint = 1;
329 l_ptr->b_ptr = b_ptr;
330 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
331 l_ptr->state = RESET_UNKNOWN;
332
333 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
334 msg = l_ptr->pmsg;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000335 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 msg_set_size(msg, sizeof(l_ptr->proto_msg));
Allan Stephensa686e682008-06-04 17:29:39 -0700337 msg_set_session(msg, (tipc_random & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 msg_set_bearer_id(msg, b_ptr->identity);
339 strcpy((char *)msg_data(msg), if_name);
340
341 l_ptr->priority = b_ptr->priority;
Per Liden4323add2006-01-18 00:38:21 +0100342 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343
344 link_init_max_pkt(l_ptr);
345
346 l_ptr->next_out_no = 1;
347 INIT_LIST_HEAD(&l_ptr->waiting_ports);
348
349 link_reset_statistics(l_ptr);
350
Per Liden4323add2006-01-18 00:38:21 +0100351 l_ptr->owner = tipc_node_attach_link(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 if (!l_ptr->owner) {
353 kfree(l_ptr);
354 return NULL;
355 }
356
Florian Westphal945710652007-07-26 00:05:07 -0700357 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
358 list_add_tail(&l_ptr->link_list, &b_ptr->links);
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000359 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 return l_ptr;
362}
363
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900364/**
Per Liden4323add2006-01-18 00:38:21 +0100365 * tipc_link_delete - delete a link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 * @l_ptr: pointer to link
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900367 *
Per Liden4323add2006-01-18 00:38:21 +0100368 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 * This routine must not grab the node lock until after link timer cancellation
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370 * to avoid a potential deadlock situation.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 */
372
Per Liden4323add2006-01-18 00:38:21 +0100373void tipc_link_delete(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374{
375 if (!l_ptr) {
376 err("Attempt to delete non-existent link\n");
377 return;
378 }
379
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 k_cancel_timer(&l_ptr->timer);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900381
Per Liden4323add2006-01-18 00:38:21 +0100382 tipc_node_lock(l_ptr->owner);
383 tipc_link_reset(l_ptr);
384 tipc_node_detach_link(l_ptr->owner, l_ptr);
385 tipc_link_stop(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 list_del_init(&l_ptr->link_list);
Per Liden4323add2006-01-18 00:38:21 +0100387 tipc_node_unlock(l_ptr->owner);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 k_term_timer(&l_ptr->timer);
389 kfree(l_ptr);
390}
391
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000392static void link_start(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 link_state_event(l_ptr, STARTING_EVT);
395}
396
397/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398 * link_schedule_port - schedule port for deferred sending
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 * @l_ptr: pointer to link
400 * @origport: reference to sending port
401 * @sz: amount of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900402 *
403 * Schedules port for renewed sending of messages after link congestion
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 * has abated.
405 */
406
407static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
408{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500409 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410
Per Liden4323add2006-01-18 00:38:21 +0100411 spin_lock_bh(&tipc_port_list_lock);
412 p_ptr = tipc_port_lock(origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 if (p_ptr) {
414 if (!p_ptr->wakeup)
415 goto exit;
416 if (!list_empty(&p_ptr->wait_list))
417 goto exit;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500418 p_ptr->congested = 1;
Allan Stephens15e979d2010-05-11 14:30:10 +0000419 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
421 l_ptr->stats.link_congs++;
422exit:
Per Liden4323add2006-01-18 00:38:21 +0100423 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 }
Per Liden4323add2006-01-18 00:38:21 +0100425 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 return -ELINKCONG;
427}
428
Per Liden4323add2006-01-18 00:38:21 +0100429void tipc_link_wakeup_ports(struct link *l_ptr, int all)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500431 struct tipc_port *p_ptr;
432 struct tipc_port *temp_p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
434
435 if (all)
436 win = 100000;
437 if (win <= 0)
438 return;
Per Liden4323add2006-01-18 00:38:21 +0100439 if (!spin_trylock_bh(&tipc_port_list_lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 return;
441 if (link_congested(l_ptr))
442 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900443 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 wait_list) {
445 if (win <= 0)
446 break;
447 list_del_init(&p_ptr->wait_list);
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500448 spin_lock_bh(p_ptr->lock);
449 p_ptr->congested = 0;
450 p_ptr->wakeup(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 win -= p_ptr->waiting_pkts;
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500452 spin_unlock_bh(p_ptr->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 }
454
455exit:
Per Liden4323add2006-01-18 00:38:21 +0100456 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457}
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 * link_release_outqueue - purge link's outbound message queue
461 * @l_ptr: pointer to link
462 */
463
464static void link_release_outqueue(struct link *l_ptr)
465{
466 struct sk_buff *buf = l_ptr->first_out;
467 struct sk_buff *next;
468
469 while (buf) {
470 next = buf->next;
471 buf_discard(buf);
472 buf = next;
473 }
474 l_ptr->first_out = NULL;
475 l_ptr->out_queue_size = 0;
476}
477
478/**
Per Liden4323add2006-01-18 00:38:21 +0100479 * tipc_link_reset_fragments - purge link's inbound message fragments queue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 * @l_ptr: pointer to link
481 */
482
Per Liden4323add2006-01-18 00:38:21 +0100483void tipc_link_reset_fragments(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484{
485 struct sk_buff *buf = l_ptr->defragm_buf;
486 struct sk_buff *next;
487
488 while (buf) {
489 next = buf->next;
490 buf_discard(buf);
491 buf = next;
492 }
493 l_ptr->defragm_buf = NULL;
494}
495
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900496/**
Per Liden4323add2006-01-18 00:38:21 +0100497 * tipc_link_stop - purge all inbound and outbound messages associated with link
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 * @l_ptr: pointer to link
499 */
500
Per Liden4323add2006-01-18 00:38:21 +0100501void tipc_link_stop(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502{
503 struct sk_buff *buf;
504 struct sk_buff *next;
505
506 buf = l_ptr->oldest_deferred_in;
507 while (buf) {
508 next = buf->next;
509 buf_discard(buf);
510 buf = next;
511 }
512
513 buf = l_ptr->first_out;
514 while (buf) {
515 next = buf->next;
516 buf_discard(buf);
517 buf = next;
518 }
519
Per Liden4323add2006-01-18 00:38:21 +0100520 tipc_link_reset_fragments(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521
522 buf_discard(l_ptr->proto_msg_queue);
523 l_ptr->proto_msg_queue = NULL;
524}
525
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526/* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527#define link_send_event(fcn, l_ptr, up) do { } while (0)
528
Per Liden4323add2006-01-18 00:38:21 +0100529void tipc_link_reset(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530{
531 struct sk_buff *buf;
532 u32 prev_state = l_ptr->state;
533 u32 checkpoint = l_ptr->next_in_no;
Allan Stephens5392d642006-06-25 23:52:50 -0700534 int was_active_link = tipc_link_is_active(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535
Allan Stephensa686e682008-06-04 17:29:39 -0700536 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537
Allan Stephensa686e682008-06-04 17:29:39 -0700538 /* Link is down, accept any session */
539 l_ptr->peer_session = INVALID_SESSION;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 /* Prepare for max packet size negotiation */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 link_init_max_pkt(l_ptr);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900543
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 l_ptr->state = RESET_UNKNOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545
546 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
547 return;
548
Per Liden4323add2006-01-18 00:38:21 +0100549 tipc_node_link_down(l_ptr->owner, l_ptr);
550 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
Paul Gortmaker7368ddf2010-10-12 14:25:58 +0000551
Allan Stephens5392d642006-06-25 23:52:50 -0700552 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 l_ptr->owner->permit_changeover) {
554 l_ptr->reset_checkpoint = checkpoint;
555 l_ptr->exp_msg_count = START_CHANGEOVER;
556 }
557
558 /* Clean up all queues: */
559
560 link_release_outqueue(l_ptr);
561 buf_discard(l_ptr->proto_msg_queue);
562 l_ptr->proto_msg_queue = NULL;
563 buf = l_ptr->oldest_deferred_in;
564 while (buf) {
565 struct sk_buff *next = buf->next;
566 buf_discard(buf);
567 buf = next;
568 }
569 if (!list_empty(&l_ptr->waiting_ports))
Per Liden4323add2006-01-18 00:38:21 +0100570 tipc_link_wakeup_ports(l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571
572 l_ptr->retransm_queue_head = 0;
573 l_ptr->retransm_queue_size = 0;
574 l_ptr->last_out = NULL;
575 l_ptr->first_out = NULL;
576 l_ptr->next_out = NULL;
577 l_ptr->unacked_window = 0;
578 l_ptr->checkpoint = 1;
579 l_ptr->next_out_no = 1;
580 l_ptr->deferred_inqueue_sz = 0;
581 l_ptr->oldest_deferred_in = NULL;
582 l_ptr->newest_deferred_in = NULL;
583 l_ptr->fsm_msg_cnt = 0;
584 l_ptr->stale_count = 0;
585 link_reset_statistics(l_ptr);
586
Per Liden4323add2006-01-18 00:38:21 +0100587 link_send_event(tipc_cfg_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100589 link_send_event(tipc_disc_link_event, l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590}
591
592
593static void link_activate(struct link *l_ptr)
594{
Allan Stephens5392d642006-06-25 23:52:50 -0700595 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
Per Liden4323add2006-01-18 00:38:21 +0100596 tipc_node_link_up(l_ptr->owner, l_ptr);
597 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
598 link_send_event(tipc_cfg_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 if (!in_own_cluster(l_ptr->addr))
Per Liden4323add2006-01-18 00:38:21 +0100600 link_send_event(tipc_disc_link_event, l_ptr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601}
602
603/**
604 * link_state_event - link finite state machine
605 * @l_ptr: pointer to link
606 * @event: state machine event to process
607 */
608
609static void link_state_event(struct link *l_ptr, unsigned event)
610{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 struct link *other;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 u32 cont_intv = l_ptr->continuity_interval;
613
614 if (!l_ptr->started && (event != STARTING_EVT))
615 return; /* Not yet. */
616
617 if (link_blocked(l_ptr)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000618 if (event == TIMEOUT_EVT)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 return; /* Changeover going on */
621 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622
623 switch (l_ptr->state) {
624 case WORKING_WORKING:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 switch (event) {
626 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 break;
629 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 if (l_ptr->next_in_no != l_ptr->checkpoint) {
631 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100632 if (tipc_bclink_acks_missing(l_ptr->owner)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900633 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100634 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 l_ptr->fsm_msg_cnt++;
636 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900637 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100638 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 l_ptr->fsm_msg_cnt++;
640 }
641 link_set_timer(l_ptr, cont_intv);
642 break;
643 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 l_ptr->state = WORKING_UNKNOWN;
645 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100646 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 l_ptr->fsm_msg_cnt++;
648 link_set_timer(l_ptr, cont_intv / 4);
649 break;
650 case RESET_MSG:
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900651 info("Resetting link <%s>, requested by peer\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700652 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100653 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 l_ptr->state = RESET_RESET;
655 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100656 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 l_ptr->fsm_msg_cnt++;
658 link_set_timer(l_ptr, cont_intv);
659 break;
660 default:
661 err("Unknown link event %u in WW state\n", event);
662 }
663 break;
664 case WORKING_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 switch (event) {
666 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 case ACTIVATE_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 l_ptr->state = WORKING_WORKING;
669 l_ptr->fsm_msg_cnt = 0;
670 link_set_timer(l_ptr, cont_intv);
671 break;
672 case RESET_MSG:
Allan Stephensa10bd922006-06-25 23:52:17 -0700673 info("Resetting link <%s>, requested by peer "
674 "while probing\n", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100675 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 l_ptr->state = RESET_RESET;
677 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100678 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 l_ptr->fsm_msg_cnt++;
680 link_set_timer(l_ptr, cont_intv);
681 break;
682 case TIMEOUT_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 if (l_ptr->next_in_no != l_ptr->checkpoint) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684 l_ptr->state = WORKING_WORKING;
685 l_ptr->fsm_msg_cnt = 0;
686 l_ptr->checkpoint = l_ptr->next_in_no;
Per Liden4323add2006-01-18 00:38:21 +0100687 if (tipc_bclink_acks_missing(l_ptr->owner)) {
688 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
689 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 l_ptr->fsm_msg_cnt++;
691 }
692 link_set_timer(l_ptr, cont_intv);
693 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +0100695 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 l_ptr->fsm_msg_cnt++;
697 link_set_timer(l_ptr, cont_intv / 4);
698 } else { /* Link has failed */
Allan Stephensa10bd922006-06-25 23:52:17 -0700699 warn("Resetting link <%s>, peer not responding\n",
700 l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100701 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 l_ptr->state = RESET_UNKNOWN;
703 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100704 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
705 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 l_ptr->fsm_msg_cnt++;
707 link_set_timer(l_ptr, cont_intv);
708 }
709 break;
710 default:
711 err("Unknown link event %u in WU state\n", event);
712 }
713 break;
714 case RESET_UNKNOWN:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 switch (event) {
716 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717 break;
718 case ACTIVATE_MSG:
719 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000720 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 l_ptr->state = WORKING_WORKING;
723 l_ptr->fsm_msg_cnt = 0;
724 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100725 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 l_ptr->fsm_msg_cnt++;
727 link_set_timer(l_ptr, cont_intv);
728 break;
729 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 l_ptr->state = RESET_RESET;
731 l_ptr->fsm_msg_cnt = 0;
Per Liden4323add2006-01-18 00:38:21 +0100732 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 l_ptr->fsm_msg_cnt++;
734 link_set_timer(l_ptr, cont_intv);
735 break;
736 case STARTING_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 l_ptr->started = 1;
738 /* fall through */
739 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100740 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 l_ptr->fsm_msg_cnt++;
742 link_set_timer(l_ptr, cont_intv);
743 break;
744 default:
745 err("Unknown link event %u in RU state\n", event);
746 }
747 break;
748 case RESET_RESET:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 switch (event) {
750 case TRAFFIC_MSG_EVT:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 case ACTIVATE_MSG:
752 other = l_ptr->owner->active_links[0];
Allan Stephens8d64a5b2010-12-31 18:59:27 +0000753 if (other && link_working_unknown(other))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 l_ptr->state = WORKING_WORKING;
756 l_ptr->fsm_msg_cnt = 0;
757 link_activate(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +0100758 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 l_ptr->fsm_msg_cnt++;
760 link_set_timer(l_ptr, cont_intv);
761 break;
762 case RESET_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 break;
764 case TIMEOUT_EVT:
Per Liden4323add2006-01-18 00:38:21 +0100765 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 l_ptr->fsm_msg_cnt++;
767 link_set_timer(l_ptr, cont_intv);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 break;
769 default:
770 err("Unknown link event %u in RR state\n", event);
771 }
772 break;
773 default:
774 err("Unknown link state %u/%u\n", l_ptr->state, event);
775 }
776}
777
778/*
779 * link_bundle_buf(): Append contents of a buffer to
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900780 * the tail of an existing one.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 */
782
783static int link_bundle_buf(struct link *l_ptr,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900784 struct sk_buff *bundler,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 struct sk_buff *buf)
786{
787 struct tipc_msg *bundler_msg = buf_msg(bundler);
788 struct tipc_msg *msg = buf_msg(buf);
789 u32 size = msg_size(msg);
Allan Stephense49060c2006-06-29 12:32:46 -0700790 u32 bundle_size = msg_size(bundler_msg);
791 u32 to_pos = align(bundle_size);
792 u32 pad = to_pos - bundle_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793
794 if (msg_user(bundler_msg) != MSG_BUNDLER)
795 return 0;
796 if (msg_type(bundler_msg) != OPEN_MSG)
797 return 0;
Allan Stephense49060c2006-06-29 12:32:46 -0700798 if (skb_tailroom(bundler) < (pad + size))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 return 0;
Allan Stephens15e979d2010-05-11 14:30:10 +0000800 if (l_ptr->max_pkt < (to_pos + size))
Allan Stephens863fae62006-07-03 19:39:36 -0700801 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802
Allan Stephense49060c2006-06-29 12:32:46 -0700803 skb_put(bundler, pad + size);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300804 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 msg_set_size(bundler_msg, to_pos + size);
806 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 buf_discard(buf);
808 l_ptr->stats.sent_bundled++;
809 return 1;
810}
811
Sam Ravnborg05790c62006-03-20 22:37:04 -0800812static void link_add_to_outqueue(struct link *l_ptr,
813 struct sk_buff *buf,
814 struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815{
816 u32 ack = mod(l_ptr->next_in_no - 1);
817 u32 seqno = mod(l_ptr->next_out_no++);
818
819 msg_set_word(msg, 2, ((ack << 16) | seqno));
820 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
821 buf->next = NULL;
822 if (l_ptr->first_out) {
823 l_ptr->last_out->next = buf;
824 l_ptr->last_out = buf;
825 } else
826 l_ptr->first_out = l_ptr->last_out = buf;
827 l_ptr->out_queue_size++;
828}
829
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900830/*
831 * tipc_link_send_buf() is the 'full path' for messages, called from
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 * inside TIPC when the 'fast path' in tipc_send_buf
833 * has failed, and from link_send()
834 */
835
Per Liden4323add2006-01-18 00:38:21 +0100836int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837{
838 struct tipc_msg *msg = buf_msg(buf);
839 u32 size = msg_size(msg);
840 u32 dsz = msg_data_sz(msg);
841 u32 queue_size = l_ptr->out_queue_size;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000842 u32 imp = tipc_msg_tot_importance(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 u32 queue_limit = l_ptr->queue_limit[imp];
Allan Stephens15e979d2010-05-11 14:30:10 +0000844 u32 max_packet = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845
846 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
847
848 /* Match msg importance against queue limits: */
849
850 if (unlikely(queue_size >= queue_limit)) {
851 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
852 return link_schedule_port(l_ptr, msg_origport(msg),
853 size);
854 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 buf_discard(buf);
856 if (imp > CONN_MANAGER) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700857 warn("Resetting link <%s>, send queue full", l_ptr->name);
Per Liden4323add2006-01-18 00:38:21 +0100858 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 }
860 return dsz;
861 }
862
863 /* Fragmentation needed ? */
864
865 if (size > max_packet)
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000866 return link_send_long_buf(l_ptr, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867
868 /* Packet can be queued or sent: */
869
870 if (queue_size > l_ptr->stats.max_queue_sz)
871 l_ptr->stats.max_queue_sz = queue_size;
872
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 !link_congested(l_ptr))) {
875 link_add_to_outqueue(l_ptr, buf, msg);
876
Per Liden4323add2006-01-18 00:38:21 +0100877 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 l_ptr->unacked_window = 0;
879 } else {
Per Liden4323add2006-01-18 00:38:21 +0100880 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 l_ptr->stats.bearer_congs++;
882 l_ptr->next_out = buf;
883 }
884 return dsz;
885 }
886 /* Congestion: can message be bundled ?: */
887
888 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
889 (msg_user(msg) != MSG_FRAGMENTER)) {
890
891 /* Try adding message to an existing bundle */
892
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893 if (l_ptr->next_out &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
Per Liden4323add2006-01-18 00:38:21 +0100895 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 return dsz;
897 }
898
899 /* Try creating a new bundle */
900
901 if (size <= max_packet * 2 / 3) {
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000902 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 struct tipc_msg bundler_hdr;
904
905 if (bundler) {
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000906 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
Allan Stephens75715212008-06-04 17:37:34 -0700907 INT_H_SIZE, l_ptr->addr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300908 skb_copy_to_linear_data(bundler, &bundler_hdr,
909 INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 skb_trim(bundler, INT_H_SIZE);
911 link_bundle_buf(l_ptr, bundler, buf);
912 buf = bundler;
913 msg = buf_msg(buf);
914 l_ptr->stats.sent_bundles++;
915 }
916 }
917 }
918 if (!l_ptr->next_out)
919 l_ptr->next_out = buf;
920 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +0100921 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 return dsz;
923}
924
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900925/*
926 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 * not been selected yet, and the the owner node is not locked
928 * Called by TIPC internal users, e.g. the name distributor
929 */
930
Per Liden4323add2006-01-18 00:38:21 +0100931int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932{
933 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700934 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 int res = -ELINKCONG;
936
Per Liden4323add2006-01-18 00:38:21 +0100937 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +0000938 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +0100940 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 l_ptr = n_ptr->active_links[selector & 1];
Allan Stephensa0168922010-12-31 18:59:35 +0000942 if (l_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100943 res = tipc_link_send_buf(l_ptr, buf);
Allan Stephensa0168922010-12-31 18:59:35 +0000944 else
Allan Stephensc33d53b2006-06-25 23:50:30 -0700945 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100946 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 buf_discard(buf);
949 }
Per Liden4323add2006-01-18 00:38:21 +0100950 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 return res;
952}
953
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900954/*
955 * link_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 * destination link is known and the header is complete,
957 * inclusive total message length. Very time critical.
958 * Link is locked. Returns user data length.
959 */
960
Sam Ravnborg05790c62006-03-20 22:37:04 -0800961static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
962 u32 *used_max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963{
964 struct tipc_msg *msg = buf_msg(buf);
965 int res = msg_data_sz(msg);
966
967 if (likely(!link_congested(l_ptr))) {
Allan Stephens15e979d2010-05-11 14:30:10 +0000968 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
970 link_add_to_outqueue(l_ptr, buf, msg);
Per Liden4323add2006-01-18 00:38:21 +0100971 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
972 &l_ptr->media_addr))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 l_ptr->unacked_window = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 return res;
975 }
Per Liden4323add2006-01-18 00:38:21 +0100976 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 l_ptr->stats.bearer_congs++;
978 l_ptr->next_out = buf;
979 return res;
980 }
Allan Stephens0e659672010-12-31 18:59:32 +0000981 } else
Allan Stephens15e979d2010-05-11 14:30:10 +0000982 *used_max_pkt = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 }
Per Liden4323add2006-01-18 00:38:21 +0100984 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985}
986
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900987/*
988 * tipc_send_buf_fast: Entry for data messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 * destination node is known and the header is complete,
990 * inclusive total message length.
991 * Returns user data length.
992 */
993int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
994{
995 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -0700996 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997 int res;
998 u32 selector = msg_origport(buf_msg(buf)) & 1;
999 u32 dummy;
1000
1001 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001002 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003
Per Liden4323add2006-01-18 00:38:21 +01001004 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001005 n_ptr = tipc_node_find(destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 if (likely(n_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001007 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 l_ptr = n_ptr->active_links[selector];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 if (likely(l_ptr)) {
1010 res = link_send_buf_fast(l_ptr, buf, &dummy);
Per Liden4323add2006-01-18 00:38:21 +01001011 tipc_node_unlock(n_ptr);
1012 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 return res;
1014 }
Per Liden4323add2006-01-18 00:38:21 +01001015 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 }
Per Liden4323add2006-01-18 00:38:21 +01001017 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 res = msg_data_sz(buf_msg(buf));
1019 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1020 return res;
1021}
1022
1023
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024/*
1025 * tipc_link_send_sections_fast: Entry for messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 * destination processor is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027 * except for total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 * Returns user data length or errno.
1029 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001030int tipc_link_send_sections_fast(struct tipc_port *sender,
Per Liden4323add2006-01-18 00:38:21 +01001031 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001032 const u32 num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001033 u32 destaddr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034{
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001035 struct tipc_msg *hdr = &sender->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 struct link *l_ptr;
1037 struct sk_buff *buf;
David S. Miller6c000552008-09-02 23:38:32 -07001038 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 int res;
1040 u32 selector = msg_origport(hdr) & 1;
1041
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042again:
1043 /*
1044 * Try building message using port's max_pkt hint.
1045 * (Must not hold any locks while building message.)
1046 */
1047
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001048 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 !sender->user_port, &buf);
1050
Per Liden4323add2006-01-18 00:38:21 +01001051 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001052 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001054 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 l_ptr = node->active_links[selector];
1056 if (likely(l_ptr)) {
1057 if (likely(buf)) {
1058 res = link_send_buf_fast(l_ptr, buf,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001059 &sender->max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 if (unlikely(res < 0))
1061 buf_discard(buf);
1062exit:
Per Liden4323add2006-01-18 00:38:21 +01001063 tipc_node_unlock(node);
1064 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 return res;
1066 }
1067
1068 /* Exit if build request was invalid */
1069
1070 if (unlikely(res < 0))
1071 goto exit;
1072
1073 /* Exit if link (or bearer) is congested */
1074
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001075 if (link_congested(l_ptr) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 !list_empty(&l_ptr->b_ptr->cong_links)) {
1077 res = link_schedule_port(l_ptr,
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001078 sender->ref, res);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 goto exit;
1080 }
1081
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001082 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 * Message size exceeds max_pkt hint; update hint,
1084 * then re-try fast path or fragment the message
1085 */
1086
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001087 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001088 tipc_node_unlock(node);
1089 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090
1091
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001092 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 goto again;
1094
1095 return link_send_sections_long(sender, msg_sect,
1096 num_sect, destaddr);
1097 }
Per Liden4323add2006-01-18 00:38:21 +01001098 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099 }
Per Liden4323add2006-01-18 00:38:21 +01001100 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101
1102 /* Couldn't find a link to the destination node */
1103
1104 if (buf)
1105 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1106 if (res >= 0)
Per Liden4323add2006-01-18 00:38:21 +01001107 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1108 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 return res;
1110}
1111
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001112/*
1113 * link_send_sections_long(): Entry for long messages where the
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 * destination node is known and the header is complete,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001115 * inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 * Link and bearer congestion status have been checked to be ok,
1117 * and are ignored if they change.
1118 *
1119 * Note that fragments do not use the full link MTU so that they won't have
1120 * to undergo refragmentation if link changeover causes them to be sent
1121 * over another link with an additional tunnel header added as prefix.
1122 * (Refragmentation will still occur if the other link has a smaller MTU.)
1123 *
1124 * Returns user data length or errno.
1125 */
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001126static int link_send_sections_long(struct tipc_port *sender,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 struct iovec const *msg_sect,
1128 u32 num_sect,
1129 u32 destaddr)
1130{
1131 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07001132 struct tipc_node *node;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001133 struct tipc_msg *hdr = &sender->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 u32 dsz = msg_data_sz(hdr);
Allan Stephens0e659672010-12-31 18:59:32 +00001135 u32 max_pkt, fragm_sz, rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 struct tipc_msg fragm_hdr;
Allan Stephens0e659672010-12-31 18:59:32 +00001137 struct sk_buff *buf, *buf_chain, *prev;
1138 u32 fragm_crs, fragm_rest, hsz, sect_rest;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 const unchar *sect_crs;
1140 int curr_sect;
1141 u32 fragm_no;
1142
1143again:
1144 fragm_no = 1;
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001145 max_pkt = sender->max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 /* leave room for tunnel header in case of link changeover */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001147 fragm_sz = max_pkt - INT_H_SIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 /* leave room for fragmentation header in each fragment */
1149 rest = dsz;
1150 fragm_crs = 0;
1151 fragm_rest = 0;
1152 sect_rest = 0;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001153 sect_crs = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 curr_sect = -1;
1155
1156 /* Prepare reusable fragment header: */
1157
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001158 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07001159 INT_H_SIZE, msg_destnode(hdr));
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001160 msg_set_link_selector(&fragm_hdr, sender->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 msg_set_size(&fragm_hdr, max_pkt);
1162 msg_set_fragm_no(&fragm_hdr, 1);
1163
1164 /* Prepare header of first fragment: */
1165
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001166 buf_chain = buf = tipc_buf_acquire(max_pkt);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 if (!buf)
1168 return -ENOMEM;
1169 buf->next = NULL;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001170 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 hsz = msg_hdr_sz(hdr);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001172 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173
1174 /* Chop up message: */
1175
1176 fragm_crs = INT_H_SIZE + hsz;
1177 fragm_rest = fragm_sz - hsz;
1178
1179 do { /* For all sections */
1180 u32 sz;
1181
1182 if (!sect_rest) {
1183 sect_rest = msg_sect[++curr_sect].iov_len;
1184 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1185 }
1186
1187 if (sect_rest < fragm_rest)
1188 sz = sect_rest;
1189 else
1190 sz = fragm_rest;
1191
1192 if (likely(!sender->user_port)) {
1193 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1194error:
1195 for (; buf_chain; buf_chain = buf) {
1196 buf = buf_chain->next;
1197 buf_discard(buf_chain);
1198 }
1199 return -EFAULT;
1200 }
1201 } else
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001202 skb_copy_to_linear_data_offset(buf, fragm_crs,
1203 sect_crs, sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 sect_crs += sz;
1205 sect_rest -= sz;
1206 fragm_crs += sz;
1207 fragm_rest -= sz;
1208 rest -= sz;
1209
1210 if (!fragm_rest && rest) {
1211
1212 /* Initiate new fragment: */
1213 if (rest <= fragm_sz) {
1214 fragm_sz = rest;
Allan Stephens0e659672010-12-31 18:59:32 +00001215 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 } else {
1217 msg_set_type(&fragm_hdr, FRAGMENT);
1218 }
1219 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1220 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1221 prev = buf;
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001222 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 if (!buf)
1224 goto error;
1225
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001226 buf->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 prev->next = buf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001228 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 fragm_crs = INT_H_SIZE;
1230 fragm_rest = fragm_sz;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 }
Allan Stephens0e659672010-12-31 18:59:32 +00001232 } while (rest > 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001234 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 * Now we have a buffer chain. Select a link and check
1236 * that packet size is still OK
1237 */
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001238 node = tipc_node_find(destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 if (likely(node)) {
Per Liden4323add2006-01-18 00:38:21 +01001240 tipc_node_lock(node);
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001241 l_ptr = node->active_links[sender->ref & 1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01001243 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244 goto reject;
1245 }
Allan Stephens15e979d2010-05-11 14:30:10 +00001246 if (l_ptr->max_pkt < max_pkt) {
Allan Stephens23dd4cc2011-01-07 11:43:40 -05001247 sender->max_pkt = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01001248 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 for (; buf_chain; buf_chain = buf) {
1250 buf = buf_chain->next;
1251 buf_discard(buf_chain);
1252 }
1253 goto again;
1254 }
1255 } else {
1256reject:
1257 for (; buf_chain; buf_chain = buf) {
1258 buf = buf_chain->next;
1259 buf_discard(buf_chain);
1260 }
Per Liden4323add2006-01-18 00:38:21 +01001261 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1262 TIPC_ERR_NO_NODE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 }
1264
1265 /* Append whole chain to send queue: */
1266
1267 buf = buf_chain;
1268 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1269 if (!l_ptr->next_out)
1270 l_ptr->next_out = buf_chain;
1271 l_ptr->stats.sent_fragmented++;
1272 while (buf) {
1273 struct sk_buff *next = buf->next;
1274 struct tipc_msg *msg = buf_msg(buf);
1275
1276 l_ptr->stats.sent_fragments++;
1277 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1278 link_add_to_outqueue(l_ptr, buf, msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 buf = next;
1280 }
1281
1282 /* Send it, if possible: */
1283
Per Liden4323add2006-01-18 00:38:21 +01001284 tipc_link_push_queue(l_ptr);
1285 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 return dsz;
1287}
1288
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001289/*
Per Liden4323add2006-01-18 00:38:21 +01001290 * tipc_link_push_packet: Push one unsent packet to the media
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 */
Per Liden4323add2006-01-18 00:38:21 +01001292u32 tipc_link_push_packet(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293{
1294 struct sk_buff *buf = l_ptr->first_out;
1295 u32 r_q_size = l_ptr->retransm_queue_size;
1296 u32 r_q_head = l_ptr->retransm_queue_head;
1297
1298 /* Step to position where retransmission failed, if any, */
1299 /* consider that buffers may have been released in meantime */
1300
1301 if (r_q_size && buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001302 u32 last = lesser(mod(r_q_head + r_q_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 link_last_sent(l_ptr));
1304 u32 first = msg_seqno(buf_msg(buf));
1305
1306 while (buf && less(first, r_q_head)) {
1307 first = mod(first + 1);
1308 buf = buf->next;
1309 }
1310 l_ptr->retransm_queue_head = r_q_head = first;
1311 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1312 }
1313
1314 /* Continue retransmission now, if there is anything: */
1315
Neil Hormanca509102010-03-15 07:58:45 +00001316 if (r_q_size && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001318 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001319 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 l_ptr->retransm_queue_head = mod(++r_q_head);
1321 l_ptr->retransm_queue_size = --r_q_size;
1322 l_ptr->stats.retransmitted++;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001323 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 } else {
1325 l_ptr->stats.bearer_congs++;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 return PUSH_FAILED;
1327 }
1328 }
1329
1330 /* Send deferred protocol message, if any: */
1331
1332 buf = l_ptr->proto_msg_queue;
1333 if (buf) {
1334 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
Allan Stephens0e659672010-12-31 18:59:32 +00001335 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001336 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 l_ptr->unacked_window = 0;
1338 buf_discard(buf);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001339 l_ptr->proto_msg_queue = NULL;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001340 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 l_ptr->stats.bearer_congs++;
1343 return PUSH_FAILED;
1344 }
1345 }
1346
1347 /* Send one deferred data message, if send window not full: */
1348
1349 buf = l_ptr->next_out;
1350 if (buf) {
1351 struct tipc_msg *msg = buf_msg(buf);
1352 u32 next = msg_seqno(msg);
1353 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1354
1355 if (mod(next - first) < l_ptr->queue_limit[0]) {
1356 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001357 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001358 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 if (msg_user(msg) == MSG_BUNDLER)
1360 msg_set_type(msg, CLOSED_MSG);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 l_ptr->next_out = buf->next;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001362 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 l_ptr->stats.bearer_congs++;
1365 return PUSH_FAILED;
1366 }
1367 }
1368 }
1369 return PUSH_FINISHED;
1370}
1371
1372/*
1373 * push_queue(): push out the unsent messages of a link where
1374 * congestion has abated. Node is locked
1375 */
Per Liden4323add2006-01-18 00:38:21 +01001376void tipc_link_push_queue(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377{
1378 u32 res;
1379
Per Liden4323add2006-01-18 00:38:21 +01001380 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 return;
1382
1383 do {
Per Liden4323add2006-01-18 00:38:21 +01001384 res = tipc_link_push_packet(l_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001385 } while (!res);
1386
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 if (res == PUSH_FAILED)
Per Liden4323add2006-01-18 00:38:21 +01001388 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389}
1390
Allan Stephensd356eeb2006-06-25 23:40:01 -07001391static void link_reset_all(unsigned long addr)
1392{
David S. Miller6c000552008-09-02 23:38:32 -07001393 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001394 char addr_string[16];
1395 u32 i;
1396
1397 read_lock_bh(&tipc_net_lock);
1398 n_ptr = tipc_node_find((u32)addr);
1399 if (!n_ptr) {
1400 read_unlock_bh(&tipc_net_lock);
1401 return; /* node no longer exists */
1402 }
1403
1404 tipc_node_lock(n_ptr);
1405
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001406 warn("Resetting all links to %s\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001407 tipc_addr_string_fill(addr_string, n_ptr->addr));
Allan Stephensd356eeb2006-06-25 23:40:01 -07001408
1409 for (i = 0; i < MAX_BEARERS; i++) {
1410 if (n_ptr->links[i]) {
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001411 link_print(n_ptr->links[i], "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001412 tipc_link_reset(n_ptr->links[i]);
1413 }
1414 }
1415
1416 tipc_node_unlock(n_ptr);
1417 read_unlock_bh(&tipc_net_lock);
1418}
1419
1420static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1421{
1422 struct tipc_msg *msg = buf_msg(buf);
1423
1424 warn("Retransmission failure on link <%s>\n", l_ptr->name);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001425
1426 if (l_ptr->addr) {
1427
1428 /* Handle failure on standard link */
1429
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001430 link_print(l_ptr, "Resetting link\n");
Allan Stephensd356eeb2006-06-25 23:40:01 -07001431 tipc_link_reset(l_ptr);
1432
1433 } else {
1434
1435 /* Handle failure on broadcast link */
1436
David S. Miller6c000552008-09-02 23:38:32 -07001437 struct tipc_node *n_ptr;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001438 char addr_string[16];
1439
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001440 info("Msg seq number: %u, ", msg_seqno(msg));
1441 info("Outstanding acks: %lu\n",
1442 (unsigned long) TIPC_SKB_CB(buf)->handle);
Jeff Garzik617dbea2006-10-03 16:25:34 -07001443
Allan Stephensd356eeb2006-06-25 23:40:01 -07001444 n_ptr = l_ptr->owner->next;
1445 tipc_node_lock(n_ptr);
1446
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001447 tipc_addr_string_fill(addr_string, n_ptr->addr);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00001448 info("Multicast link info for %s\n", addr_string);
1449 info("Supported: %d, ", n_ptr->bclink.supported);
1450 info("Acked: %u\n", n_ptr->bclink.acked);
1451 info("Last in: %u, ", n_ptr->bclink.last_in);
1452 info("Gap after: %u, ", n_ptr->bclink.gap_after);
1453 info("Gap to: %u\n", n_ptr->bclink.gap_to);
1454 info("Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001455
1456 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1457
1458 tipc_node_unlock(n_ptr);
1459
1460 l_ptr->stale_count = 0;
1461 }
1462}
1463
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001464void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
Per Liden4323add2006-01-18 00:38:21 +01001465 u32 retransmits)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466{
1467 struct tipc_msg *msg;
1468
Allan Stephensd356eeb2006-06-25 23:40:01 -07001469 if (!buf)
1470 return;
1471
1472 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001473
Allan Stephensd356eeb2006-06-25 23:40:01 -07001474 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Neil Hormanca509102010-03-15 07:58:45 +00001475 if (l_ptr->retransm_queue_size == 0) {
Allan Stephensd356eeb2006-06-25 23:40:01 -07001476 l_ptr->retransm_queue_head = msg_seqno(msg);
1477 l_ptr->retransm_queue_size = retransmits;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001478 } else {
Neil Hormanca509102010-03-15 07:58:45 +00001479 err("Unexpected retransmit on link %s (qsize=%d)\n",
1480 l_ptr->name, l_ptr->retransm_queue_size);
Allan Stephensd356eeb2006-06-25 23:40:01 -07001481 }
Neil Hormanca509102010-03-15 07:58:45 +00001482 return;
Allan Stephensd356eeb2006-06-25 23:40:01 -07001483 } else {
1484 /* Detect repeated retransmit failures on uncongested bearer */
1485
1486 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1487 if (++l_ptr->stale_count > 100) {
1488 link_retransmit_failure(l_ptr, buf);
1489 return;
1490 }
1491 } else {
1492 l_ptr->last_retransmitted = msg_seqno(msg);
1493 l_ptr->stale_count = 1;
1494 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001496
Neil Hormanca509102010-03-15 07:58:45 +00001497 while (retransmits && (buf != l_ptr->next_out) && buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 msg = buf_msg(buf);
1499 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001500 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Liden4323add2006-01-18 00:38:21 +01001501 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 buf = buf->next;
1503 retransmits--;
1504 l_ptr->stats.retransmitted++;
1505 } else {
Per Liden4323add2006-01-18 00:38:21 +01001506 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 l_ptr->stats.bearer_congs++;
1508 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1509 l_ptr->retransm_queue_size = retransmits;
1510 return;
1511 }
1512 }
Allan Stephensd356eeb2006-06-25 23:40:01 -07001513
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1515}
1516
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001517/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 * link_insert_deferred_queue - insert deferred messages back into receive chain
1519 */
1520
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001521static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522 struct sk_buff *buf)
1523{
1524 u32 seq_no;
1525
1526 if (l_ptr->oldest_deferred_in == NULL)
1527 return buf;
1528
1529 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1530 if (seq_no == mod(l_ptr->next_in_no)) {
1531 l_ptr->newest_deferred_in->next = buf;
1532 buf = l_ptr->oldest_deferred_in;
1533 l_ptr->oldest_deferred_in = NULL;
1534 l_ptr->deferred_inqueue_sz = 0;
1535 }
1536 return buf;
1537}
1538
Allan Stephens85035562008-04-15 19:04:54 -07001539/**
1540 * link_recv_buf_validate - validate basic format of received message
1541 *
1542 * This routine ensures a TIPC message has an acceptable header, and at least
1543 * as much data as the header indicates it should. The routine also ensures
1544 * that the entire message header is stored in the main fragment of the message
1545 * buffer, to simplify future access to message header fields.
1546 *
1547 * Note: Having extra info present in the message header or data areas is OK.
1548 * TIPC will ignore the excess, under the assumption that it is optional info
1549 * introduced by a later release of the protocol.
1550 */
1551
1552static int link_recv_buf_validate(struct sk_buff *buf)
1553{
1554 static u32 min_data_hdr_size[8] = {
1555 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1556 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1557 };
1558
1559 struct tipc_msg *msg;
1560 u32 tipc_hdr[2];
1561 u32 size;
1562 u32 hdr_size;
1563 u32 min_hdr_size;
1564
1565 if (unlikely(buf->len < MIN_H_SIZE))
1566 return 0;
1567
1568 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1569 if (msg == NULL)
1570 return 0;
1571
1572 if (unlikely(msg_version(msg) != TIPC_VERSION))
1573 return 0;
1574
1575 size = msg_size(msg);
1576 hdr_size = msg_hdr_sz(msg);
1577 min_hdr_size = msg_isdata(msg) ?
1578 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1579
1580 if (unlikely((hdr_size < min_hdr_size) ||
1581 (size < hdr_size) ||
1582 (buf->len < size) ||
1583 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1584 return 0;
1585
1586 return pskb_may_pull(buf, hdr_size);
1587}
1588
Allan Stephensb02b69c2010-08-17 11:00:07 +00001589/**
1590 * tipc_recv_msg - process TIPC messages arriving from off-node
1591 * @head: pointer to message buffer chain
1592 * @tb_ptr: pointer to bearer message arrived on
1593 *
1594 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1595 * structure (i.e. cannot be NULL), but bearer can be inactive.
1596 */
1597
Allan Stephens2d627b92011-01-07 13:00:11 -05001598void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599{
Per Liden4323add2006-01-18 00:38:21 +01001600 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001601 while (head) {
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
Allan Stephensa0168922010-12-31 18:59:35 +00001626 if (unlikely(buf_linearize(buf)))
Allan Stephensfe13dda2008-04-15 19:03:23 -07001627 goto cont;
Allan Stephensfe13dda2008-04-15 19:03:23 -07001628
Allan Stephens85035562008-04-15 19:04:54 -07001629 /* Handle arrival of a non-unicast link message */
1630
1631 msg = buf_msg(buf);
1632
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633 if (unlikely(msg_non_seq(msg))) {
Allan Stephens1265a022008-06-04 17:32:35 -07001634 if (msg_user(msg) == LINK_CONFIG)
1635 tipc_disc_recv_msg(buf, b_ptr);
1636 else
1637 tipc_bclink_recv_pkt(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 continue;
1639 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001640
Allan Stephens26008242006-06-25 23:39:31 -07001641 if (unlikely(!msg_short(msg) &&
1642 (msg_destnode(msg) != tipc_own_addr)))
1643 goto cont;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001644
Neil Hormande586572010-03-08 12:43:56 -08001645 /* Discard non-routeable messages destined for another node */
1646
1647 if (unlikely(!msg_isdata(msg) &&
1648 (msg_destnode(msg) != tipc_own_addr))) {
1649 if ((msg_user(msg) != CONN_MANAGER) &&
1650 (msg_user(msg) != MSG_FRAGMENTER))
1651 goto cont;
1652 }
1653
Allan Stephens5a68d5ee2010-08-17 11:00:16 +00001654 /* Locate neighboring node that sent message */
Allan Stephens85035562008-04-15 19:04:54 -07001655
Per Liden4323add2006-01-18 00:38:21 +01001656 n_ptr = tipc_node_find(msg_prevnode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657 if (unlikely(!n_ptr))
1658 goto cont;
Per Liden4323add2006-01-18 00:38:21 +01001659 tipc_node_lock(n_ptr);
Allan Stephens85035562008-04-15 19:04:54 -07001660
Allan Stephens5a68d5ee2010-08-17 11:00:16 +00001661 /* Don't talk to neighbor during cleanup after last session */
1662
1663 if (n_ptr->cleanup_required) {
1664 tipc_node_unlock(n_ptr);
1665 goto cont;
1666 }
1667
1668 /* Locate unicast link endpoint that should handle message */
1669
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 l_ptr = n_ptr->links[b_ptr->identity];
1671 if (unlikely(!l_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +01001672 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673 goto cont;
1674 }
Allan Stephens85035562008-04-15 19:04:54 -07001675
1676 /* Validate message sequence number info */
1677
1678 seq_no = msg_seqno(msg);
1679 ackd = msg_ack(msg);
1680
1681 /* Release acked messages */
1682
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001684 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1685 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 }
1687
1688 crs = l_ptr->first_out;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001689 while ((crs != l_ptr->next_out) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1691 struct sk_buff *next = crs->next;
1692
1693 buf_discard(crs);
1694 crs = next;
1695 released++;
1696 }
1697 if (released) {
1698 l_ptr->first_out = crs;
1699 l_ptr->out_queue_size -= released;
1700 }
Allan Stephens85035562008-04-15 19:04:54 -07001701
1702 /* Try sending any messages link endpoint has pending */
1703
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704 if (unlikely(l_ptr->next_out))
Per Liden4323add2006-01-18 00:38:21 +01001705 tipc_link_push_queue(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
Per Liden4323add2006-01-18 00:38:21 +01001707 tipc_link_wakeup_ports(l_ptr, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1709 l_ptr->stats.sent_acks++;
Per Liden4323add2006-01-18 00:38:21 +01001710 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 }
1712
Allan Stephens85035562008-04-15 19:04:54 -07001713 /* Now (finally!) process the incoming message */
1714
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715protocol_check:
1716 if (likely(link_working_working(l_ptr))) {
1717 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1718 l_ptr->next_in_no++;
1719 if (unlikely(l_ptr->oldest_deferred_in))
1720 head = link_insert_deferred_queue(l_ptr,
1721 head);
1722 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1723deliver:
1724 if (likely(msg_isdata(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01001725 tipc_node_unlock(n_ptr);
1726 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 continue;
1728 }
1729 switch (msg_user(msg)) {
1730 case MSG_BUNDLER:
1731 l_ptr->stats.recv_bundles++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001732 l_ptr->stats.recv_bundled +=
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +01001734 tipc_node_unlock(n_ptr);
1735 tipc_link_recv_bundle(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736 continue;
1737 case ROUTE_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001738 tipc_node_unlock(n_ptr);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00001739 buf_discard(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 continue;
1741 case NAME_DISTRIBUTOR:
Per Liden4323add2006-01-18 00:38:21 +01001742 tipc_node_unlock(n_ptr);
1743 tipc_named_recv(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 continue;
1745 case CONN_MANAGER:
Per Liden4323add2006-01-18 00:38:21 +01001746 tipc_node_unlock(n_ptr);
1747 tipc_port_recv_proto_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 continue;
1749 case MSG_FRAGMENTER:
1750 l_ptr->stats.recv_fragments++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001751 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
Per Liden4323add2006-01-18 00:38:21 +01001752 &buf, &msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753 l_ptr->stats.recv_fragmented++;
1754 goto deliver;
1755 }
1756 break;
1757 case CHANGEOVER_PROTOCOL:
1758 type = msg_type(msg);
Per Liden4323add2006-01-18 00:38:21 +01001759 if (link_recv_changeover_msg(&l_ptr, &buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 msg = buf_msg(buf);
1761 seq_no = msg_seqno(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762 if (type == ORIGINAL_MSG)
1763 goto deliver;
1764 goto protocol_check;
1765 }
1766 break;
1767 }
1768 }
Per Liden4323add2006-01-18 00:38:21 +01001769 tipc_node_unlock(n_ptr);
1770 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 continue;
1772 }
1773 link_handle_out_of_seq_msg(l_ptr, buf);
1774 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001775 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 continue;
1777 }
1778
1779 if (msg_user(msg) == LINK_PROTOCOL) {
1780 link_recv_proto_msg(l_ptr, buf);
1781 head = link_insert_deferred_queue(l_ptr, head);
Per Liden4323add2006-01-18 00:38:21 +01001782 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 continue;
1784 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001785 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1786
1787 if (link_working_working(l_ptr)) {
1788 /* Re-insert in front of queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 buf->next = head;
1790 head = buf;
Per Liden4323add2006-01-18 00:38:21 +01001791 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792 continue;
1793 }
Per Liden4323add2006-01-18 00:38:21 +01001794 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795cont:
1796 buf_discard(buf);
1797 }
Per Liden4323add2006-01-18 00:38:21 +01001798 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001799}
1800
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001801/*
1802 * link_defer_buf(): Sort a received out-of-sequence packet
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803 * into the deferred reception queue.
1804 * Returns the increase of the queue length,i.e. 0 or 1
1805 */
1806
Per Liden4323add2006-01-18 00:38:21 +01001807u32 tipc_link_defer_pkt(struct sk_buff **head,
1808 struct sk_buff **tail,
1809 struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001810{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001811 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 struct sk_buff *crs = *head;
1813 u32 seq_no = msg_seqno(buf_msg(buf));
1814
1815 buf->next = NULL;
1816
1817 /* Empty queue ? */
1818 if (*head == NULL) {
1819 *head = *tail = buf;
1820 return 1;
1821 }
1822
1823 /* Last ? */
1824 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1825 (*tail)->next = buf;
1826 *tail = buf;
1827 return 1;
1828 }
1829
1830 /* Scan through queue and sort it in */
1831 do {
1832 struct tipc_msg *msg = buf_msg(crs);
1833
1834 if (less(seq_no, msg_seqno(msg))) {
1835 buf->next = crs;
1836 if (prev)
1837 prev->next = buf;
1838 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001839 *head = buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 return 1;
1841 }
Allan Stephensa0168922010-12-31 18:59:35 +00001842 if (seq_no == msg_seqno(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 prev = crs;
1845 crs = crs->next;
Allan Stephens0e659672010-12-31 18:59:32 +00001846 } while (crs);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847
1848 /* Message is a duplicate of an existing message */
1849
1850 buf_discard(buf);
1851 return 0;
1852}
1853
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001854/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1856 */
1857
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001858static void link_handle_out_of_seq_msg(struct link *l_ptr,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 struct sk_buff *buf)
1860{
1861 u32 seq_no = msg_seqno(buf_msg(buf));
1862
1863 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1864 link_recv_proto_msg(l_ptr, buf);
1865 return;
1866 }
1867
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 /* Record OOS packet arrival (force mismatch on next timeout) */
1869
1870 l_ptr->checkpoint--;
1871
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001872 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 * Discard packet if a duplicate; otherwise add it to deferred queue
1874 * and notify peer of gap as per protocol specification
1875 */
1876
1877 if (less(seq_no, mod(l_ptr->next_in_no))) {
1878 l_ptr->stats.duplicates++;
1879 buf_discard(buf);
1880 return;
1881 }
1882
Per Liden4323add2006-01-18 00:38:21 +01001883 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1884 &l_ptr->newest_deferred_in, buf)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 l_ptr->deferred_inqueue_sz++;
1886 l_ptr->stats.deferred_recv++;
1887 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
Per Liden4323add2006-01-18 00:38:21 +01001888 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 } else
1890 l_ptr->stats.duplicates++;
1891}
1892
1893/*
1894 * Send protocol message to the other endpoint.
1895 */
Per Liden4323add2006-01-18 00:38:21 +01001896void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
1897 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001899 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 struct tipc_msg *msg = l_ptr->pmsg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001901 u32 msg_size = sizeof(l_ptr->proto_msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902
1903 if (link_blocked(l_ptr))
1904 return;
1905 msg_set_type(msg, msg_typ);
1906 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001907 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
Per Liden4323add2006-01-18 00:38:21 +01001908 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909
1910 if (msg_typ == STATE_MSG) {
1911 u32 next_sent = mod(l_ptr->next_out_no);
1912
Per Liden4323add2006-01-18 00:38:21 +01001913 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914 return;
1915 if (l_ptr->next_out)
1916 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
1917 msg_set_next_sent(msg, next_sent);
1918 if (l_ptr->oldest_deferred_in) {
1919 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1920 gap = mod(rec - mod(l_ptr->next_in_no));
1921 }
1922 msg_set_seq_gap(msg, gap);
1923 if (gap)
1924 l_ptr->stats.sent_nacks++;
1925 msg_set_link_tolerance(msg, tolerance);
1926 msg_set_linkprio(msg, priority);
1927 msg_set_max_pkt(msg, ack_mtu);
1928 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1929 msg_set_probe(msg, probe_msg != 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001930 if (probe_msg) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931 u32 mtu = l_ptr->max_pkt;
1932
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001933 if ((mtu < l_ptr->max_pkt_target) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 link_working_working(l_ptr) &&
1935 l_ptr->fsm_msg_cnt) {
1936 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001937 if (l_ptr->max_pkt_probes == 10) {
1938 l_ptr->max_pkt_target = (msg_size - 4);
1939 l_ptr->max_pkt_probes = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001941 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942 l_ptr->max_pkt_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001943 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001944
1945 l_ptr->stats.sent_probes++;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001946 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947 l_ptr->stats.sent_states++;
1948 } else { /* RESET_MSG or ACTIVATE_MSG */
1949 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1950 msg_set_seq_gap(msg, 0);
1951 msg_set_next_sent(msg, 1);
1952 msg_set_link_tolerance(msg, l_ptr->tolerance);
1953 msg_set_linkprio(msg, l_ptr->priority);
1954 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1955 }
1956
Allan Stephensa0168922010-12-31 18:59:35 +00001957 if (tipc_node_has_redundant_links(l_ptr->owner))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958 msg_set_redundant_link(msg);
Allan Stephensa0168922010-12-31 18:59:35 +00001959 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960 msg_clear_redundant_link(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961 msg_set_linkprio(msg, l_ptr->priority);
1962
1963 /* Ensure sequence number will not fit : */
1964
1965 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1966
1967 /* Congestion? */
1968
Per Liden4323add2006-01-18 00:38:21 +01001969 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970 if (!l_ptr->proto_msg_queue) {
1971 l_ptr->proto_msg_queue =
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001972 tipc_buf_acquire(sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973 }
1974 buf = l_ptr->proto_msg_queue;
1975 if (!buf)
1976 return;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001977 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978 return;
1979 }
1980 msg_set_timestamp(msg, jiffies_to_msecs(jiffies));
1981
1982 /* Message can be sent */
1983
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001984 buf = tipc_buf_acquire(msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001985 if (!buf)
1986 return;
1987
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001988 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001989 msg_set_size(buf_msg(buf), msg_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990
Per Liden4323add2006-01-18 00:38:21 +01001991 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 l_ptr->unacked_window = 0;
1993 buf_discard(buf);
1994 return;
1995 }
1996
1997 /* New congestion */
Per Liden4323add2006-01-18 00:38:21 +01001998 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999 l_ptr->proto_msg_queue = buf;
2000 l_ptr->stats.bearer_congs++;
2001}
2002
2003/*
2004 * Receive protocol message :
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002005 * Note that network plane id propagates through the network, and may
2006 * change at any time. The node with lowest address rules
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007 */
2008
2009static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2010{
2011 u32 rec_gap = 0;
2012 u32 max_pkt_info;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002013 u32 max_pkt_ack;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002014 u32 msg_tol;
2015 struct tipc_msg *msg = buf_msg(buf);
2016
Per Lidenb97bf3f2006-01-02 19:04:38 +01002017 if (link_blocked(l_ptr))
2018 goto exit;
2019
2020 /* record unnumbered packet arrival (force mismatch on next timeout) */
2021
2022 l_ptr->checkpoint--;
2023
2024 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2025 if (tipc_own_addr > msg_prevnode(msg))
2026 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2027
2028 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2029
2030 switch (msg_type(msg)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002031
Per Lidenb97bf3f2006-01-02 19:04:38 +01002032 case RESET_MSG:
Allan Stephensa686e682008-06-04 17:29:39 -07002033 if (!link_working_unknown(l_ptr) &&
2034 (l_ptr->peer_session != INVALID_SESSION)) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002035 if (msg_session(msg) == l_ptr->peer_session)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 break; /* duplicate: ignore */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037 }
2038 /* fall thru' */
2039 case ACTIVATE_MSG:
2040 /* Update link settings according other endpoint's values */
2041
2042 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2043
Allan Stephens2db99832010-12-31 18:59:33 +00002044 msg_tol = msg_link_tolerance(msg);
2045 if (msg_tol > l_ptr->tolerance)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046 link_set_supervision_props(l_ptr, msg_tol);
2047
2048 if (msg_linkprio(msg) > l_ptr->priority)
2049 l_ptr->priority = msg_linkprio(msg);
2050
2051 max_pkt_info = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002052 if (max_pkt_info) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 if (max_pkt_info < l_ptr->max_pkt_target)
2054 l_ptr->max_pkt_target = max_pkt_info;
2055 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2056 l_ptr->max_pkt = l_ptr->max_pkt_target;
2057 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002058 l_ptr->max_pkt = l_ptr->max_pkt_target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059 }
2060 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2061
2062 link_state_event(l_ptr, msg_type(msg));
2063
2064 l_ptr->peer_session = msg_session(msg);
2065 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2066
2067 /* Synchronize broadcast sequence numbers */
Allan Stephensa0168922010-12-31 18:59:35 +00002068 if (!tipc_node_has_redundant_links(l_ptr->owner))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070 break;
2071 case STATE_MSG:
2072
Allan Stephens2db99832010-12-31 18:59:33 +00002073 msg_tol = msg_link_tolerance(msg);
2074 if (msg_tol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075 link_set_supervision_props(l_ptr, msg_tol);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002076
2077 if (msg_linkprio(msg) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078 (msg_linkprio(msg) != l_ptr->priority)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002079 warn("Resetting link <%s>, priority change %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2081 l_ptr->priority = msg_linkprio(msg);
Per Liden4323add2006-01-18 00:38:21 +01002082 tipc_link_reset(l_ptr); /* Enforce change to take effect */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083 break;
2084 }
2085 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2086 l_ptr->stats.recv_states++;
2087 if (link_reset_unknown(l_ptr))
2088 break;
2089
2090 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002091 rec_gap = mod(msg_next_sent(msg) -
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092 mod(l_ptr->next_in_no));
2093 }
2094
2095 max_pkt_ack = msg_max_pkt(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002096 if (max_pkt_ack > l_ptr->max_pkt) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002097 l_ptr->max_pkt = max_pkt_ack;
2098 l_ptr->max_pkt_probes = 0;
2099 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100
2101 max_pkt_ack = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002102 if (msg_probe(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 l_ptr->stats.recv_probes++;
Allan Stephensa0168922010-12-31 18:59:35 +00002104 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002105 max_pkt_ack = msg_size(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002106 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107
2108 /* Protocol message before retransmits, reduce loss risk */
2109
Per Liden4323add2006-01-18 00:38:21 +01002110 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002111
2112 if (rec_gap || (msg_probe(msg))) {
Per Liden4323add2006-01-18 00:38:21 +01002113 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2114 0, rec_gap, 0, 0, max_pkt_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 }
2116 if (msg_seq_gap(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 l_ptr->stats.recv_nacks++;
Per Liden4323add2006-01-18 00:38:21 +01002118 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2119 msg_seq_gap(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120 }
2121 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122 }
2123exit:
2124 buf_discard(buf);
2125}
2126
2127
2128/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002129 * tipc_link_tunnel(): Send one message via a link belonging to
Per Lidenb97bf3f2006-01-02 19:04:38 +01002130 * another bearer. Owner node is locked.
2131 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002132static void tipc_link_tunnel(struct link *l_ptr,
2133 struct tipc_msg *tunnel_hdr,
2134 struct tipc_msg *msg,
2135 u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136{
2137 struct link *tunnel;
2138 struct sk_buff *buf;
2139 u32 length = msg_size(msg);
2140
2141 tunnel = l_ptr->owner->active_links[selector & 1];
Allan Stephens5392d642006-06-25 23:52:50 -07002142 if (!tipc_link_is_up(tunnel)) {
2143 warn("Link changeover error, "
2144 "tunnel link no longer available\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002145 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002146 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002148 buf = tipc_buf_acquire(length + INT_H_SIZE);
Allan Stephens5392d642006-06-25 23:52:50 -07002149 if (!buf) {
2150 warn("Link changeover error, "
2151 "unable to send tunnel msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002153 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002154 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2155 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
Per Liden4323add2006-01-18 00:38:21 +01002156 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157}
2158
2159
2160
2161/*
2162 * changeover(): Send whole message queue via the remaining link
2163 * Owner node is locked.
2164 */
2165
Per Liden4323add2006-01-18 00:38:21 +01002166void tipc_link_changeover(struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167{
2168 u32 msgcount = l_ptr->out_queue_size;
2169 struct sk_buff *crs = l_ptr->first_out;
2170 struct link *tunnel = l_ptr->owner->active_links[0];
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171 struct tipc_msg tunnel_hdr;
Allan Stephens5392d642006-06-25 23:52:50 -07002172 int split_bundles;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173
2174 if (!tunnel)
2175 return;
2176
Allan Stephens5392d642006-06-25 23:52:50 -07002177 if (!l_ptr->owner->permit_changeover) {
2178 warn("Link changeover error, "
2179 "peer did not permit changeover\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002180 return;
Allan Stephens5392d642006-06-25 23:52:50 -07002181 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002182
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002183 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002184 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002185 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2186 msg_set_msgcnt(&tunnel_hdr, msgcount);
Allan Stephensf1310722006-06-25 23:51:37 -07002187
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 if (!l_ptr->first_out) {
2189 struct sk_buff *buf;
2190
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002191 buf = tipc_buf_acquire(INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192 if (buf) {
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002193 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002194 msg_set_size(&tunnel_hdr, INT_H_SIZE);
Per Liden4323add2006-01-18 00:38:21 +01002195 tipc_link_send_buf(tunnel, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002197 warn("Link changeover error, "
2198 "unable to send changeover msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002199 }
2200 return;
2201 }
Allan Stephensf1310722006-06-25 23:51:37 -07002202
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002203 split_bundles = (l_ptr->owner->active_links[0] !=
Allan Stephens5392d642006-06-25 23:52:50 -07002204 l_ptr->owner->active_links[1]);
2205
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206 while (crs) {
2207 struct tipc_msg *msg = buf_msg(crs);
2208
2209 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210 struct tipc_msg *m = msg_get_wrapped(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00002211 unchar *pos = (unchar *)m;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002212
Florian Westphald788d802007-08-02 19:28:06 -07002213 msgcount = msg_msgcnt(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214 while (msgcount--) {
Allan Stephens0e659672010-12-31 18:59:32 +00002215 msg_set_seqno(m, msg_seqno(msg));
Per Liden4323add2006-01-18 00:38:21 +01002216 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2217 msg_link_selector(m));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002218 pos += align(msg_size(m));
2219 m = (struct tipc_msg *)pos;
2220 }
2221 } else {
Per Liden4323add2006-01-18 00:38:21 +01002222 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2223 msg_link_selector(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002224 }
2225 crs = crs->next;
2226 }
2227}
2228
Per Liden4323add2006-01-18 00:38:21 +01002229void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002230{
2231 struct sk_buff *iter;
2232 struct tipc_msg tunnel_hdr;
2233
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002234 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
Allan Stephens75715212008-06-04 17:37:34 -07002235 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002236 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2237 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2238 iter = l_ptr->first_out;
2239 while (iter) {
2240 struct sk_buff *outbuf;
2241 struct tipc_msg *msg = buf_msg(iter);
2242 u32 length = msg_size(msg);
2243
2244 if (msg_user(msg) == MSG_BUNDLER)
2245 msg_set_type(msg, CLOSED_MSG);
2246 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002247 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002248 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002249 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002250 if (outbuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002251 warn("Link changeover error, "
2252 "unable to send duplicate msg\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002253 return;
2254 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002255 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2256 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2257 length);
Per Liden4323add2006-01-18 00:38:21 +01002258 tipc_link_send_buf(tunnel, outbuf);
2259 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002260 return;
2261 iter = iter->next;
2262 }
2263}
2264
2265
2266
2267/**
2268 * buf_extract - extracts embedded TIPC message from another message
2269 * @skb: encapsulating message buffer
2270 * @from_pos: offset to extract from
2271 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002272 * Returns a new message buffer containing an embedded message. The
Per Lidenb97bf3f2006-01-02 19:04:38 +01002273 * encapsulating message itself is left unchanged.
2274 */
2275
2276static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2277{
2278 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2279 u32 size = msg_size(msg);
2280 struct sk_buff *eb;
2281
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002282 eb = tipc_buf_acquire(size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002283 if (eb)
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002284 skb_copy_to_linear_data(eb, msg, size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002285 return eb;
2286}
2287
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002288/*
Per Lidenb97bf3f2006-01-02 19:04:38 +01002289 * link_recv_changeover_msg(): Receive tunneled packet sent
2290 * via other link. Node is locked. Return extracted buffer.
2291 */
2292
2293static int link_recv_changeover_msg(struct link **l_ptr,
2294 struct sk_buff **buf)
2295{
2296 struct sk_buff *tunnel_buf = *buf;
2297 struct link *dest_link;
2298 struct tipc_msg *msg;
2299 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2300 u32 msg_typ = msg_type(tunnel_msg);
2301 u32 msg_count = msg_msgcnt(tunnel_msg);
2302
2303 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
Allan Stephensb29f1422010-12-31 18:59:25 +00002304 if (!dest_link)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002305 goto exit;
Allan Stephensf1310722006-06-25 23:51:37 -07002306 if (dest_link == *l_ptr) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002307 err("Unexpected changeover message on link <%s>\n",
Allan Stephensf1310722006-06-25 23:51:37 -07002308 (*l_ptr)->name);
2309 goto exit;
2310 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311 *l_ptr = dest_link;
2312 msg = msg_get_wrapped(tunnel_msg);
2313
2314 if (msg_typ == DUPLICATE_MSG) {
Allan Stephensb29f1422010-12-31 18:59:25 +00002315 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002316 goto exit;
Allan Stephens0e659672010-12-31 18:59:32 +00002317 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002318 if (*buf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002319 warn("Link changeover error, duplicate msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002320 goto exit;
2321 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002322 buf_discard(tunnel_buf);
2323 return 1;
2324 }
2325
2326 /* First original message ?: */
2327
Per Liden4323add2006-01-18 00:38:21 +01002328 if (tipc_link_is_up(dest_link)) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002329 info("Resetting link <%s>, changeover initiated by peer\n",
2330 dest_link->name);
Per Liden4323add2006-01-18 00:38:21 +01002331 tipc_link_reset(dest_link);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002332 dest_link->exp_msg_count = msg_count;
2333 if (!msg_count)
2334 goto exit;
2335 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336 dest_link->exp_msg_count = msg_count;
2337 if (!msg_count)
2338 goto exit;
2339 }
2340
2341 /* Receive original message */
2342
2343 if (dest_link->exp_msg_count == 0) {
Allan Stephens5392d642006-06-25 23:52:50 -07002344 warn("Link switchover error, "
2345 "got too many tunnelled messages\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002346 goto exit;
2347 }
2348 dest_link->exp_msg_count--;
2349 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 goto exit;
2351 } else {
2352 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2353 if (*buf != NULL) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002354 buf_discard(tunnel_buf);
2355 return 1;
2356 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002357 warn("Link changeover error, original msg dropped\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002358 }
2359 }
2360exit:
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002361 *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 buf_discard(tunnel_buf);
2363 return 0;
2364}
2365
2366/*
2367 * Bundler functionality:
2368 */
Per Liden4323add2006-01-18 00:38:21 +01002369void tipc_link_recv_bundle(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002370{
2371 u32 msgcount = msg_msgcnt(buf_msg(buf));
2372 u32 pos = INT_H_SIZE;
2373 struct sk_buff *obuf;
2374
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 while (msgcount--) {
2376 obuf = buf_extract(buf, pos);
2377 if (obuf == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002378 warn("Link unable to unbundle message(s)\n");
2379 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002380 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381 pos += align(msg_size(buf_msg(obuf)));
Per Liden4323add2006-01-18 00:38:21 +01002382 tipc_net_route_msg(obuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002383 }
2384 buf_discard(buf);
2385}
2386
2387/*
2388 * Fragmentation/defragmentation:
2389 */
2390
2391
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002392/*
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002393 * link_send_long_buf: Entry for buffers needing fragmentation.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002394 * The buffer is complete, inclusive total message length.
Per Lidenb97bf3f2006-01-02 19:04:38 +01002395 * Returns user data length.
2396 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002397static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398{
2399 struct tipc_msg *inmsg = buf_msg(buf);
2400 struct tipc_msg fragm_hdr;
2401 u32 insize = msg_size(inmsg);
2402 u32 dsz = msg_data_sz(inmsg);
2403 unchar *crs = buf->data;
2404 u32 rest = insize;
Allan Stephens15e979d2010-05-11 14:30:10 +00002405 u32 pack_sz = l_ptr->max_pkt;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002406 u32 fragm_sz = pack_sz - INT_H_SIZE;
2407 u32 fragm_no = 1;
Allan Stephens9c396a72008-06-04 17:36:58 -07002408 u32 destaddr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409
2410 if (msg_short(inmsg))
2411 destaddr = l_ptr->addr;
Allan Stephens9c396a72008-06-04 17:36:58 -07002412 else
2413 destaddr = msg_destnode(inmsg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414
2415 if (msg_routed(inmsg))
2416 msg_set_prevnode(inmsg, tipc_own_addr);
2417
2418 /* Prepare reusable fragment header: */
2419
Allan Stephensc68ca7b2010-05-11 14:30:12 +00002420 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
Allan Stephens75715212008-06-04 17:37:34 -07002421 INT_H_SIZE, destaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002422 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2423 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2424 msg_set_fragm_no(&fragm_hdr, fragm_no);
2425 l_ptr->stats.sent_fragmented++;
2426
2427 /* Chop up message: */
2428
2429 while (rest > 0) {
2430 struct sk_buff *fragm;
2431
2432 if (rest <= fragm_sz) {
2433 fragm_sz = rest;
2434 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2435 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002436 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 if (fragm == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -07002438 warn("Link unable to fragment message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002439 dsz = -ENOMEM;
2440 goto exit;
2441 }
2442 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002443 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2444 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2445 fragm_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002446 /* Send queued messages first, if any: */
2447
2448 l_ptr->stats.sent_fragments++;
Per Liden4323add2006-01-18 00:38:21 +01002449 tipc_link_send_buf(l_ptr, fragm);
2450 if (!tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451 return dsz;
2452 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2453 rest -= fragm_sz;
2454 crs += fragm_sz;
2455 msg_set_type(&fragm_hdr, FRAGMENT);
2456 }
2457exit:
2458 buf_discard(buf);
2459 return dsz;
2460}
2461
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002462/*
2463 * A pending message being re-assembled must store certain values
2464 * to handle subsequent fragments correctly. The following functions
Per Lidenb97bf3f2006-01-02 19:04:38 +01002465 * help storing these values in unused, available fields in the
2466 * pending message. This makes dynamic memory allocation unecessary.
2467 */
2468
Sam Ravnborg05790c62006-03-20 22:37:04 -08002469static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470{
2471 msg_set_seqno(buf_msg(buf), seqno);
2472}
2473
Sam Ravnborg05790c62006-03-20 22:37:04 -08002474static u32 get_fragm_size(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002475{
2476 return msg_ack(buf_msg(buf));
2477}
2478
Sam Ravnborg05790c62006-03-20 22:37:04 -08002479static void set_fragm_size(struct sk_buff *buf, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480{
2481 msg_set_ack(buf_msg(buf), sz);
2482}
2483
Sam Ravnborg05790c62006-03-20 22:37:04 -08002484static u32 get_expected_frags(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002485{
2486 return msg_bcast_ack(buf_msg(buf));
2487}
2488
Sam Ravnborg05790c62006-03-20 22:37:04 -08002489static void set_expected_frags(struct sk_buff *buf, u32 exp)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002490{
2491 msg_set_bcast_ack(buf_msg(buf), exp);
2492}
2493
Sam Ravnborg05790c62006-03-20 22:37:04 -08002494static u32 get_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495{
2496 return msg_reroute_cnt(buf_msg(buf));
2497}
2498
Sam Ravnborg05790c62006-03-20 22:37:04 -08002499static void incr_timer_cnt(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002500{
2501 msg_incr_reroute_cnt(buf_msg(buf));
2502}
2503
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002504/*
2505 * tipc_link_recv_fragment(): Called with node lock on. Returns
Per Lidenb97bf3f2006-01-02 19:04:38 +01002506 * the reassembled buffer if message is complete.
2507 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002508int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
Per Liden4323add2006-01-18 00:38:21 +01002509 struct tipc_msg **m)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002510{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002511 struct sk_buff *prev = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002512 struct sk_buff *fbuf = *fb;
2513 struct tipc_msg *fragm = buf_msg(fbuf);
2514 struct sk_buff *pbuf = *pending;
2515 u32 long_msg_seq_no = msg_long_msgno(fragm);
2516
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002517 *fb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002518
2519 /* Is there an incomplete message waiting for this fragment? */
2520
Joe Perchesf64f9e72009-11-29 16:55:45 -08002521 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2522 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002523 prev = pbuf;
2524 pbuf = pbuf->next;
2525 }
2526
2527 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2528 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2529 u32 msg_sz = msg_size(imsg);
2530 u32 fragm_sz = msg_data_sz(fragm);
2531 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2532 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2533 if (msg_type(imsg) == TIPC_MCAST_MSG)
2534 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2535 if (msg_size(imsg) > max) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002536 buf_discard(fbuf);
2537 return 0;
2538 }
stephen hemminger31e3c3f2010-10-13 13:20:35 +00002539 pbuf = tipc_buf_acquire(msg_size(imsg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002540 if (pbuf != NULL) {
2541 pbuf->next = *pending;
2542 *pending = pbuf;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002543 skb_copy_to_linear_data(pbuf, imsg,
2544 msg_data_sz(fragm));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002545 /* Prepare buffer for subsequent fragments. */
2546
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002547 set_long_msg_seqno(pbuf, long_msg_seq_no);
Allan Stephens0e659672010-12-31 18:59:32 +00002548 set_fragm_size(pbuf, fragm_sz);
2549 set_expected_frags(pbuf, exp_fragm_cnt - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 } else {
Allan Stephensa10bd922006-06-25 23:52:17 -07002551 warn("Link unable to reassemble fragmented message\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002552 }
2553 buf_discard(fbuf);
2554 return 0;
2555 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2556 u32 dsz = msg_data_sz(fragm);
2557 u32 fsz = get_fragm_size(pbuf);
2558 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2559 u32 exp_frags = get_expected_frags(pbuf) - 1;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03002560 skb_copy_to_linear_data_offset(pbuf, crs,
2561 msg_data(fragm), dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562 buf_discard(fbuf);
2563
2564 /* Is message complete? */
2565
2566 if (exp_frags == 0) {
2567 if (prev)
2568 prev->next = pbuf->next;
2569 else
2570 *pending = pbuf->next;
2571 msg_reset_reroute_cnt(buf_msg(pbuf));
2572 *fb = pbuf;
2573 *m = buf_msg(pbuf);
2574 return 1;
2575 }
Allan Stephens0e659672010-12-31 18:59:32 +00002576 set_expected_frags(pbuf, exp_frags);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002577 return 0;
2578 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579 buf_discard(fbuf);
2580 return 0;
2581}
2582
2583/**
2584 * link_check_defragm_bufs - flush stale incoming message fragments
2585 * @l_ptr: pointer to link
2586 */
2587
2588static void link_check_defragm_bufs(struct link *l_ptr)
2589{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002590 struct sk_buff *prev = NULL;
2591 struct sk_buff *next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002592 struct sk_buff *buf = l_ptr->defragm_buf;
2593
2594 if (!buf)
2595 return;
2596 if (!link_working_working(l_ptr))
2597 return;
2598 while (buf) {
2599 u32 cnt = get_timer_cnt(buf);
2600
2601 next = buf->next;
2602 if (cnt < 4) {
2603 incr_timer_cnt(buf);
2604 prev = buf;
2605 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002606 if (prev)
2607 prev->next = buf->next;
2608 else
2609 l_ptr->defragm_buf = buf->next;
2610 buf_discard(buf);
2611 }
2612 buf = next;
2613 }
2614}
2615
2616
2617
2618static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2619{
2620 l_ptr->tolerance = tolerance;
2621 l_ptr->continuity_interval =
2622 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2623 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2624}
2625
2626
Per Liden4323add2006-01-18 00:38:21 +01002627void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002628{
2629 /* Data messages from this node, inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002630 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2631 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2632 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2633 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634 /* Transiting data messages,inclusive FIRST_FRAGM */
Allan Stephens06d82c92008-03-06 15:06:55 -08002635 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2636 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2637 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2638 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002639 l_ptr->queue_limit[CONN_MANAGER] = 1200;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002640 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2641 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2642 /* FRAGMENT and LAST_FRAGMENT packets */
2643 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2644}
2645
2646/**
2647 * link_find_link - locate link by name
2648 * @name - ptr to link name string
2649 * @node - ptr to area to be filled with ptr to associated node
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002650 *
Per Liden4323add2006-01-18 00:38:21 +01002651 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002652 * this also prevents link deletion.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002653 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002654 * Returns pointer to link (or 0 if invalid link name).
2655 */
2656
David S. Miller6c000552008-09-02 23:38:32 -07002657static struct link *link_find_link(const char *name, struct tipc_node **node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002658{
2659 struct link_name link_name_parts;
Allan Stephens2d627b92011-01-07 13:00:11 -05002660 struct tipc_bearer *b_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002661 struct link *l_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002662
2663 if (!link_name_validate(name, &link_name_parts))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002664 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002665
Per Liden4323add2006-01-18 00:38:21 +01002666 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002667 if (!b_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002668 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002669
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002670 *node = tipc_node_find(link_name_parts.addr_peer);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002671 if (!*node)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002672 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002673
2674 l_ptr = (*node)->links[b_ptr->identity];
2675 if (!l_ptr || strcmp(l_ptr->name, name))
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08002676 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002677
2678 return l_ptr;
2679}
2680
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002681struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
Per Liden4323add2006-01-18 00:38:21 +01002682 u16 cmd)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002683{
2684 struct tipc_link_config *args;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002685 u32 new_value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002686 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002687 struct tipc_node *node;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002688 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002689
2690 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
Per Liden4323add2006-01-18 00:38:21 +01002691 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002692
2693 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2694 new_value = ntohl(args->value);
2695
Per Liden4323add2006-01-18 00:38:21 +01002696 if (!strcmp(args->name, tipc_bclink_name)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002697 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
Per Liden4323add2006-01-18 00:38:21 +01002698 (tipc_bclink_set_queue_limits(new_value) == 0))
2699 return tipc_cfg_reply_none();
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002700 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
Per Liden4323add2006-01-18 00:38:21 +01002701 " (cannot change setting on broadcast link)");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002702 }
2703
Per Liden4323add2006-01-18 00:38:21 +01002704 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002705 l_ptr = link_find_link(args->name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002706 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002707 read_unlock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002708 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002709 }
2710
Per Liden4323add2006-01-18 00:38:21 +01002711 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002712 res = -EINVAL;
2713 switch (cmd) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002714 case TIPC_CMD_SET_LINK_TOL:
2715 if ((new_value >= TIPC_MIN_LINK_TOL) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716 (new_value <= TIPC_MAX_LINK_TOL)) {
2717 link_set_supervision_props(l_ptr, new_value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002718 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002719 0, 0, new_value, 0, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002720 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002721 }
2722 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002723 case TIPC_CMD_SET_LINK_PRI:
Per Liden16cb4b32006-01-13 22:22:22 +01002724 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2725 (new_value <= TIPC_MAX_LINK_PRI)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002726 l_ptr->priority = new_value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002727 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
Per Liden4323add2006-01-18 00:38:21 +01002728 0, 0, 0, new_value, 0);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002729 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002730 }
2731 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002732 case TIPC_CMD_SET_LINK_WINDOW:
2733 if ((new_value >= TIPC_MIN_LINK_WIN) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01002734 (new_value <= TIPC_MAX_LINK_WIN)) {
Per Liden4323add2006-01-18 00:38:21 +01002735 tipc_link_set_queue_limits(l_ptr, new_value);
Allan Stephens0e35fd52008-07-14 22:44:01 -07002736 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002737 }
2738 break;
2739 }
Per Liden4323add2006-01-18 00:38:21 +01002740 tipc_node_unlock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002741
Per Liden4323add2006-01-18 00:38:21 +01002742 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002743 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002744 return tipc_cfg_reply_error_string("cannot change link setting");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002745
Per Liden4323add2006-01-18 00:38:21 +01002746 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002747}
2748
2749/**
2750 * link_reset_statistics - reset link statistics
2751 * @l_ptr: pointer to link
2752 */
2753
2754static void link_reset_statistics(struct link *l_ptr)
2755{
2756 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2757 l_ptr->stats.sent_info = l_ptr->next_out_no;
2758 l_ptr->stats.recv_info = l_ptr->next_in_no;
2759}
2760
Per Liden4323add2006-01-18 00:38:21 +01002761struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002762{
2763 char *link_name;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002764 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002765 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002766
2767 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002768 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002769
2770 link_name = (char *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01002771 if (!strcmp(link_name, tipc_bclink_name)) {
2772 if (tipc_bclink_reset_stats())
2773 return tipc_cfg_reply_error_string("link not found");
2774 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002775 }
2776
Per Liden4323add2006-01-18 00:38:21 +01002777 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002778 l_ptr = link_find_link(link_name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002779 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002780 read_unlock_bh(&tipc_net_lock);
2781 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002782 }
2783
Per Liden4323add2006-01-18 00:38:21 +01002784 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002785 link_reset_statistics(l_ptr);
Per Liden4323add2006-01-18 00:38:21 +01002786 tipc_node_unlock(node);
2787 read_unlock_bh(&tipc_net_lock);
2788 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +01002789}
2790
2791/**
2792 * percent - convert count to a percentage of total (rounding up or down)
2793 */
2794
2795static u32 percent(u32 count, u32 total)
2796{
2797 return (count * 100 + (total / 2)) / total;
2798}
2799
2800/**
Per Liden4323add2006-01-18 00:38:21 +01002801 * tipc_link_stats - print link statistics
Per Lidenb97bf3f2006-01-02 19:04:38 +01002802 * @name: link name
2803 * @buf: print buffer area
2804 * @buf_size: size of print buffer area
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002805 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002806 * Returns length of print buffer data string (or 0 if error)
2807 */
2808
Per Liden4323add2006-01-18 00:38:21 +01002809static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002810{
2811 struct print_buf pb;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002812 struct link *l_ptr;
David S. Miller6c000552008-09-02 23:38:32 -07002813 struct tipc_node *node;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002814 char *status;
2815 u32 profile_total = 0;
2816
Per Liden4323add2006-01-18 00:38:21 +01002817 if (!strcmp(name, tipc_bclink_name))
2818 return tipc_bclink_stats(buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002819
Per Liden4323add2006-01-18 00:38:21 +01002820 tipc_printbuf_init(&pb, buf, buf_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002821
Per Liden4323add2006-01-18 00:38:21 +01002822 read_lock_bh(&tipc_net_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002823 l_ptr = link_find_link(name, &node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002824 if (!l_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002825 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002826 return 0;
2827 }
Per Liden4323add2006-01-18 00:38:21 +01002828 tipc_node_lock(node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002829
Per Liden4323add2006-01-18 00:38:21 +01002830 if (tipc_link_is_active(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002831 status = "ACTIVE";
Per Liden4323add2006-01-18 00:38:21 +01002832 else if (tipc_link_is_up(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002833 status = "STANDBY";
2834 else
2835 status = "DEFUNCT";
2836 tipc_printf(&pb, "Link <%s>\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002837 " %s MTU:%u Priority:%u Tolerance:%u ms"
2838 " Window:%u packets\n",
Allan Stephens15e979d2010-05-11 14:30:10 +00002839 l_ptr->name, status, l_ptr->max_pkt,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002840 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002841 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002842 l_ptr->next_in_no - l_ptr->stats.recv_info,
2843 l_ptr->stats.recv_fragments,
2844 l_ptr->stats.recv_fragmented,
2845 l_ptr->stats.recv_bundles,
2846 l_ptr->stats.recv_bundled);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002847 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002848 l_ptr->next_out_no - l_ptr->stats.sent_info,
2849 l_ptr->stats.sent_fragments,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002850 l_ptr->stats.sent_fragmented,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002851 l_ptr->stats.sent_bundles,
2852 l_ptr->stats.sent_bundled);
2853 profile_total = l_ptr->stats.msg_length_counts;
2854 if (!profile_total)
2855 profile_total = 1;
2856 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002857 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2858 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002859 l_ptr->stats.msg_length_counts,
2860 l_ptr->stats.msg_lengths_total / profile_total,
2861 percent(l_ptr->stats.msg_length_profile[0], profile_total),
2862 percent(l_ptr->stats.msg_length_profile[1], profile_total),
2863 percent(l_ptr->stats.msg_length_profile[2], profile_total),
2864 percent(l_ptr->stats.msg_length_profile[3], profile_total),
2865 percent(l_ptr->stats.msg_length_profile[4], profile_total),
2866 percent(l_ptr->stats.msg_length_profile[5], profile_total),
2867 percent(l_ptr->stats.msg_length_profile[6], profile_total));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002868 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +01002869 l_ptr->stats.recv_states,
2870 l_ptr->stats.recv_probes,
2871 l_ptr->stats.recv_nacks,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002872 l_ptr->stats.deferred_recv,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002873 l_ptr->stats.duplicates);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002874 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
2875 l_ptr->stats.sent_states,
2876 l_ptr->stats.sent_probes,
2877 l_ptr->stats.sent_nacks,
2878 l_ptr->stats.sent_acks,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002879 l_ptr->stats.retransmitted);
2880 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
2881 l_ptr->stats.bearer_congs,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002882 l_ptr->stats.link_congs,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002883 l_ptr->stats.max_queue_sz,
2884 l_ptr->stats.queue_sz_counts
2885 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
2886 : 0);
2887
Per Liden4323add2006-01-18 00:38:21 +01002888 tipc_node_unlock(node);
2889 read_unlock_bh(&tipc_net_lock);
2890 return tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002891}
2892
2893#define MAX_LINK_STATS_INFO 2000
2894
Per Liden4323add2006-01-18 00:38:21 +01002895struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002896{
2897 struct sk_buff *buf;
2898 struct tlv_desc *rep_tlv;
2899 int str_len;
2900
2901 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
Per Liden4323add2006-01-18 00:38:21 +01002902 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002903
Per Liden4323add2006-01-18 00:38:21 +01002904 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002905 if (!buf)
2906 return NULL;
2907
2908 rep_tlv = (struct tlv_desc *)buf->data;
2909
Per Liden4323add2006-01-18 00:38:21 +01002910 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
2911 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002912 if (!str_len) {
2913 buf_discard(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002914 return tipc_cfg_reply_error_string("link not found");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002915 }
2916
2917 skb_put(buf, TLV_SPACE(str_len));
2918 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2919
2920 return buf;
2921}
2922
Per Lidenb97bf3f2006-01-02 19:04:38 +01002923/**
Per Liden4323add2006-01-18 00:38:21 +01002924 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
Per Lidenb97bf3f2006-01-02 19:04:38 +01002925 * @dest: network address of destination node
2926 * @selector: used to select from set of active links
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002927 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002928 * If no active link can be found, uses default maximum packet size.
2929 */
2930
Per Liden4323add2006-01-18 00:38:21 +01002931u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002932{
David S. Miller6c000552008-09-02 23:38:32 -07002933 struct tipc_node *n_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002934 struct link *l_ptr;
2935 u32 res = MAX_PKT_DEFAULT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002936
Per Lidenb97bf3f2006-01-02 19:04:38 +01002937 if (dest == tipc_own_addr)
2938 return MAX_MSG_SIZE;
2939
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002940 read_lock_bh(&tipc_net_lock);
Allan Stephens51a8e4d2010-12-31 18:59:18 +00002941 n_ptr = tipc_node_find(dest);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002942 if (n_ptr) {
Per Liden4323add2006-01-18 00:38:21 +01002943 tipc_node_lock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002944 l_ptr = n_ptr->active_links[selector & 1];
2945 if (l_ptr)
Allan Stephens15e979d2010-05-11 14:30:10 +00002946 res = l_ptr->max_pkt;
Per Liden4323add2006-01-18 00:38:21 +01002947 tipc_node_unlock(n_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002948 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002949 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002950 return res;
2951}
2952
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002953static void link_print(struct link *l_ptr, const char *str)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002954{
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002955 char print_area[256];
2956 struct print_buf pb;
2957 struct print_buf *buf = &pb;
2958
2959 tipc_printbuf_init(buf, print_area, sizeof(print_area));
2960
Per Lidenb97bf3f2006-01-02 19:04:38 +01002961 tipc_printf(buf, str);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002962 tipc_printf(buf, "Link %x<%s>:",
Allan Stephens2d627b92011-01-07 13:00:11 -05002963 l_ptr->addr, l_ptr->b_ptr->name);
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002964
2965#ifdef CONFIG_TIPC_DEBUG
2966 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
2967 goto print_state;
2968
Per Lidenb97bf3f2006-01-02 19:04:38 +01002969 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
2970 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
2971 tipc_printf(buf, "SQUE");
2972 if (l_ptr->first_out) {
2973 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
2974 if (l_ptr->next_out)
2975 tipc_printf(buf, "%u..",
2976 msg_seqno(buf_msg(l_ptr->next_out)));
Allan Stephensb82834e2010-05-11 14:30:04 +00002977 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002978 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
2979 msg_seqno(buf_msg(l_ptr->first_out)))
Joe Perchesf64f9e72009-11-29 16:55:45 -08002980 != (l_ptr->out_queue_size - 1)) ||
2981 (l_ptr->last_out->next != NULL)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002982 tipc_printf(buf, "\nSend queue inconsistency\n");
2983 tipc_printf(buf, "first_out= %x ", l_ptr->first_out);
2984 tipc_printf(buf, "next_out= %x ", l_ptr->next_out);
2985 tipc_printf(buf, "last_out= %x ", l_ptr->last_out);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002986 }
2987 } else
2988 tipc_printf(buf, "[]");
2989 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
2990 if (l_ptr->oldest_deferred_in) {
2991 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2992 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
2993 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
2994 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
2995 tipc_printf(buf, ":RQSIZ(%u)",
2996 l_ptr->deferred_inqueue_sz);
2997 }
2998 }
Allan Stephens8d64a5b2010-12-31 18:59:27 +00002999print_state:
3000#endif
3001
Per Lidenb97bf3f2006-01-02 19:04:38 +01003002 if (link_working_unknown(l_ptr))
3003 tipc_printf(buf, ":WU");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003004 else if (link_reset_reset(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003005 tipc_printf(buf, ":RR");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003006 else if (link_reset_unknown(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003007 tipc_printf(buf, ":RU");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003008 else if (link_working_working(l_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +01003009 tipc_printf(buf, ":WW");
3010 tipc_printf(buf, "\n");
Allan Stephens8d64a5b2010-12-31 18:59:27 +00003011
3012 tipc_printbuf_validate(buf);
3013 info("%s", print_area);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003014}
3015