]>
Commit | Line | Data |
---|---|---|
b97bf3fd PL |
1 | /* |
2 | * net/tipc/link.c: TIPC link code | |
3 | * | |
593a5f22 | 4 | * Copyright (c) 1996-2006, Ericsson AB |
b97bf3fd | 5 | * Copyright (c) 2004-2005, Wind River Systems |
b97bf3fd PL |
6 | * All rights reserved. |
7 | * | |
9ea1fd3c | 8 | * Redistribution and use in source and binary forms, with or without |
b97bf3fd PL |
9 | * modification, are permitted provided that the following conditions are met: |
10 | * | |
9ea1fd3c PL |
11 | * 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. | |
b97bf3fd | 19 | * |
9ea1fd3c PL |
20 | * 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 | |
b97bf3fd PL |
34 | * POSSIBILITY OF SUCH DAMAGE. |
35 | */ | |
36 | ||
37 | #include "core.h" | |
38 | #include "dbg.h" | |
39 | #include "link.h" | |
40 | #include "net.h" | |
41 | #include "node.h" | |
42 | #include "port.h" | |
43 | #include "addr.h" | |
44 | #include "node_subscr.h" | |
45 | #include "name_distr.h" | |
46 | #include "bearer.h" | |
47 | #include "name_table.h" | |
48 | #include "discover.h" | |
49 | #include "config.h" | |
50 | #include "bcast.h" | |
51 | ||
52 | ||
53 | /* | |
54 | * Limit for deferred reception queue: | |
55 | */ | |
56 | ||
57 | #define DEF_QUEUE_LIMIT 256u | |
58 | ||
59 | /* | |
60 | * Link state events: | |
61 | */ | |
62 | ||
63 | #define STARTING_EVT 856384768 /* link processing trigger */ | |
64 | #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */ | |
65 | #define TIMEOUT_EVT 560817u /* link timer expired */ | |
66 | ||
67 | /* | |
68 | * The following two 'message types' is really just implementation | |
69 | * data conveniently stored in the message header. | |
70 | * They must not be considered part of the protocol | |
71 | */ | |
72 | #define OPEN_MSG 0 | |
73 | #define CLOSED_MSG 1 | |
74 | ||
75 | /* | |
76 | * State value stored in 'exp_msg_count' | |
77 | */ | |
78 | ||
79 | #define START_CHANGEOVER 100000u | |
80 | ||
81 | /** | |
82 | * struct link_name - deconstructed link name | |
83 | * @addr_local: network address of node at this end | |
84 | * @if_local: name of interface at this end | |
85 | * @addr_peer: network address of node at far end | |
86 | * @if_peer: name of interface at far end | |
87 | */ | |
88 | ||
89 | struct link_name { | |
90 | u32 addr_local; | |
91 | char if_local[TIPC_MAX_IF_NAME]; | |
92 | u32 addr_peer; | |
93 | char if_peer[TIPC_MAX_IF_NAME]; | |
94 | }; | |
95 | ||
96 | #if 0 | |
97 | ||
98 | /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */ | |
99 | ||
100 | /** | |
101 | * struct link_event - link up/down event notification | |
102 | */ | |
103 | ||
104 | struct link_event { | |
105 | u32 addr; | |
106 | int up; | |
107 | void (*fcn)(u32, char *, int); | |
108 | char name[TIPC_MAX_LINK_NAME]; | |
109 | }; | |
110 | ||
111 | #endif | |
112 | ||
113 | static void link_handle_out_of_seq_msg(struct link *l_ptr, | |
114 | struct sk_buff *buf); | |
115 | static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf); | |
116 | static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf); | |
117 | static void link_set_supervision_props(struct link *l_ptr, u32 tolerance); | |
118 | static int link_send_sections_long(struct port *sender, | |
119 | struct iovec const *msg_sect, | |
120 | u32 num_sect, u32 destnode); | |
121 | static void link_check_defragm_bufs(struct link *l_ptr); | |
122 | static void link_state_event(struct link *l_ptr, u32 event); | |
123 | static void link_reset_statistics(struct link *l_ptr); | |
124 | static void link_print(struct link *l_ptr, struct print_buf *buf, | |
125 | const char *str); | |
126 | ||
127 | /* | |
128 | * Debugging code used by link routines only | |
129 | * | |
130 | * When debugging link problems on a system that has multiple links, | |
131 | * the standard TIPC debugging routines may not be useful since they | |
132 | * allow the output from multiple links to be intermixed. For this reason | |
133 | * routines of the form "dbg_link_XXX()" have been created that will capture | |
134 | * debug info into a link's personal print buffer, which can then be dumped | |
135 | * into the TIPC system log (LOG) upon request. | |
136 | * | |
137 | * To enable per-link debugging, use LINK_LOG_BUF_SIZE to specify the size | |
138 | * of the print buffer used by each link. If LINK_LOG_BUF_SIZE is set to 0, | |
139 | * the dbg_link_XXX() routines simply send their output to the standard | |
140 | * debug print buffer (DBG_OUTPUT), if it has been defined; this can be useful | |
141 | * when there is only a single link in the system being debugged. | |
142 | * | |
143 | * Notes: | |
144 | * - When enabled, LINK_LOG_BUF_SIZE should be set to at least 1000 (bytes) | |
145 | * - "l_ptr" must be valid when using dbg_link_XXX() macros | |
146 | */ | |
147 | ||
148 | #define LINK_LOG_BUF_SIZE 0 | |
149 | ||
150 | #define dbg_link(fmt, arg...) do {if (LINK_LOG_BUF_SIZE) tipc_printf(&l_ptr->print_buf, fmt, ## arg); } while(0) | |
4323add6 | 151 | #define dbg_link_msg(msg, txt) do {if (LINK_LOG_BUF_SIZE) tipc_msg_print(&l_ptr->print_buf, msg, txt); } while(0) |
b97bf3fd PL |
152 | #define dbg_link_state(txt) do {if (LINK_LOG_BUF_SIZE) link_print(l_ptr, &l_ptr->print_buf, txt); } while(0) |
153 | #define dbg_link_dump() do { \ | |
154 | if (LINK_LOG_BUF_SIZE) { \ | |
155 | tipc_printf(LOG, "\n\nDumping link <%s>:\n", l_ptr->name); \ | |
4323add6 | 156 | tipc_printbuf_move(LOG, &l_ptr->print_buf); \ |
b97bf3fd PL |
157 | } \ |
158 | } while (0) | |
159 | ||
05790c64 | 160 | static void dbg_print_link(struct link *l_ptr, const char *str) |
b97bf3fd PL |
161 | { |
162 | if (DBG_OUTPUT) | |
163 | link_print(l_ptr, DBG_OUTPUT, str); | |
164 | } | |
165 | ||
05790c64 | 166 | static void dbg_print_buf_chain(struct sk_buff *root_buf) |
b97bf3fd PL |
167 | { |
168 | if (DBG_OUTPUT) { | |
169 | struct sk_buff *buf = root_buf; | |
170 | ||
171 | while (buf) { | |
172 | msg_dbg(buf_msg(buf), "In chain: "); | |
173 | buf = buf->next; | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | /* | |
05790c64 | 179 | * Simple link routines |
b97bf3fd PL |
180 | */ |
181 | ||
05790c64 | 182 | static unsigned int align(unsigned int i) |
b97bf3fd PL |
183 | { |
184 | return (i + 3) & ~3u; | |
185 | } | |
186 | ||
05790c64 | 187 | static int link_working_working(struct link *l_ptr) |
b97bf3fd PL |
188 | { |
189 | return (l_ptr->state == WORKING_WORKING); | |
190 | } | |
191 | ||
05790c64 | 192 | static int link_working_unknown(struct link *l_ptr) |
b97bf3fd PL |
193 | { |
194 | return (l_ptr->state == WORKING_UNKNOWN); | |
195 | } | |
196 | ||
05790c64 | 197 | static int link_reset_unknown(struct link *l_ptr) |
b97bf3fd PL |
198 | { |
199 | return (l_ptr->state == RESET_UNKNOWN); | |
200 | } | |
201 | ||
05790c64 | 202 | static int link_reset_reset(struct link *l_ptr) |
b97bf3fd PL |
203 | { |
204 | return (l_ptr->state == RESET_RESET); | |
205 | } | |
206 | ||
05790c64 | 207 | static int link_blocked(struct link *l_ptr) |
b97bf3fd PL |
208 | { |
209 | return (l_ptr->exp_msg_count || l_ptr->blocked); | |
210 | } | |
211 | ||
05790c64 | 212 | static int link_congested(struct link *l_ptr) |
b97bf3fd PL |
213 | { |
214 | return (l_ptr->out_queue_size >= l_ptr->queue_limit[0]); | |
215 | } | |
216 | ||
05790c64 | 217 | static u32 link_max_pkt(struct link *l_ptr) |
b97bf3fd PL |
218 | { |
219 | return l_ptr->max_pkt; | |
220 | } | |
221 | ||
05790c64 | 222 | static void link_init_max_pkt(struct link *l_ptr) |
b97bf3fd PL |
223 | { |
224 | u32 max_pkt; | |
225 | ||
226 | max_pkt = (l_ptr->b_ptr->publ.mtu & ~3); | |
227 | if (max_pkt > MAX_MSG_SIZE) | |
228 | max_pkt = MAX_MSG_SIZE; | |
229 | ||
230 | l_ptr->max_pkt_target = max_pkt; | |
231 | if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT) | |
232 | l_ptr->max_pkt = l_ptr->max_pkt_target; | |
233 | else | |
234 | l_ptr->max_pkt = MAX_PKT_DEFAULT; | |
235 | ||
236 | l_ptr->max_pkt_probes = 0; | |
237 | } | |
238 | ||
05790c64 | 239 | static u32 link_next_sent(struct link *l_ptr) |
b97bf3fd PL |
240 | { |
241 | if (l_ptr->next_out) | |
242 | return msg_seqno(buf_msg(l_ptr->next_out)); | |
243 | return mod(l_ptr->next_out_no); | |
244 | } | |
245 | ||
05790c64 | 246 | static u32 link_last_sent(struct link *l_ptr) |
b97bf3fd PL |
247 | { |
248 | return mod(link_next_sent(l_ptr) - 1); | |
249 | } | |
250 | ||
251 | /* | |
05790c64 | 252 | * Simple non-static link routines (i.e. referenced outside this file) |
b97bf3fd PL |
253 | */ |
254 | ||
4323add6 | 255 | int tipc_link_is_up(struct link *l_ptr) |
b97bf3fd PL |
256 | { |
257 | if (!l_ptr) | |
258 | return 0; | |
259 | return (link_working_working(l_ptr) || link_working_unknown(l_ptr)); | |
260 | } | |
261 | ||
4323add6 | 262 | int tipc_link_is_active(struct link *l_ptr) |
b97bf3fd PL |
263 | { |
264 | return ((l_ptr->owner->active_links[0] == l_ptr) || | |
265 | (l_ptr->owner->active_links[1] == l_ptr)); | |
266 | } | |
267 | ||
268 | /** | |
269 | * link_name_validate - validate & (optionally) deconstruct link name | |
270 | * @name - ptr to link name string | |
271 | * @name_parts - ptr to area for link name components (or NULL if not needed) | |
272 | * | |
273 | * Returns 1 if link name is valid, otherwise 0. | |
274 | */ | |
275 | ||
276 | static int link_name_validate(const char *name, struct link_name *name_parts) | |
277 | { | |
278 | char name_copy[TIPC_MAX_LINK_NAME]; | |
279 | char *addr_local; | |
280 | char *if_local; | |
281 | char *addr_peer; | |
282 | char *if_peer; | |
283 | char dummy; | |
284 | u32 z_local, c_local, n_local; | |
285 | u32 z_peer, c_peer, n_peer; | |
286 | u32 if_local_len; | |
287 | u32 if_peer_len; | |
288 | ||
289 | /* copy link name & ensure length is OK */ | |
290 | ||
291 | name_copy[TIPC_MAX_LINK_NAME - 1] = 0; | |
292 | /* need above in case non-Posix strncpy() doesn't pad with nulls */ | |
293 | strncpy(name_copy, name, TIPC_MAX_LINK_NAME); | |
294 | if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0) | |
295 | return 0; | |
296 | ||
297 | /* ensure all component parts of link name are present */ | |
298 | ||
299 | addr_local = name_copy; | |
300 | if ((if_local = strchr(addr_local, ':')) == NULL) | |
301 | return 0; | |
302 | *(if_local++) = 0; | |
303 | if ((addr_peer = strchr(if_local, '-')) == NULL) | |
304 | return 0; | |
305 | *(addr_peer++) = 0; | |
306 | if_local_len = addr_peer - if_local; | |
307 | if ((if_peer = strchr(addr_peer, ':')) == NULL) | |
308 | return 0; | |
309 | *(if_peer++) = 0; | |
310 | if_peer_len = strlen(if_peer) + 1; | |
311 | ||
312 | /* validate component parts of link name */ | |
313 | ||
314 | if ((sscanf(addr_local, "%u.%u.%u%c", | |
315 | &z_local, &c_local, &n_local, &dummy) != 3) || | |
316 | (sscanf(addr_peer, "%u.%u.%u%c", | |
317 | &z_peer, &c_peer, &n_peer, &dummy) != 3) || | |
318 | (z_local > 255) || (c_local > 4095) || (n_local > 4095) || | |
319 | (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) || | |
320 | (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) || | |
321 | (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) || | |
322 | (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) || | |
323 | (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1))) | |
324 | return 0; | |
325 | ||
326 | /* return link name components, if necessary */ | |
327 | ||
328 | if (name_parts) { | |
329 | name_parts->addr_local = tipc_addr(z_local, c_local, n_local); | |
330 | strcpy(name_parts->if_local, if_local); | |
331 | name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer); | |
332 | strcpy(name_parts->if_peer, if_peer); | |
333 | } | |
334 | return 1; | |
335 | } | |
336 | ||
337 | /** | |
338 | * link_timeout - handle expiration of link timer | |
339 | * @l_ptr: pointer to link | |
340 | * | |
4323add6 PL |
341 | * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict |
342 | * with tipc_link_delete(). (There is no risk that the node will be deleted by | |
343 | * another thread because tipc_link_delete() always cancels the link timer before | |
344 | * tipc_node_delete() is called.) | |
b97bf3fd PL |
345 | */ |
346 | ||
347 | static void link_timeout(struct link *l_ptr) | |
348 | { | |
4323add6 | 349 | tipc_node_lock(l_ptr->owner); |
b97bf3fd PL |
350 | |
351 | /* update counters used in statistical profiling of send traffic */ | |
352 | ||
353 | l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size; | |
354 | l_ptr->stats.queue_sz_counts++; | |
355 | ||
356 | if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz) | |
357 | l_ptr->stats.max_queue_sz = l_ptr->out_queue_size; | |
358 | ||
359 | if (l_ptr->first_out) { | |
360 | struct tipc_msg *msg = buf_msg(l_ptr->first_out); | |
361 | u32 length = msg_size(msg); | |
362 | ||
363 | if ((msg_user(msg) == MSG_FRAGMENTER) | |
364 | && (msg_type(msg) == FIRST_FRAGMENT)) { | |
365 | length = msg_size(msg_get_wrapped(msg)); | |
366 | } | |
367 | if (length) { | |
368 | l_ptr->stats.msg_lengths_total += length; | |
369 | l_ptr->stats.msg_length_counts++; | |
370 | if (length <= 64) | |
371 | l_ptr->stats.msg_length_profile[0]++; | |
372 | else if (length <= 256) | |
373 | l_ptr->stats.msg_length_profile[1]++; | |
374 | else if (length <= 1024) | |
375 | l_ptr->stats.msg_length_profile[2]++; | |
376 | else if (length <= 4096) | |
377 | l_ptr->stats.msg_length_profile[3]++; | |
378 | else if (length <= 16384) | |
379 | l_ptr->stats.msg_length_profile[4]++; | |
380 | else if (length <= 32768) | |
381 | l_ptr->stats.msg_length_profile[5]++; | |
382 | else | |
383 | l_ptr->stats.msg_length_profile[6]++; | |
384 | } | |
385 | } | |
386 | ||
387 | /* do all other link processing performed on a periodic basis */ | |
388 | ||
389 | link_check_defragm_bufs(l_ptr); | |
390 | ||
391 | link_state_event(l_ptr, TIMEOUT_EVT); | |
392 | ||
393 | if (l_ptr->next_out) | |
4323add6 | 394 | tipc_link_push_queue(l_ptr); |
b97bf3fd | 395 | |
4323add6 | 396 | tipc_node_unlock(l_ptr->owner); |
b97bf3fd PL |
397 | } |
398 | ||
05790c64 | 399 | static void link_set_timer(struct link *l_ptr, u32 time) |
b97bf3fd PL |
400 | { |
401 | k_start_timer(&l_ptr->timer, time); | |
402 | } | |
403 | ||
404 | /** | |
4323add6 | 405 | * tipc_link_create - create a new link |
b97bf3fd PL |
406 | * @b_ptr: pointer to associated bearer |
407 | * @peer: network address of node at other end of link | |
408 | * @media_addr: media address to use when sending messages over link | |
409 | * | |
410 | * Returns pointer to link. | |
411 | */ | |
412 | ||
4323add6 PL |
413 | struct link *tipc_link_create(struct bearer *b_ptr, const u32 peer, |
414 | const struct tipc_media_addr *media_addr) | |
b97bf3fd PL |
415 | { |
416 | struct link *l_ptr; | |
417 | struct tipc_msg *msg; | |
418 | char *if_name; | |
419 | ||
420 | l_ptr = (struct link *)kmalloc(sizeof(*l_ptr), GFP_ATOMIC); | |
421 | if (!l_ptr) { | |
422 | warn("Memory squeeze; Failed to create link\n"); | |
423 | return NULL; | |
424 | } | |
425 | memset(l_ptr, 0, sizeof(*l_ptr)); | |
426 | ||
427 | l_ptr->addr = peer; | |
428 | if_name = strchr(b_ptr->publ.name, ':') + 1; | |
429 | sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:", | |
430 | tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr), | |
431 | tipc_node(tipc_own_addr), | |
432 | if_name, | |
433 | tipc_zone(peer), tipc_cluster(peer), tipc_node(peer)); | |
434 | /* note: peer i/f is appended to link name by reset/activate */ | |
435 | memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr)); | |
436 | k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr); | |
437 | list_add_tail(&l_ptr->link_list, &b_ptr->links); | |
438 | l_ptr->checkpoint = 1; | |
439 | l_ptr->b_ptr = b_ptr; | |
440 | link_set_supervision_props(l_ptr, b_ptr->media->tolerance); | |
441 | l_ptr->state = RESET_UNKNOWN; | |
442 | ||
443 | l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg; | |
444 | msg = l_ptr->pmsg; | |
445 | msg_init(msg, LINK_PROTOCOL, RESET_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr); | |
446 | msg_set_size(msg, sizeof(l_ptr->proto_msg)); | |
447 | msg_set_session(msg, tipc_random); | |
448 | msg_set_bearer_id(msg, b_ptr->identity); | |
449 | strcpy((char *)msg_data(msg), if_name); | |
450 | ||
451 | l_ptr->priority = b_ptr->priority; | |
4323add6 | 452 | tipc_link_set_queue_limits(l_ptr, b_ptr->media->window); |
b97bf3fd PL |
453 | |
454 | link_init_max_pkt(l_ptr); | |
455 | ||
456 | l_ptr->next_out_no = 1; | |
457 | INIT_LIST_HEAD(&l_ptr->waiting_ports); | |
458 | ||
459 | link_reset_statistics(l_ptr); | |
460 | ||
4323add6 | 461 | l_ptr->owner = tipc_node_attach_link(l_ptr); |
b97bf3fd PL |
462 | if (!l_ptr->owner) { |
463 | kfree(l_ptr); | |
464 | return NULL; | |
465 | } | |
466 | ||
467 | if (LINK_LOG_BUF_SIZE) { | |
468 | char *pb = kmalloc(LINK_LOG_BUF_SIZE, GFP_ATOMIC); | |
469 | ||
470 | if (!pb) { | |
471 | kfree(l_ptr); | |
472 | warn("Memory squeeze; Failed to create link\n"); | |
473 | return NULL; | |
474 | } | |
4323add6 | 475 | tipc_printbuf_init(&l_ptr->print_buf, pb, LINK_LOG_BUF_SIZE); |
b97bf3fd PL |
476 | } |
477 | ||
4323add6 | 478 | tipc_k_signal((Handler)tipc_link_start, (unsigned long)l_ptr); |
b97bf3fd | 479 | |
4323add6 | 480 | dbg("tipc_link_create(): tolerance = %u,cont intv = %u, abort_limit = %u\n", |
b97bf3fd PL |
481 | l_ptr->tolerance, l_ptr->continuity_interval, l_ptr->abort_limit); |
482 | ||
483 | return l_ptr; | |
484 | } | |
485 | ||
486 | /** | |
4323add6 | 487 | * tipc_link_delete - delete a link |
b97bf3fd PL |
488 | * @l_ptr: pointer to link |
489 | * | |
4323add6 | 490 | * Note: 'tipc_net_lock' is write_locked, bearer is locked. |
b97bf3fd PL |
491 | * This routine must not grab the node lock until after link timer cancellation |
492 | * to avoid a potential deadlock situation. | |
493 | */ | |
494 | ||
4323add6 | 495 | void tipc_link_delete(struct link *l_ptr) |
b97bf3fd PL |
496 | { |
497 | if (!l_ptr) { | |
498 | err("Attempt to delete non-existent link\n"); | |
499 | return; | |
500 | } | |
501 | ||
4323add6 | 502 | dbg("tipc_link_delete()\n"); |
b97bf3fd PL |
503 | |
504 | k_cancel_timer(&l_ptr->timer); | |
505 | ||
4323add6 PL |
506 | tipc_node_lock(l_ptr->owner); |
507 | tipc_link_reset(l_ptr); | |
508 | tipc_node_detach_link(l_ptr->owner, l_ptr); | |
509 | tipc_link_stop(l_ptr); | |
b97bf3fd PL |
510 | list_del_init(&l_ptr->link_list); |
511 | if (LINK_LOG_BUF_SIZE) | |
512 | kfree(l_ptr->print_buf.buf); | |
4323add6 | 513 | tipc_node_unlock(l_ptr->owner); |
b97bf3fd PL |
514 | k_term_timer(&l_ptr->timer); |
515 | kfree(l_ptr); | |
516 | } | |
517 | ||
4323add6 | 518 | void tipc_link_start(struct link *l_ptr) |
b97bf3fd | 519 | { |
4323add6 | 520 | dbg("tipc_link_start %x\n", l_ptr); |
b97bf3fd PL |
521 | link_state_event(l_ptr, STARTING_EVT); |
522 | } | |
523 | ||
524 | /** | |
525 | * link_schedule_port - schedule port for deferred sending | |
526 | * @l_ptr: pointer to link | |
527 | * @origport: reference to sending port | |
528 | * @sz: amount of data to be sent | |
529 | * | |
530 | * Schedules port for renewed sending of messages after link congestion | |
531 | * has abated. | |
532 | */ | |
533 | ||
534 | static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz) | |
535 | { | |
536 | struct port *p_ptr; | |
537 | ||
4323add6 PL |
538 | spin_lock_bh(&tipc_port_list_lock); |
539 | p_ptr = tipc_port_lock(origport); | |
b97bf3fd PL |
540 | if (p_ptr) { |
541 | if (!p_ptr->wakeup) | |
542 | goto exit; | |
543 | if (!list_empty(&p_ptr->wait_list)) | |
544 | goto exit; | |
545 | p_ptr->congested_link = l_ptr; | |
546 | p_ptr->publ.congested = 1; | |
547 | p_ptr->waiting_pkts = 1 + ((sz - 1) / link_max_pkt(l_ptr)); | |
548 | list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports); | |
549 | l_ptr->stats.link_congs++; | |
550 | exit: | |
4323add6 | 551 | tipc_port_unlock(p_ptr); |
b97bf3fd | 552 | } |
4323add6 | 553 | spin_unlock_bh(&tipc_port_list_lock); |
b97bf3fd PL |
554 | return -ELINKCONG; |
555 | } | |
556 | ||
4323add6 | 557 | void tipc_link_wakeup_ports(struct link *l_ptr, int all) |
b97bf3fd PL |
558 | { |
559 | struct port *p_ptr; | |
560 | struct port *temp_p_ptr; | |
561 | int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size; | |
562 | ||
563 | if (all) | |
564 | win = 100000; | |
565 | if (win <= 0) | |
566 | return; | |
4323add6 | 567 | if (!spin_trylock_bh(&tipc_port_list_lock)) |
b97bf3fd PL |
568 | return; |
569 | if (link_congested(l_ptr)) | |
570 | goto exit; | |
571 | list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports, | |
572 | wait_list) { | |
573 | if (win <= 0) | |
574 | break; | |
575 | list_del_init(&p_ptr->wait_list); | |
1fc54d8f | 576 | p_ptr->congested_link = NULL; |
b97bf3fd PL |
577 | assert(p_ptr->wakeup); |
578 | spin_lock_bh(p_ptr->publ.lock); | |
579 | p_ptr->publ.congested = 0; | |
580 | p_ptr->wakeup(&p_ptr->publ); | |
581 | win -= p_ptr->waiting_pkts; | |
582 | spin_unlock_bh(p_ptr->publ.lock); | |
583 | } | |
584 | ||
585 | exit: | |
4323add6 | 586 | spin_unlock_bh(&tipc_port_list_lock); |
b97bf3fd PL |
587 | } |
588 | ||
589 | /** | |
590 | * link_release_outqueue - purge link's outbound message queue | |
591 | * @l_ptr: pointer to link | |
592 | */ | |
593 | ||
594 | static void link_release_outqueue(struct link *l_ptr) | |
595 | { | |
596 | struct sk_buff *buf = l_ptr->first_out; | |
597 | struct sk_buff *next; | |
598 | ||
599 | while (buf) { | |
600 | next = buf->next; | |
601 | buf_discard(buf); | |
602 | buf = next; | |
603 | } | |
604 | l_ptr->first_out = NULL; | |
605 | l_ptr->out_queue_size = 0; | |
606 | } | |
607 | ||
608 | /** | |
4323add6 | 609 | * tipc_link_reset_fragments - purge link's inbound message fragments queue |
b97bf3fd PL |
610 | * @l_ptr: pointer to link |
611 | */ | |
612 | ||
4323add6 | 613 | void tipc_link_reset_fragments(struct link *l_ptr) |
b97bf3fd PL |
614 | { |
615 | struct sk_buff *buf = l_ptr->defragm_buf; | |
616 | struct sk_buff *next; | |
617 | ||
618 | while (buf) { | |
619 | next = buf->next; | |
620 | buf_discard(buf); | |
621 | buf = next; | |
622 | } | |
623 | l_ptr->defragm_buf = NULL; | |
624 | } | |
625 | ||
626 | /** | |
4323add6 | 627 | * tipc_link_stop - purge all inbound and outbound messages associated with link |
b97bf3fd PL |
628 | * @l_ptr: pointer to link |
629 | */ | |
630 | ||
4323add6 | 631 | void tipc_link_stop(struct link *l_ptr) |
b97bf3fd PL |
632 | { |
633 | struct sk_buff *buf; | |
634 | struct sk_buff *next; | |
635 | ||
636 | buf = l_ptr->oldest_deferred_in; | |
637 | while (buf) { | |
638 | next = buf->next; | |
639 | buf_discard(buf); | |
640 | buf = next; | |
641 | } | |
642 | ||
643 | buf = l_ptr->first_out; | |
644 | while (buf) { | |
645 | next = buf->next; | |
646 | buf_discard(buf); | |
647 | buf = next; | |
648 | } | |
649 | ||
4323add6 | 650 | tipc_link_reset_fragments(l_ptr); |
b97bf3fd PL |
651 | |
652 | buf_discard(l_ptr->proto_msg_queue); | |
653 | l_ptr->proto_msg_queue = NULL; | |
654 | } | |
655 | ||
656 | #if 0 | |
657 | ||
658 | /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */ | |
659 | ||
660 | static void link_recv_event(struct link_event *ev) | |
661 | { | |
662 | ev->fcn(ev->addr, ev->name, ev->up); | |
663 | kfree(ev); | |
664 | } | |
665 | ||
666 | static void link_send_event(void (*fcn)(u32 a, char *n, int up), | |
667 | struct link *l_ptr, int up) | |
668 | { | |
669 | struct link_event *ev; | |
670 | ||
671 | ev = kmalloc(sizeof(*ev), GFP_ATOMIC); | |
672 | if (!ev) { | |
673 | warn("Link event allocation failure\n"); | |
674 | return; | |
675 | } | |
676 | ev->addr = l_ptr->addr; | |
677 | ev->up = up; | |
678 | ev->fcn = fcn; | |
679 | memcpy(ev->name, l_ptr->name, TIPC_MAX_LINK_NAME); | |
4323add6 | 680 | tipc_k_signal((Handler)link_recv_event, (unsigned long)ev); |
b97bf3fd PL |
681 | } |
682 | ||
683 | #else | |
684 | ||
685 | #define link_send_event(fcn, l_ptr, up) do { } while (0) | |
686 | ||
687 | #endif | |
688 | ||
4323add6 | 689 | void tipc_link_reset(struct link *l_ptr) |
b97bf3fd PL |
690 | { |
691 | struct sk_buff *buf; | |
692 | u32 prev_state = l_ptr->state; | |
693 | u32 checkpoint = l_ptr->next_in_no; | |
694 | ||
695 | msg_set_session(l_ptr->pmsg, msg_session(l_ptr->pmsg) + 1); | |
696 | ||
697 | /* Link is down, accept any session: */ | |
698 | l_ptr->peer_session = 0; | |
699 | ||
700 | /* Prepare for max packet size negotiation */ | |
701 | link_init_max_pkt(l_ptr); | |
702 | ||
703 | l_ptr->state = RESET_UNKNOWN; | |
704 | dbg_link_state("Resetting Link\n"); | |
705 | ||
706 | if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET)) | |
707 | return; | |
708 | ||
4323add6 PL |
709 | tipc_node_link_down(l_ptr->owner, l_ptr); |
710 | tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr); | |
b97bf3fd | 711 | #if 0 |
4323add6 | 712 | tipc_printf(TIPC_CONS, "\nReset link <%s>\n", l_ptr->name); |
b97bf3fd PL |
713 | dbg_link_dump(); |
714 | #endif | |
4323add6 | 715 | if (tipc_node_has_active_links(l_ptr->owner) && |
b97bf3fd PL |
716 | l_ptr->owner->permit_changeover) { |
717 | l_ptr->reset_checkpoint = checkpoint; | |
718 | l_ptr->exp_msg_count = START_CHANGEOVER; | |
719 | } | |
720 | ||
721 | /* Clean up all queues: */ | |
722 | ||
723 | link_release_outqueue(l_ptr); | |
724 | buf_discard(l_ptr->proto_msg_queue); | |
725 | l_ptr->proto_msg_queue = NULL; | |
726 | buf = l_ptr->oldest_deferred_in; | |
727 | while (buf) { | |
728 | struct sk_buff *next = buf->next; | |
729 | buf_discard(buf); | |
730 | buf = next; | |
731 | } | |
732 | if (!list_empty(&l_ptr->waiting_ports)) | |
4323add6 | 733 | tipc_link_wakeup_ports(l_ptr, 1); |
b97bf3fd PL |
734 | |
735 | l_ptr->retransm_queue_head = 0; | |
736 | l_ptr->retransm_queue_size = 0; | |
737 | l_ptr->last_out = NULL; | |
738 | l_ptr->first_out = NULL; | |
739 | l_ptr->next_out = NULL; | |
740 | l_ptr->unacked_window = 0; | |
741 | l_ptr->checkpoint = 1; | |
742 | l_ptr->next_out_no = 1; | |
743 | l_ptr->deferred_inqueue_sz = 0; | |
744 | l_ptr->oldest_deferred_in = NULL; | |
745 | l_ptr->newest_deferred_in = NULL; | |
746 | l_ptr->fsm_msg_cnt = 0; | |
747 | l_ptr->stale_count = 0; | |
748 | link_reset_statistics(l_ptr); | |
749 | ||
4323add6 | 750 | link_send_event(tipc_cfg_link_event, l_ptr, 0); |
b97bf3fd | 751 | if (!in_own_cluster(l_ptr->addr)) |
4323add6 | 752 | link_send_event(tipc_disc_link_event, l_ptr, 0); |
b97bf3fd PL |
753 | } |
754 | ||
755 | ||
756 | static void link_activate(struct link *l_ptr) | |
757 | { | |
758 | l_ptr->next_in_no = 1; | |
4323add6 PL |
759 | tipc_node_link_up(l_ptr->owner, l_ptr); |
760 | tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr); | |
761 | link_send_event(tipc_cfg_link_event, l_ptr, 1); | |
b97bf3fd | 762 | if (!in_own_cluster(l_ptr->addr)) |
4323add6 | 763 | link_send_event(tipc_disc_link_event, l_ptr, 1); |
b97bf3fd PL |
764 | } |
765 | ||
766 | /** | |
767 | * link_state_event - link finite state machine | |
768 | * @l_ptr: pointer to link | |
769 | * @event: state machine event to process | |
770 | */ | |
771 | ||
772 | static void link_state_event(struct link *l_ptr, unsigned event) | |
773 | { | |
774 | struct link *other; | |
775 | u32 cont_intv = l_ptr->continuity_interval; | |
776 | ||
777 | if (!l_ptr->started && (event != STARTING_EVT)) | |
778 | return; /* Not yet. */ | |
779 | ||
780 | if (link_blocked(l_ptr)) { | |
781 | if (event == TIMEOUT_EVT) { | |
782 | link_set_timer(l_ptr, cont_intv); | |
783 | } | |
784 | return; /* Changeover going on */ | |
785 | } | |
786 | dbg_link("STATE_EV: <%s> ", l_ptr->name); | |
787 | ||
788 | switch (l_ptr->state) { | |
789 | case WORKING_WORKING: | |
790 | dbg_link("WW/"); | |
791 | switch (event) { | |
792 | case TRAFFIC_MSG_EVT: | |
793 | dbg_link("TRF-"); | |
794 | /* fall through */ | |
795 | case ACTIVATE_MSG: | |
796 | dbg_link("ACT\n"); | |
797 | break; | |
798 | case TIMEOUT_EVT: | |
799 | dbg_link("TIM "); | |
800 | if (l_ptr->next_in_no != l_ptr->checkpoint) { | |
801 | l_ptr->checkpoint = l_ptr->next_in_no; | |
4323add6 PL |
802 | if (tipc_bclink_acks_missing(l_ptr->owner)) { |
803 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, | |
804 | 0, 0, 0, 0, 0); | |
b97bf3fd PL |
805 | l_ptr->fsm_msg_cnt++; |
806 | } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) { | |
4323add6 PL |
807 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, |
808 | 1, 0, 0, 0, 0); | |
b97bf3fd PL |
809 | l_ptr->fsm_msg_cnt++; |
810 | } | |
811 | link_set_timer(l_ptr, cont_intv); | |
812 | break; | |
813 | } | |
814 | dbg_link(" -> WU\n"); | |
815 | l_ptr->state = WORKING_UNKNOWN; | |
816 | l_ptr->fsm_msg_cnt = 0; | |
4323add6 | 817 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0); |
b97bf3fd PL |
818 | l_ptr->fsm_msg_cnt++; |
819 | link_set_timer(l_ptr, cont_intv / 4); | |
820 | break; | |
821 | case RESET_MSG: | |
822 | dbg_link("RES -> RR\n"); | |
4323add6 | 823 | tipc_link_reset(l_ptr); |
b97bf3fd PL |
824 | l_ptr->state = RESET_RESET; |
825 | l_ptr->fsm_msg_cnt = 0; | |
4323add6 | 826 | tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0); |
b97bf3fd PL |
827 | l_ptr->fsm_msg_cnt++; |
828 | link_set_timer(l_ptr, cont_intv); | |
829 | break; | |
830 | default: | |
831 | err("Unknown link event %u in WW state\n", event); | |
832 | } | |
833 | break; | |
834 | case WORKING_UNKNOWN: | |
835 | dbg_link("WU/"); | |
836 | switch (event) { | |
837 | case TRAFFIC_MSG_EVT: | |
838 | dbg_link("TRF-"); | |
839 | case ACTIVATE_MSG: | |
840 | dbg_link("ACT -> WW\n"); | |
841 | l_ptr->state = WORKING_WORKING; | |
842 | l_ptr->fsm_msg_cnt = 0; | |
843 | link_set_timer(l_ptr, cont_intv); | |
844 | break; | |
845 | case RESET_MSG: | |
846 | dbg_link("RES -> RR\n"); | |
4323add6 | 847 | tipc_link_reset(l_ptr); |
b97bf3fd PL |
848 | l_ptr->state = RESET_RESET; |
849 | l_ptr->fsm_msg_cnt = 0; | |
4323add6 | 850 | tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0); |
b97bf3fd PL |
851 | l_ptr->fsm_msg_cnt++; |
852 | link_set_timer(l_ptr, cont_intv); | |
853 | break; | |
854 | case TIMEOUT_EVT: | |
855 | dbg_link("TIM "); | |
856 | if (l_ptr->next_in_no != l_ptr->checkpoint) { | |
857 | dbg_link("-> WW \n"); | |
858 | l_ptr->state = WORKING_WORKING; | |
859 | l_ptr->fsm_msg_cnt = 0; | |
860 | l_ptr->checkpoint = l_ptr->next_in_no; | |
4323add6 PL |
861 | if (tipc_bclink_acks_missing(l_ptr->owner)) { |
862 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, | |
863 | 0, 0, 0, 0, 0); | |
b97bf3fd PL |
864 | l_ptr->fsm_msg_cnt++; |
865 | } | |
866 | link_set_timer(l_ptr, cont_intv); | |
867 | } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) { | |
868 | dbg_link("Probing %u/%u,timer = %u ms)\n", | |
869 | l_ptr->fsm_msg_cnt, l_ptr->abort_limit, | |
870 | cont_intv / 4); | |
4323add6 PL |
871 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, |
872 | 1, 0, 0, 0, 0); | |
b97bf3fd PL |
873 | l_ptr->fsm_msg_cnt++; |
874 | link_set_timer(l_ptr, cont_intv / 4); | |
875 | } else { /* Link has failed */ | |
876 | dbg_link("-> RU (%u probes unanswered)\n", | |
877 | l_ptr->fsm_msg_cnt); | |
4323add6 | 878 | tipc_link_reset(l_ptr); |
b97bf3fd PL |
879 | l_ptr->state = RESET_UNKNOWN; |
880 | l_ptr->fsm_msg_cnt = 0; | |
4323add6 PL |
881 | tipc_link_send_proto_msg(l_ptr, RESET_MSG, |
882 | 0, 0, 0, 0, 0); | |
b97bf3fd PL |
883 | l_ptr->fsm_msg_cnt++; |
884 | link_set_timer(l_ptr, cont_intv); | |
885 | } | |
886 | break; | |
887 | default: | |
888 | err("Unknown link event %u in WU state\n", event); | |
889 | } | |
890 | break; | |
891 | case RESET_UNKNOWN: | |
892 | dbg_link("RU/"); | |
893 | switch (event) { | |
894 | case TRAFFIC_MSG_EVT: | |
895 | dbg_link("TRF-\n"); | |
896 | break; | |
897 | case ACTIVATE_MSG: | |
898 | other = l_ptr->owner->active_links[0]; | |
899 | if (other && link_working_unknown(other)) { | |
900 | dbg_link("ACT\n"); | |
901 | break; | |
902 | } | |
903 | dbg_link("ACT -> WW\n"); | |
904 | l_ptr->state = WORKING_WORKING; | |
905 | l_ptr->fsm_msg_cnt = 0; | |
906 | link_activate(l_ptr); | |
4323add6 | 907 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0); |
b97bf3fd PL |
908 | l_ptr->fsm_msg_cnt++; |
909 | link_set_timer(l_ptr, cont_intv); | |
910 | break; | |
911 | case RESET_MSG: | |
912 | dbg_link("RES \n"); | |
913 | dbg_link(" -> RR\n"); | |
914 | l_ptr->state = RESET_RESET; | |
915 | l_ptr->fsm_msg_cnt = 0; | |
4323add6 | 916 | tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0); |
b97bf3fd PL |
917 | l_ptr->fsm_msg_cnt++; |
918 | link_set_timer(l_ptr, cont_intv); | |
919 | break; | |
920 | case STARTING_EVT: | |
921 | dbg_link("START-"); | |
922 | l_ptr->started = 1; | |
923 | /* fall through */ | |
924 | case TIMEOUT_EVT: | |
925 | dbg_link("TIM \n"); | |
4323add6 | 926 | tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0); |
b97bf3fd PL |
927 | l_ptr->fsm_msg_cnt++; |
928 | link_set_timer(l_ptr, cont_intv); | |
929 | break; | |
930 | default: | |
931 | err("Unknown link event %u in RU state\n", event); | |
932 | } | |
933 | break; | |
934 | case RESET_RESET: | |
935 | dbg_link("RR/ "); | |
936 | switch (event) { | |
937 | case TRAFFIC_MSG_EVT: | |
938 | dbg_link("TRF-"); | |
939 | /* fall through */ | |
940 | case ACTIVATE_MSG: | |
941 | other = l_ptr->owner->active_links[0]; | |
942 | if (other && link_working_unknown(other)) { | |
943 | dbg_link("ACT\n"); | |
944 | break; | |
945 | } | |
946 | dbg_link("ACT -> WW\n"); | |
947 | l_ptr->state = WORKING_WORKING; | |
948 | l_ptr->fsm_msg_cnt = 0; | |
949 | link_activate(l_ptr); | |
4323add6 | 950 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0); |
b97bf3fd PL |
951 | l_ptr->fsm_msg_cnt++; |
952 | link_set_timer(l_ptr, cont_intv); | |
953 | break; | |
954 | case RESET_MSG: | |
955 | dbg_link("RES\n"); | |
956 | break; | |
957 | case TIMEOUT_EVT: | |
958 | dbg_link("TIM\n"); | |
4323add6 | 959 | tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0); |
b97bf3fd PL |
960 | l_ptr->fsm_msg_cnt++; |
961 | link_set_timer(l_ptr, cont_intv); | |
962 | dbg_link("fsm_msg_cnt %u\n", l_ptr->fsm_msg_cnt); | |
963 | break; | |
964 | default: | |
965 | err("Unknown link event %u in RR state\n", event); | |
966 | } | |
967 | break; | |
968 | default: | |
969 | err("Unknown link state %u/%u\n", l_ptr->state, event); | |
970 | } | |
971 | } | |
972 | ||
973 | /* | |
974 | * link_bundle_buf(): Append contents of a buffer to | |
975 | * the tail of an existing one. | |
976 | */ | |
977 | ||
978 | static int link_bundle_buf(struct link *l_ptr, | |
979 | struct sk_buff *bundler, | |
980 | struct sk_buff *buf) | |
981 | { | |
982 | struct tipc_msg *bundler_msg = buf_msg(bundler); | |
983 | struct tipc_msg *msg = buf_msg(buf); | |
984 | u32 size = msg_size(msg); | |
985 | u32 to_pos = align(msg_size(bundler_msg)); | |
986 | u32 rest = link_max_pkt(l_ptr) - to_pos; | |
987 | ||
988 | if (msg_user(bundler_msg) != MSG_BUNDLER) | |
989 | return 0; | |
990 | if (msg_type(bundler_msg) != OPEN_MSG) | |
991 | return 0; | |
992 | if (rest < align(size)) | |
993 | return 0; | |
994 | ||
995 | skb_put(bundler, (to_pos - msg_size(bundler_msg)) + size); | |
996 | memcpy(bundler->data + to_pos, buf->data, size); | |
997 | msg_set_size(bundler_msg, to_pos + size); | |
998 | msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1); | |
999 | dbg("Packed msg # %u(%u octets) into pos %u in buf(#%u)\n", | |
1000 | msg_msgcnt(bundler_msg), size, to_pos, msg_seqno(bundler_msg)); | |
1001 | msg_dbg(msg, "PACKD:"); | |
1002 | buf_discard(buf); | |
1003 | l_ptr->stats.sent_bundled++; | |
1004 | return 1; | |
1005 | } | |
1006 | ||
05790c64 SR |
1007 | static void link_add_to_outqueue(struct link *l_ptr, |
1008 | struct sk_buff *buf, | |
1009 | struct tipc_msg *msg) | |
b97bf3fd PL |
1010 | { |
1011 | u32 ack = mod(l_ptr->next_in_no - 1); | |
1012 | u32 seqno = mod(l_ptr->next_out_no++); | |
1013 | ||
1014 | msg_set_word(msg, 2, ((ack << 16) | seqno)); | |
1015 | msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); | |
1016 | buf->next = NULL; | |
1017 | if (l_ptr->first_out) { | |
1018 | l_ptr->last_out->next = buf; | |
1019 | l_ptr->last_out = buf; | |
1020 | } else | |
1021 | l_ptr->first_out = l_ptr->last_out = buf; | |
1022 | l_ptr->out_queue_size++; | |
1023 | } | |
1024 | ||
1025 | /* | |
4323add6 | 1026 | * tipc_link_send_buf() is the 'full path' for messages, called from |
b97bf3fd PL |
1027 | * inside TIPC when the 'fast path' in tipc_send_buf |
1028 | * has failed, and from link_send() | |
1029 | */ | |
1030 | ||
4323add6 | 1031 | int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf) |
b97bf3fd PL |
1032 | { |
1033 | struct tipc_msg *msg = buf_msg(buf); | |
1034 | u32 size = msg_size(msg); | |
1035 | u32 dsz = msg_data_sz(msg); | |
1036 | u32 queue_size = l_ptr->out_queue_size; | |
1037 | u32 imp = msg_tot_importance(msg); | |
1038 | u32 queue_limit = l_ptr->queue_limit[imp]; | |
1039 | u32 max_packet = link_max_pkt(l_ptr); | |
1040 | ||
1041 | msg_set_prevnode(msg, tipc_own_addr); /* If routed message */ | |
1042 | ||
1043 | /* Match msg importance against queue limits: */ | |
1044 | ||
1045 | if (unlikely(queue_size >= queue_limit)) { | |
1046 | if (imp <= TIPC_CRITICAL_IMPORTANCE) { | |
1047 | return link_schedule_port(l_ptr, msg_origport(msg), | |
1048 | size); | |
1049 | } | |
1050 | msg_dbg(msg, "TIPC: Congestion, throwing away\n"); | |
1051 | buf_discard(buf); | |
1052 | if (imp > CONN_MANAGER) { | |
1053 | warn("Resetting <%s>, send queue full", l_ptr->name); | |
4323add6 | 1054 | tipc_link_reset(l_ptr); |
b97bf3fd PL |
1055 | } |
1056 | return dsz; | |
1057 | } | |
1058 | ||
1059 | /* Fragmentation needed ? */ | |
1060 | ||
1061 | if (size > max_packet) | |
4323add6 | 1062 | return tipc_link_send_long_buf(l_ptr, buf); |
b97bf3fd PL |
1063 | |
1064 | /* Packet can be queued or sent: */ | |
1065 | ||
1066 | if (queue_size > l_ptr->stats.max_queue_sz) | |
1067 | l_ptr->stats.max_queue_sz = queue_size; | |
1068 | ||
4323add6 | 1069 | if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) && |
b97bf3fd PL |
1070 | !link_congested(l_ptr))) { |
1071 | link_add_to_outqueue(l_ptr, buf, msg); | |
1072 | ||
4323add6 | 1073 | if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) { |
b97bf3fd PL |
1074 | l_ptr->unacked_window = 0; |
1075 | } else { | |
4323add6 | 1076 | tipc_bearer_schedule(l_ptr->b_ptr, l_ptr); |
b97bf3fd PL |
1077 | l_ptr->stats.bearer_congs++; |
1078 | l_ptr->next_out = buf; | |
1079 | } | |
1080 | return dsz; | |
1081 | } | |
1082 | /* Congestion: can message be bundled ?: */ | |
1083 | ||
1084 | if ((msg_user(msg) != CHANGEOVER_PROTOCOL) && | |
1085 | (msg_user(msg) != MSG_FRAGMENTER)) { | |
1086 | ||
1087 | /* Try adding message to an existing bundle */ | |
1088 | ||
1089 | if (l_ptr->next_out && | |
1090 | link_bundle_buf(l_ptr, l_ptr->last_out, buf)) { | |
4323add6 | 1091 | tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr); |
b97bf3fd PL |
1092 | return dsz; |
1093 | } | |
1094 | ||
1095 | /* Try creating a new bundle */ | |
1096 | ||
1097 | if (size <= max_packet * 2 / 3) { | |
1098 | struct sk_buff *bundler = buf_acquire(max_packet); | |
1099 | struct tipc_msg bundler_hdr; | |
1100 | ||
1101 | if (bundler) { | |
1102 | msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG, | |
1103 | TIPC_OK, INT_H_SIZE, l_ptr->addr); | |
1104 | memcpy(bundler->data, (unchar *)&bundler_hdr, | |
1105 | INT_H_SIZE); | |
1106 | skb_trim(bundler, INT_H_SIZE); | |
1107 | link_bundle_buf(l_ptr, bundler, buf); | |
1108 | buf = bundler; | |
1109 | msg = buf_msg(buf); | |
1110 | l_ptr->stats.sent_bundles++; | |
1111 | } | |
1112 | } | |
1113 | } | |
1114 | if (!l_ptr->next_out) | |
1115 | l_ptr->next_out = buf; | |
1116 | link_add_to_outqueue(l_ptr, buf, msg); | |
4323add6 | 1117 | tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr); |
b97bf3fd PL |
1118 | return dsz; |
1119 | } | |
1120 | ||
1121 | /* | |
4323add6 | 1122 | * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has |
b97bf3fd PL |
1123 | * not been selected yet, and the the owner node is not locked |
1124 | * Called by TIPC internal users, e.g. the name distributor | |
1125 | */ | |
1126 | ||
4323add6 | 1127 | int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector) |
b97bf3fd PL |
1128 | { |
1129 | struct link *l_ptr; | |
1130 | struct node *n_ptr; | |
1131 | int res = -ELINKCONG; | |
1132 | ||
4323add6 PL |
1133 | read_lock_bh(&tipc_net_lock); |
1134 | n_ptr = tipc_node_select(dest, selector); | |
b97bf3fd | 1135 | if (n_ptr) { |
4323add6 | 1136 | tipc_node_lock(n_ptr); |
b97bf3fd | 1137 | l_ptr = n_ptr->active_links[selector & 1]; |
b97bf3fd | 1138 | if (l_ptr) { |
c33d53b2 | 1139 | dbg("tipc_link_send: found link %x for dest %x\n", l_ptr, dest); |
4323add6 | 1140 | res = tipc_link_send_buf(l_ptr, buf); |
c33d53b2 AS |
1141 | } else { |
1142 | dbg("Attempt to send msg to unreachable node:\n"); | |
1143 | msg_dbg(buf_msg(buf),">>>"); | |
1144 | buf_discard(buf); | |
b97bf3fd | 1145 | } |
4323add6 | 1146 | tipc_node_unlock(n_ptr); |
b97bf3fd PL |
1147 | } else { |
1148 | dbg("Attempt to send msg to unknown node:\n"); | |
1149 | msg_dbg(buf_msg(buf),">>>"); | |
1150 | buf_discard(buf); | |
1151 | } | |
4323add6 | 1152 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
1153 | return res; |
1154 | } | |
1155 | ||
1156 | /* | |
1157 | * link_send_buf_fast: Entry for data messages where the | |
1158 | * destination link is known and the header is complete, | |
1159 | * inclusive total message length. Very time critical. | |
1160 | * Link is locked. Returns user data length. | |
1161 | */ | |
1162 | ||
05790c64 SR |
1163 | static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf, |
1164 | u32 *used_max_pkt) | |
b97bf3fd PL |
1165 | { |
1166 | struct tipc_msg *msg = buf_msg(buf); | |
1167 | int res = msg_data_sz(msg); | |
1168 | ||
1169 | if (likely(!link_congested(l_ptr))) { | |
1170 | if (likely(msg_size(msg) <= link_max_pkt(l_ptr))) { | |
1171 | if (likely(list_empty(&l_ptr->b_ptr->cong_links))) { | |
1172 | link_add_to_outqueue(l_ptr, buf, msg); | |
4323add6 PL |
1173 | if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, |
1174 | &l_ptr->media_addr))) { | |
b97bf3fd PL |
1175 | l_ptr->unacked_window = 0; |
1176 | msg_dbg(msg,"SENT_FAST:"); | |
1177 | return res; | |
1178 | } | |
1179 | dbg("failed sent fast...\n"); | |
4323add6 | 1180 | tipc_bearer_schedule(l_ptr->b_ptr, l_ptr); |
b97bf3fd PL |
1181 | l_ptr->stats.bearer_congs++; |
1182 | l_ptr->next_out = buf; | |
1183 | return res; | |
1184 | } | |
1185 | } | |
1186 | else | |
1187 | *used_max_pkt = link_max_pkt(l_ptr); | |
1188 | } | |
4323add6 | 1189 | return tipc_link_send_buf(l_ptr, buf); /* All other cases */ |
b97bf3fd PL |
1190 | } |
1191 | ||
1192 | /* | |
1193 | * tipc_send_buf_fast: Entry for data messages where the | |
1194 | * destination node is known and the header is complete, | |
1195 | * inclusive total message length. | |
1196 | * Returns user data length. | |
1197 | */ | |
1198 | int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode) | |
1199 | { | |
1200 | struct link *l_ptr; | |
1201 | struct node *n_ptr; | |
1202 | int res; | |
1203 | u32 selector = msg_origport(buf_msg(buf)) & 1; | |
1204 | u32 dummy; | |
1205 | ||
1206 | if (destnode == tipc_own_addr) | |
4323add6 | 1207 | return tipc_port_recv_msg(buf); |
b97bf3fd | 1208 | |
4323add6 PL |
1209 | read_lock_bh(&tipc_net_lock); |
1210 | n_ptr = tipc_node_select(destnode, selector); | |
b97bf3fd | 1211 | if (likely(n_ptr)) { |
4323add6 | 1212 | tipc_node_lock(n_ptr); |
b97bf3fd PL |
1213 | l_ptr = n_ptr->active_links[selector]; |
1214 | dbg("send_fast: buf %x selected %x, destnode = %x\n", | |
1215 | buf, l_ptr, destnode); | |
1216 | if (likely(l_ptr)) { | |
1217 | res = link_send_buf_fast(l_ptr, buf, &dummy); | |
4323add6 PL |
1218 | tipc_node_unlock(n_ptr); |
1219 | read_unlock_bh(&tipc_net_lock); | |
b97bf3fd PL |
1220 | return res; |
1221 | } | |
4323add6 | 1222 | tipc_node_unlock(n_ptr); |
b97bf3fd | 1223 | } |
4323add6 | 1224 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
1225 | res = msg_data_sz(buf_msg(buf)); |
1226 | tipc_reject_msg(buf, TIPC_ERR_NO_NODE); | |
1227 | return res; | |
1228 | } | |
1229 | ||
1230 | ||
1231 | /* | |
4323add6 | 1232 | * tipc_link_send_sections_fast: Entry for messages where the |
b97bf3fd PL |
1233 | * destination processor is known and the header is complete, |
1234 | * except for total message length. | |
1235 | * Returns user data length or errno. | |
1236 | */ | |
4323add6 PL |
1237 | int tipc_link_send_sections_fast(struct port *sender, |
1238 | struct iovec const *msg_sect, | |
1239 | const u32 num_sect, | |
1240 | u32 destaddr) | |
b97bf3fd PL |
1241 | { |
1242 | struct tipc_msg *hdr = &sender->publ.phdr; | |
1243 | struct link *l_ptr; | |
1244 | struct sk_buff *buf; | |
1245 | struct node *node; | |
1246 | int res; | |
1247 | u32 selector = msg_origport(hdr) & 1; | |
1248 | ||
1249 | assert(destaddr != tipc_own_addr); | |
1250 | ||
1251 | again: | |
1252 | /* | |
1253 | * Try building message using port's max_pkt hint. | |
1254 | * (Must not hold any locks while building message.) | |
1255 | */ | |
1256 | ||
1257 | res = msg_build(hdr, msg_sect, num_sect, sender->max_pkt, | |
1258 | !sender->user_port, &buf); | |
1259 | ||
4323add6 PL |
1260 | read_lock_bh(&tipc_net_lock); |
1261 | node = tipc_node_select(destaddr, selector); | |
b97bf3fd | 1262 | if (likely(node)) { |
4323add6 | 1263 | tipc_node_lock(node); |
b97bf3fd PL |
1264 | l_ptr = node->active_links[selector]; |
1265 | if (likely(l_ptr)) { | |
1266 | if (likely(buf)) { | |
1267 | res = link_send_buf_fast(l_ptr, buf, | |
1268 | &sender->max_pkt); | |
1269 | if (unlikely(res < 0)) | |
1270 | buf_discard(buf); | |
1271 | exit: | |
4323add6 PL |
1272 | tipc_node_unlock(node); |
1273 | read_unlock_bh(&tipc_net_lock); | |
b97bf3fd PL |
1274 | return res; |
1275 | } | |
1276 | ||
1277 | /* Exit if build request was invalid */ | |
1278 | ||
1279 | if (unlikely(res < 0)) | |
1280 | goto exit; | |
1281 | ||
1282 | /* Exit if link (or bearer) is congested */ | |
1283 | ||
1284 | if (link_congested(l_ptr) || | |
1285 | !list_empty(&l_ptr->b_ptr->cong_links)) { | |
1286 | res = link_schedule_port(l_ptr, | |
1287 | sender->publ.ref, res); | |
1288 | goto exit; | |
1289 | } | |
1290 | ||
1291 | /* | |
1292 | * Message size exceeds max_pkt hint; update hint, | |
1293 | * then re-try fast path or fragment the message | |
1294 | */ | |
1295 | ||
1296 | sender->max_pkt = link_max_pkt(l_ptr); | |
4323add6 PL |
1297 | tipc_node_unlock(node); |
1298 | read_unlock_bh(&tipc_net_lock); | |
b97bf3fd PL |
1299 | |
1300 | ||
1301 | if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt) | |
1302 | goto again; | |
1303 | ||
1304 | return link_send_sections_long(sender, msg_sect, | |
1305 | num_sect, destaddr); | |
1306 | } | |
4323add6 | 1307 | tipc_node_unlock(node); |
b97bf3fd | 1308 | } |
4323add6 | 1309 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
1310 | |
1311 | /* Couldn't find a link to the destination node */ | |
1312 | ||
1313 | if (buf) | |
1314 | return tipc_reject_msg(buf, TIPC_ERR_NO_NODE); | |
1315 | if (res >= 0) | |
4323add6 PL |
1316 | return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect, |
1317 | TIPC_ERR_NO_NODE); | |
b97bf3fd PL |
1318 | return res; |
1319 | } | |
1320 | ||
1321 | /* | |
1322 | * link_send_sections_long(): Entry for long messages where the | |
1323 | * destination node is known and the header is complete, | |
1324 | * inclusive total message length. | |
1325 | * Link and bearer congestion status have been checked to be ok, | |
1326 | * and are ignored if they change. | |
1327 | * | |
1328 | * Note that fragments do not use the full link MTU so that they won't have | |
1329 | * to undergo refragmentation if link changeover causes them to be sent | |
1330 | * over another link with an additional tunnel header added as prefix. | |
1331 | * (Refragmentation will still occur if the other link has a smaller MTU.) | |
1332 | * | |
1333 | * Returns user data length or errno. | |
1334 | */ | |
1335 | static int link_send_sections_long(struct port *sender, | |
1336 | struct iovec const *msg_sect, | |
1337 | u32 num_sect, | |
1338 | u32 destaddr) | |
1339 | { | |
1340 | struct link *l_ptr; | |
1341 | struct node *node; | |
1342 | struct tipc_msg *hdr = &sender->publ.phdr; | |
1343 | u32 dsz = msg_data_sz(hdr); | |
1344 | u32 max_pkt,fragm_sz,rest; | |
1345 | struct tipc_msg fragm_hdr; | |
1346 | struct sk_buff *buf,*buf_chain,*prev; | |
1347 | u32 fragm_crs,fragm_rest,hsz,sect_rest; | |
1348 | const unchar *sect_crs; | |
1349 | int curr_sect; | |
1350 | u32 fragm_no; | |
1351 | ||
1352 | again: | |
1353 | fragm_no = 1; | |
1354 | max_pkt = sender->max_pkt - INT_H_SIZE; | |
1355 | /* leave room for tunnel header in case of link changeover */ | |
1356 | fragm_sz = max_pkt - INT_H_SIZE; | |
1357 | /* leave room for fragmentation header in each fragment */ | |
1358 | rest = dsz; | |
1359 | fragm_crs = 0; | |
1360 | fragm_rest = 0; | |
1361 | sect_rest = 0; | |
1fc54d8f | 1362 | sect_crs = NULL; |
b97bf3fd PL |
1363 | curr_sect = -1; |
1364 | ||
1365 | /* Prepare reusable fragment header: */ | |
1366 | ||
1367 | msg_dbg(hdr, ">FRAGMENTING>"); | |
1368 | msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT, | |
1369 | TIPC_OK, INT_H_SIZE, msg_destnode(hdr)); | |
1370 | msg_set_link_selector(&fragm_hdr, sender->publ.ref); | |
1371 | msg_set_size(&fragm_hdr, max_pkt); | |
1372 | msg_set_fragm_no(&fragm_hdr, 1); | |
1373 | ||
1374 | /* Prepare header of first fragment: */ | |
1375 | ||
1376 | buf_chain = buf = buf_acquire(max_pkt); | |
1377 | if (!buf) | |
1378 | return -ENOMEM; | |
1379 | buf->next = NULL; | |
1380 | memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE); | |
1381 | hsz = msg_hdr_sz(hdr); | |
1382 | memcpy(buf->data + INT_H_SIZE, (unchar *)hdr, hsz); | |
1383 | msg_dbg(buf_msg(buf), ">BUILD>"); | |
1384 | ||
1385 | /* Chop up message: */ | |
1386 | ||
1387 | fragm_crs = INT_H_SIZE + hsz; | |
1388 | fragm_rest = fragm_sz - hsz; | |
1389 | ||
1390 | do { /* For all sections */ | |
1391 | u32 sz; | |
1392 | ||
1393 | if (!sect_rest) { | |
1394 | sect_rest = msg_sect[++curr_sect].iov_len; | |
1395 | sect_crs = (const unchar *)msg_sect[curr_sect].iov_base; | |
1396 | } | |
1397 | ||
1398 | if (sect_rest < fragm_rest) | |
1399 | sz = sect_rest; | |
1400 | else | |
1401 | sz = fragm_rest; | |
1402 | ||
1403 | if (likely(!sender->user_port)) { | |
1404 | if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) { | |
1405 | error: | |
1406 | for (; buf_chain; buf_chain = buf) { | |
1407 | buf = buf_chain->next; | |
1408 | buf_discard(buf_chain); | |
1409 | } | |
1410 | return -EFAULT; | |
1411 | } | |
1412 | } else | |
1413 | memcpy(buf->data + fragm_crs, sect_crs, sz); | |
1414 | ||
1415 | sect_crs += sz; | |
1416 | sect_rest -= sz; | |
1417 | fragm_crs += sz; | |
1418 | fragm_rest -= sz; | |
1419 | rest -= sz; | |
1420 | ||
1421 | if (!fragm_rest && rest) { | |
1422 | ||
1423 | /* Initiate new fragment: */ | |
1424 | if (rest <= fragm_sz) { | |
1425 | fragm_sz = rest; | |
1426 | msg_set_type(&fragm_hdr,LAST_FRAGMENT); | |
1427 | } else { | |
1428 | msg_set_type(&fragm_hdr, FRAGMENT); | |
1429 | } | |
1430 | msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE); | |
1431 | msg_set_fragm_no(&fragm_hdr, ++fragm_no); | |
1432 | prev = buf; | |
1433 | buf = buf_acquire(fragm_sz + INT_H_SIZE); | |
1434 | if (!buf) | |
1435 | goto error; | |
1436 | ||
1437 | buf->next = NULL; | |
1438 | prev->next = buf; | |
1439 | memcpy(buf->data, (unchar *)&fragm_hdr, INT_H_SIZE); | |
1440 | fragm_crs = INT_H_SIZE; | |
1441 | fragm_rest = fragm_sz; | |
1442 | msg_dbg(buf_msg(buf)," >BUILD>"); | |
1443 | } | |
1444 | } | |
1445 | while (rest > 0); | |
1446 | ||
1447 | /* | |
1448 | * Now we have a buffer chain. Select a link and check | |
1449 | * that packet size is still OK | |
1450 | */ | |
4323add6 | 1451 | node = tipc_node_select(destaddr, sender->publ.ref & 1); |
b97bf3fd | 1452 | if (likely(node)) { |
4323add6 | 1453 | tipc_node_lock(node); |
b97bf3fd PL |
1454 | l_ptr = node->active_links[sender->publ.ref & 1]; |
1455 | if (!l_ptr) { | |
4323add6 | 1456 | tipc_node_unlock(node); |
b97bf3fd PL |
1457 | goto reject; |
1458 | } | |
1459 | if (link_max_pkt(l_ptr) < max_pkt) { | |
1460 | sender->max_pkt = link_max_pkt(l_ptr); | |
4323add6 | 1461 | tipc_node_unlock(node); |
b97bf3fd PL |
1462 | for (; buf_chain; buf_chain = buf) { |
1463 | buf = buf_chain->next; | |
1464 | buf_discard(buf_chain); | |
1465 | } | |
1466 | goto again; | |
1467 | } | |
1468 | } else { | |
1469 | reject: | |
1470 | for (; buf_chain; buf_chain = buf) { | |
1471 | buf = buf_chain->next; | |
1472 | buf_discard(buf_chain); | |
1473 | } | |
4323add6 PL |
1474 | return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect, |
1475 | TIPC_ERR_NO_NODE); | |
b97bf3fd PL |
1476 | } |
1477 | ||
1478 | /* Append whole chain to send queue: */ | |
1479 | ||
1480 | buf = buf_chain; | |
1481 | l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1); | |
1482 | if (!l_ptr->next_out) | |
1483 | l_ptr->next_out = buf_chain; | |
1484 | l_ptr->stats.sent_fragmented++; | |
1485 | while (buf) { | |
1486 | struct sk_buff *next = buf->next; | |
1487 | struct tipc_msg *msg = buf_msg(buf); | |
1488 | ||
1489 | l_ptr->stats.sent_fragments++; | |
1490 | msg_set_long_msgno(msg, l_ptr->long_msg_seq_no); | |
1491 | link_add_to_outqueue(l_ptr, buf, msg); | |
1492 | msg_dbg(msg, ">ADD>"); | |
1493 | buf = next; | |
1494 | } | |
1495 | ||
1496 | /* Send it, if possible: */ | |
1497 | ||
4323add6 PL |
1498 | tipc_link_push_queue(l_ptr); |
1499 | tipc_node_unlock(node); | |
b97bf3fd PL |
1500 | return dsz; |
1501 | } | |
1502 | ||
1503 | /* | |
4323add6 | 1504 | * tipc_link_push_packet: Push one unsent packet to the media |
b97bf3fd | 1505 | */ |
4323add6 | 1506 | u32 tipc_link_push_packet(struct link *l_ptr) |
b97bf3fd PL |
1507 | { |
1508 | struct sk_buff *buf = l_ptr->first_out; | |
1509 | u32 r_q_size = l_ptr->retransm_queue_size; | |
1510 | u32 r_q_head = l_ptr->retransm_queue_head; | |
1511 | ||
1512 | /* Step to position where retransmission failed, if any, */ | |
1513 | /* consider that buffers may have been released in meantime */ | |
1514 | ||
1515 | if (r_q_size && buf) { | |
1516 | u32 last = lesser(mod(r_q_head + r_q_size), | |
1517 | link_last_sent(l_ptr)); | |
1518 | u32 first = msg_seqno(buf_msg(buf)); | |
1519 | ||
1520 | while (buf && less(first, r_q_head)) { | |
1521 | first = mod(first + 1); | |
1522 | buf = buf->next; | |
1523 | } | |
1524 | l_ptr->retransm_queue_head = r_q_head = first; | |
1525 | l_ptr->retransm_queue_size = r_q_size = mod(last - first); | |
1526 | } | |
1527 | ||
1528 | /* Continue retransmission now, if there is anything: */ | |
1529 | ||
1530 | if (r_q_size && buf && !skb_cloned(buf)) { | |
1531 | msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1)); | |
1532 | msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in); | |
4323add6 | 1533 | if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) { |
b97bf3fd PL |
1534 | msg_dbg(buf_msg(buf), ">DEF-RETR>"); |
1535 | l_ptr->retransm_queue_head = mod(++r_q_head); | |
1536 | l_ptr->retransm_queue_size = --r_q_size; | |
1537 | l_ptr->stats.retransmitted++; | |
1538 | return TIPC_OK; | |
1539 | } else { | |
1540 | l_ptr->stats.bearer_congs++; | |
1541 | msg_dbg(buf_msg(buf), "|>DEF-RETR>"); | |
1542 | return PUSH_FAILED; | |
1543 | } | |
1544 | } | |
1545 | ||
1546 | /* Send deferred protocol message, if any: */ | |
1547 | ||
1548 | buf = l_ptr->proto_msg_queue; | |
1549 | if (buf) { | |
1550 | msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1)); | |
1551 | msg_set_bcast_ack(buf_msg(buf),l_ptr->owner->bclink.last_in); | |
4323add6 | 1552 | if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) { |
b97bf3fd PL |
1553 | msg_dbg(buf_msg(buf), ">DEF-PROT>"); |
1554 | l_ptr->unacked_window = 0; | |
1555 | buf_discard(buf); | |
1fc54d8f | 1556 | l_ptr->proto_msg_queue = NULL; |
b97bf3fd PL |
1557 | return TIPC_OK; |
1558 | } else { | |
1559 | msg_dbg(buf_msg(buf), "|>DEF-PROT>"); | |
1560 | l_ptr->stats.bearer_congs++; | |
1561 | return PUSH_FAILED; | |
1562 | } | |
1563 | } | |
1564 | ||
1565 | /* Send one deferred data message, if send window not full: */ | |
1566 | ||
1567 | buf = l_ptr->next_out; | |
1568 | if (buf) { | |
1569 | struct tipc_msg *msg = buf_msg(buf); | |
1570 | u32 next = msg_seqno(msg); | |
1571 | u32 first = msg_seqno(buf_msg(l_ptr->first_out)); | |
1572 | ||
1573 | if (mod(next - first) < l_ptr->queue_limit[0]) { | |
1574 | msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); | |
1575 | msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); | |
4323add6 | 1576 | if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) { |
b97bf3fd PL |
1577 | if (msg_user(msg) == MSG_BUNDLER) |
1578 | msg_set_type(msg, CLOSED_MSG); | |
1579 | msg_dbg(msg, ">PUSH-DATA>"); | |
1580 | l_ptr->next_out = buf->next; | |
1581 | return TIPC_OK; | |
1582 | } else { | |
1583 | msg_dbg(msg, "|PUSH-DATA|"); | |
1584 | l_ptr->stats.bearer_congs++; | |
1585 | return PUSH_FAILED; | |
1586 | } | |
1587 | } | |
1588 | } | |
1589 | return PUSH_FINISHED; | |
1590 | } | |
1591 | ||
1592 | /* | |
1593 | * push_queue(): push out the unsent messages of a link where | |
1594 | * congestion has abated. Node is locked | |
1595 | */ | |
4323add6 | 1596 | void tipc_link_push_queue(struct link *l_ptr) |
b97bf3fd PL |
1597 | { |
1598 | u32 res; | |
1599 | ||
4323add6 | 1600 | if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) |
b97bf3fd PL |
1601 | return; |
1602 | ||
1603 | do { | |
4323add6 | 1604 | res = tipc_link_push_packet(l_ptr); |
b97bf3fd PL |
1605 | } |
1606 | while (res == TIPC_OK); | |
1607 | if (res == PUSH_FAILED) | |
4323add6 | 1608 | tipc_bearer_schedule(l_ptr->b_ptr, l_ptr); |
b97bf3fd PL |
1609 | } |
1610 | ||
d356eeba AS |
1611 | static void link_reset_all(unsigned long addr) |
1612 | { | |
1613 | struct node *n_ptr; | |
1614 | char addr_string[16]; | |
1615 | u32 i; | |
1616 | ||
1617 | read_lock_bh(&tipc_net_lock); | |
1618 | n_ptr = tipc_node_find((u32)addr); | |
1619 | if (!n_ptr) { | |
1620 | read_unlock_bh(&tipc_net_lock); | |
1621 | return; /* node no longer exists */ | |
1622 | } | |
1623 | ||
1624 | tipc_node_lock(n_ptr); | |
1625 | ||
1626 | warn("Resetting all links to %s\n", | |
1627 | addr_string_fill(addr_string, n_ptr->addr)); | |
1628 | ||
1629 | for (i = 0; i < MAX_BEARERS; i++) { | |
1630 | if (n_ptr->links[i]) { | |
1631 | link_print(n_ptr->links[i], TIPC_OUTPUT, | |
1632 | "Resetting link\n"); | |
1633 | tipc_link_reset(n_ptr->links[i]); | |
1634 | } | |
1635 | } | |
1636 | ||
1637 | tipc_node_unlock(n_ptr); | |
1638 | read_unlock_bh(&tipc_net_lock); | |
1639 | } | |
1640 | ||
1641 | static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf) | |
1642 | { | |
1643 | struct tipc_msg *msg = buf_msg(buf); | |
1644 | ||
1645 | warn("Retransmission failure on link <%s>\n", l_ptr->name); | |
1646 | tipc_msg_print(TIPC_OUTPUT, msg, ">RETR-FAIL>"); | |
1647 | ||
1648 | if (l_ptr->addr) { | |
1649 | ||
1650 | /* Handle failure on standard link */ | |
1651 | ||
1652 | link_print(l_ptr, TIPC_OUTPUT, "Resetting link\n"); | |
1653 | tipc_link_reset(l_ptr); | |
1654 | ||
1655 | } else { | |
1656 | ||
1657 | /* Handle failure on broadcast link */ | |
1658 | ||
1659 | struct node *n_ptr; | |
1660 | char addr_string[16]; | |
1661 | ||
1662 | tipc_printf(TIPC_OUTPUT, "Msg seq number: %u, ", msg_seqno(msg)); | |
1663 | tipc_printf(TIPC_OUTPUT, "Outstanding acks: %u\n", (u32)TIPC_SKB_CB(buf)->handle); | |
1664 | ||
1665 | n_ptr = l_ptr->owner->next; | |
1666 | tipc_node_lock(n_ptr); | |
1667 | ||
1668 | addr_string_fill(addr_string, n_ptr->addr); | |
1669 | tipc_printf(TIPC_OUTPUT, "Multicast link info for %s\n", addr_string); | |
1670 | tipc_printf(TIPC_OUTPUT, "Supported: %d, ", n_ptr->bclink.supported); | |
1671 | tipc_printf(TIPC_OUTPUT, "Acked: %u\n", n_ptr->bclink.acked); | |
1672 | tipc_printf(TIPC_OUTPUT, "Last in: %u, ", n_ptr->bclink.last_in); | |
1673 | tipc_printf(TIPC_OUTPUT, "Gap after: %u, ", n_ptr->bclink.gap_after); | |
1674 | tipc_printf(TIPC_OUTPUT, "Gap to: %u\n", n_ptr->bclink.gap_to); | |
1675 | tipc_printf(TIPC_OUTPUT, "Nack sync: %u\n\n", n_ptr->bclink.nack_sync); | |
1676 | ||
1677 | tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr); | |
1678 | ||
1679 | tipc_node_unlock(n_ptr); | |
1680 | ||
1681 | l_ptr->stale_count = 0; | |
1682 | } | |
1683 | } | |
1684 | ||
4323add6 PL |
1685 | void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf, |
1686 | u32 retransmits) | |
b97bf3fd PL |
1687 | { |
1688 | struct tipc_msg *msg; | |
1689 | ||
d356eeba AS |
1690 | if (!buf) |
1691 | return; | |
1692 | ||
1693 | msg = buf_msg(buf); | |
1694 | ||
b97bf3fd PL |
1695 | dbg("Retransmitting %u in link %x\n", retransmits, l_ptr); |
1696 | ||
d356eeba AS |
1697 | if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) { |
1698 | if (!skb_cloned(buf)) { | |
1699 | msg_dbg(msg, ">NO_RETR->BCONG>"); | |
1700 | dbg_print_link(l_ptr, " "); | |
1701 | l_ptr->retransm_queue_head = msg_seqno(msg); | |
1702 | l_ptr->retransm_queue_size = retransmits; | |
1703 | return; | |
1704 | } else { | |
1705 | /* Don't retransmit if driver already has the buffer */ | |
1706 | } | |
1707 | } else { | |
1708 | /* Detect repeated retransmit failures on uncongested bearer */ | |
1709 | ||
1710 | if (l_ptr->last_retransmitted == msg_seqno(msg)) { | |
1711 | if (++l_ptr->stale_count > 100) { | |
1712 | link_retransmit_failure(l_ptr, buf); | |
1713 | return; | |
1714 | } | |
1715 | } else { | |
1716 | l_ptr->last_retransmitted = msg_seqno(msg); | |
1717 | l_ptr->stale_count = 1; | |
1718 | } | |
b97bf3fd | 1719 | } |
d356eeba | 1720 | |
b97bf3fd PL |
1721 | while (retransmits && (buf != l_ptr->next_out) && buf && !skb_cloned(buf)) { |
1722 | msg = buf_msg(buf); | |
1723 | msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); | |
1724 | msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); | |
4323add6 | 1725 | if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) { |
b97bf3fd PL |
1726 | msg_dbg(buf_msg(buf), ">RETR>"); |
1727 | buf = buf->next; | |
1728 | retransmits--; | |
1729 | l_ptr->stats.retransmitted++; | |
1730 | } else { | |
4323add6 | 1731 | tipc_bearer_schedule(l_ptr->b_ptr, l_ptr); |
b97bf3fd PL |
1732 | l_ptr->stats.bearer_congs++; |
1733 | l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf)); | |
1734 | l_ptr->retransm_queue_size = retransmits; | |
1735 | return; | |
1736 | } | |
1737 | } | |
d356eeba | 1738 | |
b97bf3fd PL |
1739 | l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0; |
1740 | } | |
1741 | ||
1742 | /* | |
1743 | * link_recv_non_seq: Receive packets which are outside | |
1744 | * the link sequence flow | |
1745 | */ | |
1746 | ||
1747 | static void link_recv_non_seq(struct sk_buff *buf) | |
1748 | { | |
1749 | struct tipc_msg *msg = buf_msg(buf); | |
1750 | ||
1751 | if (msg_user(msg) == LINK_CONFIG) | |
4323add6 | 1752 | tipc_disc_recv_msg(buf); |
b97bf3fd | 1753 | else |
4323add6 | 1754 | tipc_bclink_recv_pkt(buf); |
b97bf3fd PL |
1755 | } |
1756 | ||
1757 | /** | |
1758 | * link_insert_deferred_queue - insert deferred messages back into receive chain | |
1759 | */ | |
1760 | ||
1761 | static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr, | |
1762 | struct sk_buff *buf) | |
1763 | { | |
1764 | u32 seq_no; | |
1765 | ||
1766 | if (l_ptr->oldest_deferred_in == NULL) | |
1767 | return buf; | |
1768 | ||
1769 | seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in)); | |
1770 | if (seq_no == mod(l_ptr->next_in_no)) { | |
1771 | l_ptr->newest_deferred_in->next = buf; | |
1772 | buf = l_ptr->oldest_deferred_in; | |
1773 | l_ptr->oldest_deferred_in = NULL; | |
1774 | l_ptr->deferred_inqueue_sz = 0; | |
1775 | } | |
1776 | return buf; | |
1777 | } | |
1778 | ||
1779 | void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *tb_ptr) | |
1780 | { | |
4323add6 | 1781 | read_lock_bh(&tipc_net_lock); |
b97bf3fd PL |
1782 | while (head) { |
1783 | struct bearer *b_ptr; | |
1784 | struct node *n_ptr; | |
1785 | struct link *l_ptr; | |
1786 | struct sk_buff *crs; | |
1787 | struct sk_buff *buf = head; | |
1788 | struct tipc_msg *msg = buf_msg(buf); | |
1789 | u32 seq_no = msg_seqno(msg); | |
1790 | u32 ackd = msg_ack(msg); | |
1791 | u32 released = 0; | |
1792 | int type; | |
1793 | ||
1794 | b_ptr = (struct bearer *)tb_ptr; | |
1795 | TIPC_SKB_CB(buf)->handle = b_ptr; | |
1796 | ||
1797 | head = head->next; | |
1798 | if (unlikely(msg_version(msg) != TIPC_VERSION)) | |
1799 | goto cont; | |
1800 | #if 0 | |
1801 | if (msg_user(msg) != LINK_PROTOCOL) | |
1802 | #endif | |
1803 | msg_dbg(msg,"<REC<"); | |
1804 | ||
1805 | if (unlikely(msg_non_seq(msg))) { | |
1806 | link_recv_non_seq(buf); | |
1807 | continue; | |
1808 | } | |
26008247 AS |
1809 | |
1810 | if (unlikely(!msg_short(msg) && | |
1811 | (msg_destnode(msg) != tipc_own_addr))) | |
1812 | goto cont; | |
1813 | ||
4323add6 | 1814 | n_ptr = tipc_node_find(msg_prevnode(msg)); |
b97bf3fd PL |
1815 | if (unlikely(!n_ptr)) |
1816 | goto cont; | |
1817 | ||
4323add6 | 1818 | tipc_node_lock(n_ptr); |
b97bf3fd PL |
1819 | l_ptr = n_ptr->links[b_ptr->identity]; |
1820 | if (unlikely(!l_ptr)) { | |
4323add6 | 1821 | tipc_node_unlock(n_ptr); |
b97bf3fd PL |
1822 | goto cont; |
1823 | } | |
1824 | /* | |
1825 | * Release acked messages | |
1826 | */ | |
1827 | if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) { | |
4323add6 PL |
1828 | if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported) |
1829 | tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg)); | |
b97bf3fd PL |
1830 | } |
1831 | ||
1832 | crs = l_ptr->first_out; | |
1833 | while ((crs != l_ptr->next_out) && | |
1834 | less_eq(msg_seqno(buf_msg(crs)), ackd)) { | |
1835 | struct sk_buff *next = crs->next; | |
1836 | ||
1837 | buf_discard(crs); | |
1838 | crs = next; | |
1839 | released++; | |
1840 | } | |
1841 | if (released) { | |
1842 | l_ptr->first_out = crs; | |
1843 | l_ptr->out_queue_size -= released; | |
1844 | } | |
1845 | if (unlikely(l_ptr->next_out)) | |
4323add6 | 1846 | tipc_link_push_queue(l_ptr); |
b97bf3fd | 1847 | if (unlikely(!list_empty(&l_ptr->waiting_ports))) |
4323add6 | 1848 | tipc_link_wakeup_ports(l_ptr, 0); |
b97bf3fd PL |
1849 | if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) { |
1850 | l_ptr->stats.sent_acks++; | |
4323add6 | 1851 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0); |
b97bf3fd PL |
1852 | } |
1853 | ||
1854 | protocol_check: | |
1855 | if (likely(link_working_working(l_ptr))) { | |
1856 | if (likely(seq_no == mod(l_ptr->next_in_no))) { | |
1857 | l_ptr->next_in_no++; | |
1858 | if (unlikely(l_ptr->oldest_deferred_in)) | |
1859 | head = link_insert_deferred_queue(l_ptr, | |
1860 | head); | |
1861 | if (likely(msg_is_dest(msg, tipc_own_addr))) { | |
1862 | deliver: | |
1863 | if (likely(msg_isdata(msg))) { | |
4323add6 PL |
1864 | tipc_node_unlock(n_ptr); |
1865 | tipc_port_recv_msg(buf); | |
b97bf3fd PL |
1866 | continue; |
1867 | } | |
1868 | switch (msg_user(msg)) { | |
1869 | case MSG_BUNDLER: | |
1870 | l_ptr->stats.recv_bundles++; | |
1871 | l_ptr->stats.recv_bundled += | |
1872 | msg_msgcnt(msg); | |
4323add6 PL |
1873 | tipc_node_unlock(n_ptr); |
1874 | tipc_link_recv_bundle(buf); | |
b97bf3fd PL |
1875 | continue; |
1876 | case ROUTE_DISTRIBUTOR: | |
4323add6 PL |
1877 | tipc_node_unlock(n_ptr); |
1878 | tipc_cltr_recv_routing_table(buf); | |
b97bf3fd PL |
1879 | continue; |
1880 | case NAME_DISTRIBUTOR: | |
4323add6 PL |
1881 | tipc_node_unlock(n_ptr); |
1882 | tipc_named_recv(buf); | |
b97bf3fd PL |
1883 | continue; |
1884 | case CONN_MANAGER: | |
4323add6 PL |
1885 | tipc_node_unlock(n_ptr); |
1886 | tipc_port_recv_proto_msg(buf); | |
b97bf3fd PL |
1887 | continue; |
1888 | case MSG_FRAGMENTER: | |
1889 | l_ptr->stats.recv_fragments++; | |
4323add6 PL |
1890 | if (tipc_link_recv_fragment(&l_ptr->defragm_buf, |
1891 | &buf, &msg)) { | |
b97bf3fd PL |
1892 | l_ptr->stats.recv_fragmented++; |
1893 | goto deliver; | |
1894 | } | |
1895 | break; | |
1896 | case CHANGEOVER_PROTOCOL: | |
1897 | type = msg_type(msg); | |
4323add6 | 1898 | if (link_recv_changeover_msg(&l_ptr, &buf)) { |
b97bf3fd PL |
1899 | msg = buf_msg(buf); |
1900 | seq_no = msg_seqno(msg); | |
1901 | TIPC_SKB_CB(buf)->handle | |
1902 | = b_ptr; | |
1903 | if (type == ORIGINAL_MSG) | |
1904 | goto deliver; | |
1905 | goto protocol_check; | |
1906 | } | |
1907 | break; | |
1908 | } | |
1909 | } | |
4323add6 PL |
1910 | tipc_node_unlock(n_ptr); |
1911 | tipc_net_route_msg(buf); | |
b97bf3fd PL |
1912 | continue; |
1913 | } | |
1914 | link_handle_out_of_seq_msg(l_ptr, buf); | |
1915 | head = link_insert_deferred_queue(l_ptr, head); | |
4323add6 | 1916 | tipc_node_unlock(n_ptr); |
b97bf3fd PL |
1917 | continue; |
1918 | } | |
1919 | ||
1920 | if (msg_user(msg) == LINK_PROTOCOL) { | |
1921 | link_recv_proto_msg(l_ptr, buf); | |
1922 | head = link_insert_deferred_queue(l_ptr, head); | |
4323add6 | 1923 | tipc_node_unlock(n_ptr); |
b97bf3fd PL |
1924 | continue; |
1925 | } | |
1926 | msg_dbg(msg,"NSEQ<REC<"); | |
1927 | link_state_event(l_ptr, TRAFFIC_MSG_EVT); | |
1928 | ||
1929 | if (link_working_working(l_ptr)) { | |
1930 | /* Re-insert in front of queue */ | |
1931 | msg_dbg(msg,"RECV-REINS:"); | |
1932 | buf->next = head; | |
1933 | head = buf; | |
4323add6 | 1934 | tipc_node_unlock(n_ptr); |
b97bf3fd PL |
1935 | continue; |
1936 | } | |
4323add6 | 1937 | tipc_node_unlock(n_ptr); |
b97bf3fd PL |
1938 | cont: |
1939 | buf_discard(buf); | |
1940 | } | |
4323add6 | 1941 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
1942 | } |
1943 | ||
1944 | /* | |
1945 | * link_defer_buf(): Sort a received out-of-sequence packet | |
1946 | * into the deferred reception queue. | |
1947 | * Returns the increase of the queue length,i.e. 0 or 1 | |
1948 | */ | |
1949 | ||
4323add6 PL |
1950 | u32 tipc_link_defer_pkt(struct sk_buff **head, |
1951 | struct sk_buff **tail, | |
1952 | struct sk_buff *buf) | |
b97bf3fd | 1953 | { |
1fc54d8f | 1954 | struct sk_buff *prev = NULL; |
b97bf3fd PL |
1955 | struct sk_buff *crs = *head; |
1956 | u32 seq_no = msg_seqno(buf_msg(buf)); | |
1957 | ||
1958 | buf->next = NULL; | |
1959 | ||
1960 | /* Empty queue ? */ | |
1961 | if (*head == NULL) { | |
1962 | *head = *tail = buf; | |
1963 | return 1; | |
1964 | } | |
1965 | ||
1966 | /* Last ? */ | |
1967 | if (less(msg_seqno(buf_msg(*tail)), seq_no)) { | |
1968 | (*tail)->next = buf; | |
1969 | *tail = buf; | |
1970 | return 1; | |
1971 | } | |
1972 | ||
1973 | /* Scan through queue and sort it in */ | |
1974 | do { | |
1975 | struct tipc_msg *msg = buf_msg(crs); | |
1976 | ||
1977 | if (less(seq_no, msg_seqno(msg))) { | |
1978 | buf->next = crs; | |
1979 | if (prev) | |
1980 | prev->next = buf; | |
1981 | else | |
1982 | *head = buf; | |
1983 | return 1; | |
1984 | } | |
1985 | if (seq_no == msg_seqno(msg)) { | |
1986 | break; | |
1987 | } | |
1988 | prev = crs; | |
1989 | crs = crs->next; | |
1990 | } | |
1991 | while (crs); | |
1992 | ||
1993 | /* Message is a duplicate of an existing message */ | |
1994 | ||
1995 | buf_discard(buf); | |
1996 | return 0; | |
1997 | } | |
1998 | ||
1999 | /** | |
2000 | * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet | |
2001 | */ | |
2002 | ||
2003 | static void link_handle_out_of_seq_msg(struct link *l_ptr, | |
2004 | struct sk_buff *buf) | |
2005 | { | |
2006 | u32 seq_no = msg_seqno(buf_msg(buf)); | |
2007 | ||
2008 | if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) { | |
2009 | link_recv_proto_msg(l_ptr, buf); | |
2010 | return; | |
2011 | } | |
2012 | ||
2013 | dbg("rx OOS msg: seq_no %u, expecting %u (%u)\n", | |
2014 | seq_no, mod(l_ptr->next_in_no), l_ptr->next_in_no); | |
2015 | ||
2016 | /* Record OOS packet arrival (force mismatch on next timeout) */ | |
2017 | ||
2018 | l_ptr->checkpoint--; | |
2019 | ||
2020 | /* | |
2021 | * Discard packet if a duplicate; otherwise add it to deferred queue | |
2022 | * and notify peer of gap as per protocol specification | |
2023 | */ | |
2024 | ||
2025 | if (less(seq_no, mod(l_ptr->next_in_no))) { | |
2026 | l_ptr->stats.duplicates++; | |
2027 | buf_discard(buf); | |
2028 | return; | |
2029 | } | |
2030 | ||
4323add6 PL |
2031 | if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in, |
2032 | &l_ptr->newest_deferred_in, buf)) { | |
b97bf3fd PL |
2033 | l_ptr->deferred_inqueue_sz++; |
2034 | l_ptr->stats.deferred_recv++; | |
2035 | if ((l_ptr->deferred_inqueue_sz % 16) == 1) | |
4323add6 | 2036 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0); |
b97bf3fd PL |
2037 | } else |
2038 | l_ptr->stats.duplicates++; | |
2039 | } | |
2040 | ||
2041 | /* | |
2042 | * Send protocol message to the other endpoint. | |
2043 | */ | |
4323add6 PL |
2044 | void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg, |
2045 | u32 gap, u32 tolerance, u32 priority, u32 ack_mtu) | |
b97bf3fd | 2046 | { |
1fc54d8f | 2047 | struct sk_buff *buf = NULL; |
b97bf3fd PL |
2048 | struct tipc_msg *msg = l_ptr->pmsg; |
2049 | u32 msg_size = sizeof(l_ptr->proto_msg); | |
2050 | ||
2051 | if (link_blocked(l_ptr)) | |
2052 | return; | |
2053 | msg_set_type(msg, msg_typ); | |
2054 | msg_set_net_plane(msg, l_ptr->b_ptr->net_plane); | |
2055 | msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in)); | |
4323add6 | 2056 | msg_set_last_bcast(msg, tipc_bclink_get_last_sent()); |
b97bf3fd PL |
2057 | |
2058 | if (msg_typ == STATE_MSG) { | |
2059 | u32 next_sent = mod(l_ptr->next_out_no); | |
2060 | ||
4323add6 | 2061 | if (!tipc_link_is_up(l_ptr)) |
b97bf3fd PL |
2062 | return; |
2063 | if (l_ptr->next_out) | |
2064 | next_sent = msg_seqno(buf_msg(l_ptr->next_out)); | |
2065 | msg_set_next_sent(msg, next_sent); | |
2066 | if (l_ptr->oldest_deferred_in) { | |
2067 | u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in)); | |
2068 | gap = mod(rec - mod(l_ptr->next_in_no)); | |
2069 | } | |
2070 | msg_set_seq_gap(msg, gap); | |
2071 | if (gap) | |
2072 | l_ptr->stats.sent_nacks++; | |
2073 | msg_set_link_tolerance(msg, tolerance); | |
2074 | msg_set_linkprio(msg, priority); | |
2075 | msg_set_max_pkt(msg, ack_mtu); | |
2076 | msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); | |
2077 | msg_set_probe(msg, probe_msg != 0); | |
2078 | if (probe_msg) { | |
2079 | u32 mtu = l_ptr->max_pkt; | |
2080 | ||
2081 | if ((mtu < l_ptr->max_pkt_target) && | |
2082 | link_working_working(l_ptr) && | |
2083 | l_ptr->fsm_msg_cnt) { | |
2084 | msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3; | |
2085 | if (l_ptr->max_pkt_probes == 10) { | |
2086 | l_ptr->max_pkt_target = (msg_size - 4); | |
2087 | l_ptr->max_pkt_probes = 0; | |
2088 | msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3; | |
2089 | } | |
2090 | l_ptr->max_pkt_probes++; | |
2091 | } | |
2092 | ||
2093 | l_ptr->stats.sent_probes++; | |
2094 | } | |
2095 | l_ptr->stats.sent_states++; | |
2096 | } else { /* RESET_MSG or ACTIVATE_MSG */ | |
2097 | msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1)); | |
2098 | msg_set_seq_gap(msg, 0); | |
2099 | msg_set_next_sent(msg, 1); | |
2100 | msg_set_link_tolerance(msg, l_ptr->tolerance); | |
2101 | msg_set_linkprio(msg, l_ptr->priority); | |
2102 | msg_set_max_pkt(msg, l_ptr->max_pkt_target); | |
2103 | } | |
2104 | ||
4323add6 | 2105 | if (tipc_node_has_redundant_links(l_ptr->owner)) { |
b97bf3fd PL |
2106 | msg_set_redundant_link(msg); |
2107 | } else { | |
2108 | msg_clear_redundant_link(msg); | |
2109 | } | |
2110 | msg_set_linkprio(msg, l_ptr->priority); | |
2111 | ||
2112 | /* Ensure sequence number will not fit : */ | |
2113 | ||
2114 | msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2))); | |
2115 | ||
2116 | /* Congestion? */ | |
2117 | ||
4323add6 | 2118 | if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) { |
b97bf3fd PL |
2119 | if (!l_ptr->proto_msg_queue) { |
2120 | l_ptr->proto_msg_queue = | |
2121 | buf_acquire(sizeof(l_ptr->proto_msg)); | |
2122 | } | |
2123 | buf = l_ptr->proto_msg_queue; | |
2124 | if (!buf) | |
2125 | return; | |
2126 | memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg)); | |
2127 | return; | |
2128 | } | |
2129 | msg_set_timestamp(msg, jiffies_to_msecs(jiffies)); | |
2130 | ||
2131 | /* Message can be sent */ | |
2132 | ||
2133 | msg_dbg(msg, ">>"); | |
2134 | ||
2135 | buf = buf_acquire(msg_size); | |
2136 | if (!buf) | |
2137 | return; | |
2138 | ||
2139 | memcpy(buf->data, (unchar *)msg, sizeof(l_ptr->proto_msg)); | |
2140 | msg_set_size(buf_msg(buf), msg_size); | |
2141 | ||
4323add6 | 2142 | if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) { |
b97bf3fd PL |
2143 | l_ptr->unacked_window = 0; |
2144 | buf_discard(buf); | |
2145 | return; | |
2146 | } | |
2147 | ||
2148 | /* New congestion */ | |
4323add6 | 2149 | tipc_bearer_schedule(l_ptr->b_ptr, l_ptr); |
b97bf3fd PL |
2150 | l_ptr->proto_msg_queue = buf; |
2151 | l_ptr->stats.bearer_congs++; | |
2152 | } | |
2153 | ||
2154 | /* | |
2155 | * Receive protocol message : | |
2156 | * Note that network plane id propagates through the network, and may | |
2157 | * change at any time. The node with lowest address rules | |
2158 | */ | |
2159 | ||
2160 | static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf) | |
2161 | { | |
2162 | u32 rec_gap = 0; | |
2163 | u32 max_pkt_info; | |
2164 | u32 max_pkt_ack; | |
2165 | u32 msg_tol; | |
2166 | struct tipc_msg *msg = buf_msg(buf); | |
2167 | ||
2168 | dbg("AT(%u):", jiffies_to_msecs(jiffies)); | |
2169 | msg_dbg(msg, "<<"); | |
2170 | if (link_blocked(l_ptr)) | |
2171 | goto exit; | |
2172 | ||
2173 | /* record unnumbered packet arrival (force mismatch on next timeout) */ | |
2174 | ||
2175 | l_ptr->checkpoint--; | |
2176 | ||
2177 | if (l_ptr->b_ptr->net_plane != msg_net_plane(msg)) | |
2178 | if (tipc_own_addr > msg_prevnode(msg)) | |
2179 | l_ptr->b_ptr->net_plane = msg_net_plane(msg); | |
2180 | ||
2181 | l_ptr->owner->permit_changeover = msg_redundant_link(msg); | |
2182 | ||
2183 | switch (msg_type(msg)) { | |
2184 | ||
2185 | case RESET_MSG: | |
2186 | if (!link_working_unknown(l_ptr) && l_ptr->peer_session) { | |
2187 | if (msg_session(msg) == l_ptr->peer_session) { | |
2188 | dbg("Duplicate RESET: %u<->%u\n", | |
2189 | msg_session(msg), l_ptr->peer_session); | |
2190 | break; /* duplicate: ignore */ | |
2191 | } | |
2192 | } | |
2193 | /* fall thru' */ | |
2194 | case ACTIVATE_MSG: | |
2195 | /* Update link settings according other endpoint's values */ | |
2196 | ||
2197 | strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg)); | |
2198 | ||
2199 | if ((msg_tol = msg_link_tolerance(msg)) && | |
2200 | (msg_tol > l_ptr->tolerance)) | |
2201 | link_set_supervision_props(l_ptr, msg_tol); | |
2202 | ||
2203 | if (msg_linkprio(msg) > l_ptr->priority) | |
2204 | l_ptr->priority = msg_linkprio(msg); | |
2205 | ||
2206 | max_pkt_info = msg_max_pkt(msg); | |
2207 | if (max_pkt_info) { | |
2208 | if (max_pkt_info < l_ptr->max_pkt_target) | |
2209 | l_ptr->max_pkt_target = max_pkt_info; | |
2210 | if (l_ptr->max_pkt > l_ptr->max_pkt_target) | |
2211 | l_ptr->max_pkt = l_ptr->max_pkt_target; | |
2212 | } else { | |
2213 | l_ptr->max_pkt = l_ptr->max_pkt_target; | |
2214 | } | |
2215 | l_ptr->owner->bclink.supported = (max_pkt_info != 0); | |
2216 | ||
2217 | link_state_event(l_ptr, msg_type(msg)); | |
2218 | ||
2219 | l_ptr->peer_session = msg_session(msg); | |
2220 | l_ptr->peer_bearer_id = msg_bearer_id(msg); | |
2221 | ||
2222 | /* Synchronize broadcast sequence numbers */ | |
4323add6 | 2223 | if (!tipc_node_has_redundant_links(l_ptr->owner)) { |
b97bf3fd PL |
2224 | l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg)); |
2225 | } | |
2226 | break; | |
2227 | case STATE_MSG: | |
2228 | ||
2229 | if ((msg_tol = msg_link_tolerance(msg))) | |
2230 | link_set_supervision_props(l_ptr, msg_tol); | |
2231 | ||
2232 | if (msg_linkprio(msg) && | |
2233 | (msg_linkprio(msg) != l_ptr->priority)) { | |
2234 | warn("Changing prio <%s>: %u->%u\n", | |
2235 | l_ptr->name, l_ptr->priority, msg_linkprio(msg)); | |
2236 | l_ptr->priority = msg_linkprio(msg); | |
4323add6 | 2237 | tipc_link_reset(l_ptr); /* Enforce change to take effect */ |
b97bf3fd PL |
2238 | break; |
2239 | } | |
2240 | link_state_event(l_ptr, TRAFFIC_MSG_EVT); | |
2241 | l_ptr->stats.recv_states++; | |
2242 | if (link_reset_unknown(l_ptr)) | |
2243 | break; | |
2244 | ||
2245 | if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) { | |
2246 | rec_gap = mod(msg_next_sent(msg) - | |
2247 | mod(l_ptr->next_in_no)); | |
2248 | } | |
2249 | ||
2250 | max_pkt_ack = msg_max_pkt(msg); | |
2251 | if (max_pkt_ack > l_ptr->max_pkt) { | |
2252 | dbg("Link <%s> updated MTU %u -> %u\n", | |
2253 | l_ptr->name, l_ptr->max_pkt, max_pkt_ack); | |
2254 | l_ptr->max_pkt = max_pkt_ack; | |
2255 | l_ptr->max_pkt_probes = 0; | |
2256 | } | |
2257 | ||
2258 | max_pkt_ack = 0; | |
2259 | if (msg_probe(msg)) { | |
2260 | l_ptr->stats.recv_probes++; | |
2261 | if (msg_size(msg) > sizeof(l_ptr->proto_msg)) { | |
2262 | max_pkt_ack = msg_size(msg); | |
2263 | } | |
2264 | } | |
2265 | ||
2266 | /* Protocol message before retransmits, reduce loss risk */ | |
2267 | ||
4323add6 | 2268 | tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg)); |
b97bf3fd PL |
2269 | |
2270 | if (rec_gap || (msg_probe(msg))) { | |
4323add6 PL |
2271 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, |
2272 | 0, rec_gap, 0, 0, max_pkt_ack); | |
b97bf3fd PL |
2273 | } |
2274 | if (msg_seq_gap(msg)) { | |
2275 | msg_dbg(msg, "With Gap:"); | |
2276 | l_ptr->stats.recv_nacks++; | |
4323add6 PL |
2277 | tipc_link_retransmit(l_ptr, l_ptr->first_out, |
2278 | msg_seq_gap(msg)); | |
b97bf3fd PL |
2279 | } |
2280 | break; | |
2281 | default: | |
2282 | msg_dbg(buf_msg(buf), "<DISCARDING UNKNOWN<"); | |
2283 | } | |
2284 | exit: | |
2285 | buf_discard(buf); | |
2286 | } | |
2287 | ||
2288 | ||
2289 | /* | |
4323add6 | 2290 | * tipc_link_tunnel(): Send one message via a link belonging to |
b97bf3fd PL |
2291 | * another bearer. Owner node is locked. |
2292 | */ | |
4323add6 PL |
2293 | void tipc_link_tunnel(struct link *l_ptr, |
2294 | struct tipc_msg *tunnel_hdr, | |
2295 | struct tipc_msg *msg, | |
2296 | u32 selector) | |
b97bf3fd PL |
2297 | { |
2298 | struct link *tunnel; | |
2299 | struct sk_buff *buf; | |
2300 | u32 length = msg_size(msg); | |
2301 | ||
2302 | tunnel = l_ptr->owner->active_links[selector & 1]; | |
4323add6 | 2303 | if (!tipc_link_is_up(tunnel)) |
b97bf3fd PL |
2304 | return; |
2305 | msg_set_size(tunnel_hdr, length + INT_H_SIZE); | |
2306 | buf = buf_acquire(length + INT_H_SIZE); | |
2307 | if (!buf) | |
2308 | return; | |
2309 | memcpy(buf->data, (unchar *)tunnel_hdr, INT_H_SIZE); | |
2310 | memcpy(buf->data + INT_H_SIZE, (unchar *)msg, length); | |
2311 | dbg("%c->%c:", l_ptr->b_ptr->net_plane, tunnel->b_ptr->net_plane); | |
2312 | msg_dbg(buf_msg(buf), ">SEND>"); | |
2313 | assert(tunnel); | |
4323add6 | 2314 | tipc_link_send_buf(tunnel, buf); |
b97bf3fd PL |
2315 | } |
2316 | ||
2317 | ||
2318 | ||
2319 | /* | |
2320 | * changeover(): Send whole message queue via the remaining link | |
2321 | * Owner node is locked. | |
2322 | */ | |
2323 | ||
4323add6 | 2324 | void tipc_link_changeover(struct link *l_ptr) |
b97bf3fd PL |
2325 | { |
2326 | u32 msgcount = l_ptr->out_queue_size; | |
2327 | struct sk_buff *crs = l_ptr->first_out; | |
2328 | struct link *tunnel = l_ptr->owner->active_links[0]; | |
4323add6 | 2329 | int split_bundles = tipc_node_has_redundant_links(l_ptr->owner); |
b97bf3fd PL |
2330 | struct tipc_msg tunnel_hdr; |
2331 | ||
2332 | if (!tunnel) | |
2333 | return; | |
2334 | ||
2335 | if (!l_ptr->owner->permit_changeover) | |
2336 | return; | |
2337 | ||
2338 | msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL, | |
2339 | ORIGINAL_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr); | |
2340 | msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id); | |
2341 | msg_set_msgcnt(&tunnel_hdr, msgcount); | |
2342 | if (!l_ptr->first_out) { | |
2343 | struct sk_buff *buf; | |
2344 | ||
2345 | assert(!msgcount); | |
2346 | buf = buf_acquire(INT_H_SIZE); | |
2347 | if (buf) { | |
2348 | memcpy(buf->data, (unchar *)&tunnel_hdr, INT_H_SIZE); | |
2349 | msg_set_size(&tunnel_hdr, INT_H_SIZE); | |
2350 | dbg("%c->%c:", l_ptr->b_ptr->net_plane, | |
2351 | tunnel->b_ptr->net_plane); | |
2352 | msg_dbg(&tunnel_hdr, "EMPTY>SEND>"); | |
4323add6 | 2353 | tipc_link_send_buf(tunnel, buf); |
b97bf3fd PL |
2354 | } else { |
2355 | warn("Memory squeeze; link changeover failed\n"); | |
2356 | } | |
2357 | return; | |
2358 | } | |
2359 | while (crs) { | |
2360 | struct tipc_msg *msg = buf_msg(crs); | |
2361 | ||
2362 | if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) { | |
2363 | u32 msgcount = msg_msgcnt(msg); | |
2364 | struct tipc_msg *m = msg_get_wrapped(msg); | |
2365 | unchar* pos = (unchar*)m; | |
2366 | ||
2367 | while (msgcount--) { | |
2368 | msg_set_seqno(m,msg_seqno(msg)); | |
4323add6 PL |
2369 | tipc_link_tunnel(l_ptr, &tunnel_hdr, m, |
2370 | msg_link_selector(m)); | |
b97bf3fd PL |
2371 | pos += align(msg_size(m)); |
2372 | m = (struct tipc_msg *)pos; | |
2373 | } | |
2374 | } else { | |
4323add6 PL |
2375 | tipc_link_tunnel(l_ptr, &tunnel_hdr, msg, |
2376 | msg_link_selector(msg)); | |
b97bf3fd PL |
2377 | } |
2378 | crs = crs->next; | |
2379 | } | |
2380 | } | |
2381 | ||
4323add6 | 2382 | void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel) |
b97bf3fd PL |
2383 | { |
2384 | struct sk_buff *iter; | |
2385 | struct tipc_msg tunnel_hdr; | |
2386 | ||
2387 | msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL, | |
2388 | DUPLICATE_MSG, TIPC_OK, INT_H_SIZE, l_ptr->addr); | |
2389 | msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size); | |
2390 | msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id); | |
2391 | iter = l_ptr->first_out; | |
2392 | while (iter) { | |
2393 | struct sk_buff *outbuf; | |
2394 | struct tipc_msg *msg = buf_msg(iter); | |
2395 | u32 length = msg_size(msg); | |
2396 | ||
2397 | if (msg_user(msg) == MSG_BUNDLER) | |
2398 | msg_set_type(msg, CLOSED_MSG); | |
2399 | msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */ | |
2400 | msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); | |
2401 | msg_set_size(&tunnel_hdr, length + INT_H_SIZE); | |
2402 | outbuf = buf_acquire(length + INT_H_SIZE); | |
2403 | if (outbuf == NULL) { | |
2404 | warn("Memory squeeze; buffer duplication failed\n"); | |
2405 | return; | |
2406 | } | |
2407 | memcpy(outbuf->data, (unchar *)&tunnel_hdr, INT_H_SIZE); | |
2408 | memcpy(outbuf->data + INT_H_SIZE, iter->data, length); | |
2409 | dbg("%c->%c:", l_ptr->b_ptr->net_plane, | |
2410 | tunnel->b_ptr->net_plane); | |
2411 | msg_dbg(buf_msg(outbuf), ">SEND>"); | |
4323add6 PL |
2412 | tipc_link_send_buf(tunnel, outbuf); |
2413 | if (!tipc_link_is_up(l_ptr)) | |
b97bf3fd PL |
2414 | return; |
2415 | iter = iter->next; | |
2416 | } | |
2417 | } | |
2418 | ||
2419 | ||
2420 | ||
2421 | /** | |
2422 | * buf_extract - extracts embedded TIPC message from another message | |
2423 | * @skb: encapsulating message buffer | |
2424 | * @from_pos: offset to extract from | |
2425 | * | |
2426 | * Returns a new message buffer containing an embedded message. The | |
2427 | * encapsulating message itself is left unchanged. | |
2428 | */ | |
2429 | ||
2430 | static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos) | |
2431 | { | |
2432 | struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos); | |
2433 | u32 size = msg_size(msg); | |
2434 | struct sk_buff *eb; | |
2435 | ||
2436 | eb = buf_acquire(size); | |
2437 | if (eb) | |
2438 | memcpy(eb->data, (unchar *)msg, size); | |
2439 | return eb; | |
2440 | } | |
2441 | ||
2442 | /* | |
2443 | * link_recv_changeover_msg(): Receive tunneled packet sent | |
2444 | * via other link. Node is locked. Return extracted buffer. | |
2445 | */ | |
2446 | ||
2447 | static int link_recv_changeover_msg(struct link **l_ptr, | |
2448 | struct sk_buff **buf) | |
2449 | { | |
2450 | struct sk_buff *tunnel_buf = *buf; | |
2451 | struct link *dest_link; | |
2452 | struct tipc_msg *msg; | |
2453 | struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf); | |
2454 | u32 msg_typ = msg_type(tunnel_msg); | |
2455 | u32 msg_count = msg_msgcnt(tunnel_msg); | |
2456 | ||
2457 | dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)]; | |
2458 | assert(dest_link != *l_ptr); | |
2459 | if (!dest_link) { | |
2460 | msg_dbg(tunnel_msg, "NOLINK/<REC<"); | |
2461 | goto exit; | |
2462 | } | |
2463 | dbg("%c<-%c:", dest_link->b_ptr->net_plane, | |
2464 | (*l_ptr)->b_ptr->net_plane); | |
2465 | *l_ptr = dest_link; | |
2466 | msg = msg_get_wrapped(tunnel_msg); | |
2467 | ||
2468 | if (msg_typ == DUPLICATE_MSG) { | |
2469 | if (less(msg_seqno(msg), mod(dest_link->next_in_no))) { | |
2470 | msg_dbg(tunnel_msg, "DROP/<REC<"); | |
2471 | goto exit; | |
2472 | } | |
2473 | *buf = buf_extract(tunnel_buf,INT_H_SIZE); | |
2474 | if (*buf == NULL) { | |
2475 | warn("Memory squeeze; failed to extract msg\n"); | |
2476 | goto exit; | |
2477 | } | |
2478 | msg_dbg(tunnel_msg, "TNL<REC<"); | |
2479 | buf_discard(tunnel_buf); | |
2480 | return 1; | |
2481 | } | |
2482 | ||
2483 | /* First original message ?: */ | |
2484 | ||
4323add6 | 2485 | if (tipc_link_is_up(dest_link)) { |
b97bf3fd | 2486 | msg_dbg(tunnel_msg, "UP/FIRST/<REC<"); |
4323add6 | 2487 | tipc_link_reset(dest_link); |
b97bf3fd PL |
2488 | dest_link->exp_msg_count = msg_count; |
2489 | if (!msg_count) | |
2490 | goto exit; | |
2491 | } else if (dest_link->exp_msg_count == START_CHANGEOVER) { | |
2492 | msg_dbg(tunnel_msg, "BLK/FIRST/<REC<"); | |
2493 | dest_link->exp_msg_count = msg_count; | |
2494 | if (!msg_count) | |
2495 | goto exit; | |
2496 | } | |
2497 | ||
2498 | /* Receive original message */ | |
2499 | ||
2500 | if (dest_link->exp_msg_count == 0) { | |
2501 | msg_dbg(tunnel_msg, "OVERDUE/DROP/<REC<"); | |
2502 | dbg_print_link(dest_link, "LINK:"); | |
2503 | goto exit; | |
2504 | } | |
2505 | dest_link->exp_msg_count--; | |
2506 | if (less(msg_seqno(msg), dest_link->reset_checkpoint)) { | |
2507 | msg_dbg(tunnel_msg, "DROP/DUPL/<REC<"); | |
2508 | goto exit; | |
2509 | } else { | |
2510 | *buf = buf_extract(tunnel_buf, INT_H_SIZE); | |
2511 | if (*buf != NULL) { | |
2512 | msg_dbg(tunnel_msg, "TNL<REC<"); | |
2513 | buf_discard(tunnel_buf); | |
2514 | return 1; | |
2515 | } else { | |
2516 | warn("Memory squeeze; dropped incoming msg\n"); | |
2517 | } | |
2518 | } | |
2519 | exit: | |
1fc54d8f | 2520 | *buf = NULL; |
b97bf3fd PL |
2521 | buf_discard(tunnel_buf); |
2522 | return 0; | |
2523 | } | |
2524 | ||
2525 | /* | |
2526 | * Bundler functionality: | |
2527 | */ | |
4323add6 | 2528 | void tipc_link_recv_bundle(struct sk_buff *buf) |
b97bf3fd PL |
2529 | { |
2530 | u32 msgcount = msg_msgcnt(buf_msg(buf)); | |
2531 | u32 pos = INT_H_SIZE; | |
2532 | struct sk_buff *obuf; | |
2533 | ||
2534 | msg_dbg(buf_msg(buf), "<BNDL<: "); | |
2535 | while (msgcount--) { | |
2536 | obuf = buf_extract(buf, pos); | |
2537 | if (obuf == NULL) { | |
2538 | char addr_string[16]; | |
2539 | ||
2540 | warn("Buffer allocation failure;\n"); | |
2541 | warn(" incoming message(s) from %s lost\n", | |
2542 | addr_string_fill(addr_string, | |
2543 | msg_orignode(buf_msg(buf)))); | |
2544 | return; | |
2545 | }; | |
2546 | pos += align(msg_size(buf_msg(obuf))); | |
2547 | msg_dbg(buf_msg(obuf), " /"); | |
4323add6 | 2548 | tipc_net_route_msg(obuf); |
b97bf3fd PL |
2549 | } |
2550 | buf_discard(buf); | |
2551 | } | |
2552 | ||
2553 | /* | |
2554 | * Fragmentation/defragmentation: | |
2555 | */ | |
2556 | ||
2557 | ||
2558 | /* | |
4323add6 | 2559 | * tipc_link_send_long_buf: Entry for buffers needing fragmentation. |
b97bf3fd PL |
2560 | * The buffer is complete, inclusive total message length. |
2561 | * Returns user data length. | |
2562 | */ | |
4323add6 | 2563 | int tipc_link_send_long_buf(struct link *l_ptr, struct sk_buff *buf) |
b97bf3fd PL |
2564 | { |
2565 | struct tipc_msg *inmsg = buf_msg(buf); | |
2566 | struct tipc_msg fragm_hdr; | |
2567 | u32 insize = msg_size(inmsg); | |
2568 | u32 dsz = msg_data_sz(inmsg); | |
2569 | unchar *crs = buf->data; | |
2570 | u32 rest = insize; | |
2571 | u32 pack_sz = link_max_pkt(l_ptr); | |
2572 | u32 fragm_sz = pack_sz - INT_H_SIZE; | |
2573 | u32 fragm_no = 1; | |
2574 | u32 destaddr = msg_destnode(inmsg); | |
2575 | ||
2576 | if (msg_short(inmsg)) | |
2577 | destaddr = l_ptr->addr; | |
2578 | ||
2579 | if (msg_routed(inmsg)) | |
2580 | msg_set_prevnode(inmsg, tipc_own_addr); | |
2581 | ||
2582 | /* Prepare reusable fragment header: */ | |
2583 | ||
2584 | msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT, | |
2585 | TIPC_OK, INT_H_SIZE, destaddr); | |
2586 | msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg)); | |
2587 | msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++)); | |
2588 | msg_set_fragm_no(&fragm_hdr, fragm_no); | |
2589 | l_ptr->stats.sent_fragmented++; | |
2590 | ||
2591 | /* Chop up message: */ | |
2592 | ||
2593 | while (rest > 0) { | |
2594 | struct sk_buff *fragm; | |
2595 | ||
2596 | if (rest <= fragm_sz) { | |
2597 | fragm_sz = rest; | |
2598 | msg_set_type(&fragm_hdr, LAST_FRAGMENT); | |
2599 | } | |
2600 | fragm = buf_acquire(fragm_sz + INT_H_SIZE); | |
2601 | if (fragm == NULL) { | |
2602 | warn("Memory squeeze; failed to fragment msg\n"); | |
2603 | dsz = -ENOMEM; | |
2604 | goto exit; | |
2605 | } | |
2606 | msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE); | |
2607 | memcpy(fragm->data, (unchar *)&fragm_hdr, INT_H_SIZE); | |
2608 | memcpy(fragm->data + INT_H_SIZE, crs, fragm_sz); | |
2609 | ||
2610 | /* Send queued messages first, if any: */ | |
2611 | ||
2612 | l_ptr->stats.sent_fragments++; | |
4323add6 PL |
2613 | tipc_link_send_buf(l_ptr, fragm); |
2614 | if (!tipc_link_is_up(l_ptr)) | |
b97bf3fd PL |
2615 | return dsz; |
2616 | msg_set_fragm_no(&fragm_hdr, ++fragm_no); | |
2617 | rest -= fragm_sz; | |
2618 | crs += fragm_sz; | |
2619 | msg_set_type(&fragm_hdr, FRAGMENT); | |
2620 | } | |
2621 | exit: | |
2622 | buf_discard(buf); | |
2623 | return dsz; | |
2624 | } | |
2625 | ||
2626 | /* | |
2627 | * A pending message being re-assembled must store certain values | |
2628 | * to handle subsequent fragments correctly. The following functions | |
2629 | * help storing these values in unused, available fields in the | |
2630 | * pending message. This makes dynamic memory allocation unecessary. | |
2631 | */ | |
2632 | ||
05790c64 | 2633 | static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno) |
b97bf3fd PL |
2634 | { |
2635 | msg_set_seqno(buf_msg(buf), seqno); | |
2636 | } | |
2637 | ||
05790c64 | 2638 | static u32 get_fragm_size(struct sk_buff *buf) |
b97bf3fd PL |
2639 | { |
2640 | return msg_ack(buf_msg(buf)); | |
2641 | } | |
2642 | ||
05790c64 | 2643 | static void set_fragm_size(struct sk_buff *buf, u32 sz) |
b97bf3fd PL |
2644 | { |
2645 | msg_set_ack(buf_msg(buf), sz); | |
2646 | } | |
2647 | ||
05790c64 | 2648 | static u32 get_expected_frags(struct sk_buff *buf) |
b97bf3fd PL |
2649 | { |
2650 | return msg_bcast_ack(buf_msg(buf)); | |
2651 | } | |
2652 | ||
05790c64 | 2653 | static void set_expected_frags(struct sk_buff *buf, u32 exp) |
b97bf3fd PL |
2654 | { |
2655 | msg_set_bcast_ack(buf_msg(buf), exp); | |
2656 | } | |
2657 | ||
05790c64 | 2658 | static u32 get_timer_cnt(struct sk_buff *buf) |
b97bf3fd PL |
2659 | { |
2660 | return msg_reroute_cnt(buf_msg(buf)); | |
2661 | } | |
2662 | ||
05790c64 | 2663 | static void incr_timer_cnt(struct sk_buff *buf) |
b97bf3fd PL |
2664 | { |
2665 | msg_incr_reroute_cnt(buf_msg(buf)); | |
2666 | } | |
2667 | ||
2668 | /* | |
4323add6 | 2669 | * tipc_link_recv_fragment(): Called with node lock on. Returns |
b97bf3fd PL |
2670 | * the reassembled buffer if message is complete. |
2671 | */ | |
4323add6 PL |
2672 | int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb, |
2673 | struct tipc_msg **m) | |
b97bf3fd | 2674 | { |
1fc54d8f | 2675 | struct sk_buff *prev = NULL; |
b97bf3fd PL |
2676 | struct sk_buff *fbuf = *fb; |
2677 | struct tipc_msg *fragm = buf_msg(fbuf); | |
2678 | struct sk_buff *pbuf = *pending; | |
2679 | u32 long_msg_seq_no = msg_long_msgno(fragm); | |
2680 | ||
1fc54d8f | 2681 | *fb = NULL; |
b97bf3fd PL |
2682 | msg_dbg(fragm,"FRG<REC<"); |
2683 | ||
2684 | /* Is there an incomplete message waiting for this fragment? */ | |
2685 | ||
2686 | while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) | |
2687 | || (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) { | |
2688 | prev = pbuf; | |
2689 | pbuf = pbuf->next; | |
2690 | } | |
2691 | ||
2692 | if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) { | |
2693 | struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm); | |
2694 | u32 msg_sz = msg_size(imsg); | |
2695 | u32 fragm_sz = msg_data_sz(fragm); | |
2696 | u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz); | |
2697 | u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE; | |
2698 | if (msg_type(imsg) == TIPC_MCAST_MSG) | |
2699 | max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE; | |
2700 | if (msg_size(imsg) > max) { | |
2701 | msg_dbg(fragm,"<REC<Oversized: "); | |
2702 | buf_discard(fbuf); | |
2703 | return 0; | |
2704 | } | |
2705 | pbuf = buf_acquire(msg_size(imsg)); | |
2706 | if (pbuf != NULL) { | |
2707 | pbuf->next = *pending; | |
2708 | *pending = pbuf; | |
2709 | memcpy(pbuf->data, (unchar *)imsg, msg_data_sz(fragm)); | |
2710 | ||
2711 | /* Prepare buffer for subsequent fragments. */ | |
2712 | ||
2713 | set_long_msg_seqno(pbuf, long_msg_seq_no); | |
2714 | set_fragm_size(pbuf,fragm_sz); | |
2715 | set_expected_frags(pbuf,exp_fragm_cnt - 1); | |
2716 | } else { | |
2717 | warn("Memory squeeze; got no defragmenting buffer\n"); | |
2718 | } | |
2719 | buf_discard(fbuf); | |
2720 | return 0; | |
2721 | } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) { | |
2722 | u32 dsz = msg_data_sz(fragm); | |
2723 | u32 fsz = get_fragm_size(pbuf); | |
2724 | u32 crs = ((msg_fragm_no(fragm) - 1) * fsz); | |
2725 | u32 exp_frags = get_expected_frags(pbuf) - 1; | |
2726 | memcpy(pbuf->data + crs, msg_data(fragm), dsz); | |
2727 | buf_discard(fbuf); | |
2728 | ||
2729 | /* Is message complete? */ | |
2730 | ||
2731 | if (exp_frags == 0) { | |
2732 | if (prev) | |
2733 | prev->next = pbuf->next; | |
2734 | else | |
2735 | *pending = pbuf->next; | |
2736 | msg_reset_reroute_cnt(buf_msg(pbuf)); | |
2737 | *fb = pbuf; | |
2738 | *m = buf_msg(pbuf); | |
2739 | return 1; | |
2740 | } | |
2741 | set_expected_frags(pbuf,exp_frags); | |
2742 | return 0; | |
2743 | } | |
2744 | dbg(" Discarding orphan fragment %x\n",fbuf); | |
2745 | msg_dbg(fragm,"ORPHAN:"); | |
2746 | dbg("Pending long buffers:\n"); | |
2747 | dbg_print_buf_chain(*pending); | |
2748 | buf_discard(fbuf); | |
2749 | return 0; | |
2750 | } | |
2751 | ||
2752 | /** | |
2753 | * link_check_defragm_bufs - flush stale incoming message fragments | |
2754 | * @l_ptr: pointer to link | |
2755 | */ | |
2756 | ||
2757 | static void link_check_defragm_bufs(struct link *l_ptr) | |
2758 | { | |
1fc54d8f SR |
2759 | struct sk_buff *prev = NULL; |
2760 | struct sk_buff *next = NULL; | |
b97bf3fd PL |
2761 | struct sk_buff *buf = l_ptr->defragm_buf; |
2762 | ||
2763 | if (!buf) | |
2764 | return; | |
2765 | if (!link_working_working(l_ptr)) | |
2766 | return; | |
2767 | while (buf) { | |
2768 | u32 cnt = get_timer_cnt(buf); | |
2769 | ||
2770 | next = buf->next; | |
2771 | if (cnt < 4) { | |
2772 | incr_timer_cnt(buf); | |
2773 | prev = buf; | |
2774 | } else { | |
2775 | dbg(" Discarding incomplete long buffer\n"); | |
2776 | msg_dbg(buf_msg(buf), "LONG:"); | |
2777 | dbg_print_link(l_ptr, "curr:"); | |
2778 | dbg("Pending long buffers:\n"); | |
2779 | dbg_print_buf_chain(l_ptr->defragm_buf); | |
2780 | if (prev) | |
2781 | prev->next = buf->next; | |
2782 | else | |
2783 | l_ptr->defragm_buf = buf->next; | |
2784 | buf_discard(buf); | |
2785 | } | |
2786 | buf = next; | |
2787 | } | |
2788 | } | |
2789 | ||
2790 | ||
2791 | ||
2792 | static void link_set_supervision_props(struct link *l_ptr, u32 tolerance) | |
2793 | { | |
2794 | l_ptr->tolerance = tolerance; | |
2795 | l_ptr->continuity_interval = | |
2796 | ((tolerance / 4) > 500) ? 500 : tolerance / 4; | |
2797 | l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4); | |
2798 | } | |
2799 | ||
2800 | ||
4323add6 | 2801 | void tipc_link_set_queue_limits(struct link *l_ptr, u32 window) |
b97bf3fd PL |
2802 | { |
2803 | /* Data messages from this node, inclusive FIRST_FRAGM */ | |
2804 | l_ptr->queue_limit[DATA_LOW] = window; | |
2805 | l_ptr->queue_limit[DATA_MEDIUM] = (window / 3) * 4; | |
2806 | l_ptr->queue_limit[DATA_HIGH] = (window / 3) * 5; | |
2807 | l_ptr->queue_limit[DATA_CRITICAL] = (window / 3) * 6; | |
2808 | /* Transiting data messages,inclusive FIRST_FRAGM */ | |
2809 | l_ptr->queue_limit[DATA_LOW + 4] = 300; | |
2810 | l_ptr->queue_limit[DATA_MEDIUM + 4] = 600; | |
2811 | l_ptr->queue_limit[DATA_HIGH + 4] = 900; | |
2812 | l_ptr->queue_limit[DATA_CRITICAL + 4] = 1200; | |
2813 | l_ptr->queue_limit[CONN_MANAGER] = 1200; | |
2814 | l_ptr->queue_limit[ROUTE_DISTRIBUTOR] = 1200; | |
2815 | l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500; | |
2816 | l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000; | |
2817 | /* FRAGMENT and LAST_FRAGMENT packets */ | |
2818 | l_ptr->queue_limit[MSG_FRAGMENTER] = 4000; | |
2819 | } | |
2820 | ||
2821 | /** | |
2822 | * link_find_link - locate link by name | |
2823 | * @name - ptr to link name string | |
2824 | * @node - ptr to area to be filled with ptr to associated node | |
2825 | * | |
4323add6 | 2826 | * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted; |
b97bf3fd PL |
2827 | * this also prevents link deletion. |
2828 | * | |
2829 | * Returns pointer to link (or 0 if invalid link name). | |
2830 | */ | |
2831 | ||
2832 | static struct link *link_find_link(const char *name, struct node **node) | |
2833 | { | |
2834 | struct link_name link_name_parts; | |
2835 | struct bearer *b_ptr; | |
2836 | struct link *l_ptr; | |
2837 | ||
2838 | if (!link_name_validate(name, &link_name_parts)) | |
1fc54d8f | 2839 | return NULL; |
b97bf3fd | 2840 | |
4323add6 | 2841 | b_ptr = tipc_bearer_find_interface(link_name_parts.if_local); |
b97bf3fd | 2842 | if (!b_ptr) |
1fc54d8f | 2843 | return NULL; |
b97bf3fd | 2844 | |
4323add6 | 2845 | *node = tipc_node_find(link_name_parts.addr_peer); |
b97bf3fd | 2846 | if (!*node) |
1fc54d8f | 2847 | return NULL; |
b97bf3fd PL |
2848 | |
2849 | l_ptr = (*node)->links[b_ptr->identity]; | |
2850 | if (!l_ptr || strcmp(l_ptr->name, name)) | |
1fc54d8f | 2851 | return NULL; |
b97bf3fd PL |
2852 | |
2853 | return l_ptr; | |
2854 | } | |
2855 | ||
4323add6 PL |
2856 | struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space, |
2857 | u16 cmd) | |
b97bf3fd PL |
2858 | { |
2859 | struct tipc_link_config *args; | |
2860 | u32 new_value; | |
2861 | struct link *l_ptr; | |
2862 | struct node *node; | |
2863 | int res; | |
2864 | ||
2865 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG)) | |
4323add6 | 2866 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd PL |
2867 | |
2868 | args = (struct tipc_link_config *)TLV_DATA(req_tlv_area); | |
2869 | new_value = ntohl(args->value); | |
2870 | ||
4323add6 | 2871 | if (!strcmp(args->name, tipc_bclink_name)) { |
b97bf3fd | 2872 | if ((cmd == TIPC_CMD_SET_LINK_WINDOW) && |
4323add6 PL |
2873 | (tipc_bclink_set_queue_limits(new_value) == 0)) |
2874 | return tipc_cfg_reply_none(); | |
2875 | return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED | |
2876 | " (cannot change setting on broadcast link)"); | |
b97bf3fd PL |
2877 | } |
2878 | ||
4323add6 | 2879 | read_lock_bh(&tipc_net_lock); |
b97bf3fd PL |
2880 | l_ptr = link_find_link(args->name, &node); |
2881 | if (!l_ptr) { | |
4323add6 PL |
2882 | read_unlock_bh(&tipc_net_lock); |
2883 | return tipc_cfg_reply_error_string("link not found"); | |
b97bf3fd PL |
2884 | } |
2885 | ||
4323add6 | 2886 | tipc_node_lock(node); |
b97bf3fd PL |
2887 | res = -EINVAL; |
2888 | switch (cmd) { | |
2889 | case TIPC_CMD_SET_LINK_TOL: | |
2890 | if ((new_value >= TIPC_MIN_LINK_TOL) && | |
2891 | (new_value <= TIPC_MAX_LINK_TOL)) { | |
2892 | link_set_supervision_props(l_ptr, new_value); | |
4323add6 PL |
2893 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, |
2894 | 0, 0, new_value, 0, 0); | |
b97bf3fd PL |
2895 | res = TIPC_OK; |
2896 | } | |
2897 | break; | |
2898 | case TIPC_CMD_SET_LINK_PRI: | |
16cb4b33 PL |
2899 | if ((new_value >= TIPC_MIN_LINK_PRI) && |
2900 | (new_value <= TIPC_MAX_LINK_PRI)) { | |
b97bf3fd | 2901 | l_ptr->priority = new_value; |
4323add6 PL |
2902 | tipc_link_send_proto_msg(l_ptr, STATE_MSG, |
2903 | 0, 0, 0, new_value, 0); | |
b97bf3fd PL |
2904 | res = TIPC_OK; |
2905 | } | |
2906 | break; | |
2907 | case TIPC_CMD_SET_LINK_WINDOW: | |
2908 | if ((new_value >= TIPC_MIN_LINK_WIN) && | |
2909 | (new_value <= TIPC_MAX_LINK_WIN)) { | |
4323add6 | 2910 | tipc_link_set_queue_limits(l_ptr, new_value); |
b97bf3fd PL |
2911 | res = TIPC_OK; |
2912 | } | |
2913 | break; | |
2914 | } | |
4323add6 | 2915 | tipc_node_unlock(node); |
b97bf3fd | 2916 | |
4323add6 | 2917 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd | 2918 | if (res) |
4323add6 | 2919 | return tipc_cfg_reply_error_string("cannot change link setting"); |
b97bf3fd | 2920 | |
4323add6 | 2921 | return tipc_cfg_reply_none(); |
b97bf3fd PL |
2922 | } |
2923 | ||
2924 | /** | |
2925 | * link_reset_statistics - reset link statistics | |
2926 | * @l_ptr: pointer to link | |
2927 | */ | |
2928 | ||
2929 | static void link_reset_statistics(struct link *l_ptr) | |
2930 | { | |
2931 | memset(&l_ptr->stats, 0, sizeof(l_ptr->stats)); | |
2932 | l_ptr->stats.sent_info = l_ptr->next_out_no; | |
2933 | l_ptr->stats.recv_info = l_ptr->next_in_no; | |
2934 | } | |
2935 | ||
4323add6 | 2936 | struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
2937 | { |
2938 | char *link_name; | |
2939 | struct link *l_ptr; | |
2940 | struct node *node; | |
2941 | ||
2942 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME)) | |
4323add6 | 2943 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd PL |
2944 | |
2945 | link_name = (char *)TLV_DATA(req_tlv_area); | |
4323add6 PL |
2946 | if (!strcmp(link_name, tipc_bclink_name)) { |
2947 | if (tipc_bclink_reset_stats()) | |
2948 | return tipc_cfg_reply_error_string("link not found"); | |
2949 | return tipc_cfg_reply_none(); | |
b97bf3fd PL |
2950 | } |
2951 | ||
4323add6 | 2952 | read_lock_bh(&tipc_net_lock); |
b97bf3fd PL |
2953 | l_ptr = link_find_link(link_name, &node); |
2954 | if (!l_ptr) { | |
4323add6 PL |
2955 | read_unlock_bh(&tipc_net_lock); |
2956 | return tipc_cfg_reply_error_string("link not found"); | |
b97bf3fd PL |
2957 | } |
2958 | ||
4323add6 | 2959 | tipc_node_lock(node); |
b97bf3fd | 2960 | link_reset_statistics(l_ptr); |
4323add6 PL |
2961 | tipc_node_unlock(node); |
2962 | read_unlock_bh(&tipc_net_lock); | |
2963 | return tipc_cfg_reply_none(); | |
b97bf3fd PL |
2964 | } |
2965 | ||
2966 | /** | |
2967 | * percent - convert count to a percentage of total (rounding up or down) | |
2968 | */ | |
2969 | ||
2970 | static u32 percent(u32 count, u32 total) | |
2971 | { | |
2972 | return (count * 100 + (total / 2)) / total; | |
2973 | } | |
2974 | ||
2975 | /** | |
4323add6 | 2976 | * tipc_link_stats - print link statistics |
b97bf3fd PL |
2977 | * @name: link name |
2978 | * @buf: print buffer area | |
2979 | * @buf_size: size of print buffer area | |
2980 | * | |
2981 | * Returns length of print buffer data string (or 0 if error) | |
2982 | */ | |
2983 | ||
4323add6 | 2984 | static int tipc_link_stats(const char *name, char *buf, const u32 buf_size) |
b97bf3fd PL |
2985 | { |
2986 | struct print_buf pb; | |
2987 | struct link *l_ptr; | |
2988 | struct node *node; | |
2989 | char *status; | |
2990 | u32 profile_total = 0; | |
2991 | ||
4323add6 PL |
2992 | if (!strcmp(name, tipc_bclink_name)) |
2993 | return tipc_bclink_stats(buf, buf_size); | |
b97bf3fd | 2994 | |
4323add6 | 2995 | tipc_printbuf_init(&pb, buf, buf_size); |
b97bf3fd | 2996 | |
4323add6 | 2997 | read_lock_bh(&tipc_net_lock); |
b97bf3fd PL |
2998 | l_ptr = link_find_link(name, &node); |
2999 | if (!l_ptr) { | |
4323add6 | 3000 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
3001 | return 0; |
3002 | } | |
4323add6 | 3003 | tipc_node_lock(node); |
b97bf3fd | 3004 | |
4323add6 | 3005 | if (tipc_link_is_active(l_ptr)) |
b97bf3fd | 3006 | status = "ACTIVE"; |
4323add6 | 3007 | else if (tipc_link_is_up(l_ptr)) |
b97bf3fd PL |
3008 | status = "STANDBY"; |
3009 | else | |
3010 | status = "DEFUNCT"; | |
3011 | tipc_printf(&pb, "Link <%s>\n" | |
3012 | " %s MTU:%u Priority:%u Tolerance:%u ms" | |
3013 | " Window:%u packets\n", | |
3014 | l_ptr->name, status, link_max_pkt(l_ptr), | |
3015 | l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]); | |
3016 | tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n", | |
3017 | l_ptr->next_in_no - l_ptr->stats.recv_info, | |
3018 | l_ptr->stats.recv_fragments, | |
3019 | l_ptr->stats.recv_fragmented, | |
3020 | l_ptr->stats.recv_bundles, | |
3021 | l_ptr->stats.recv_bundled); | |
3022 | tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n", | |
3023 | l_ptr->next_out_no - l_ptr->stats.sent_info, | |
3024 | l_ptr->stats.sent_fragments, | |
3025 | l_ptr->stats.sent_fragmented, | |
3026 | l_ptr->stats.sent_bundles, | |
3027 | l_ptr->stats.sent_bundled); | |
3028 | profile_total = l_ptr->stats.msg_length_counts; | |
3029 | if (!profile_total) | |
3030 | profile_total = 1; | |
3031 | tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n" | |
3032 | " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% " | |
3033 | "-16354:%u%% -32768:%u%% -66000:%u%%\n", | |
3034 | l_ptr->stats.msg_length_counts, | |
3035 | l_ptr->stats.msg_lengths_total / profile_total, | |
3036 | percent(l_ptr->stats.msg_length_profile[0], profile_total), | |
3037 | percent(l_ptr->stats.msg_length_profile[1], profile_total), | |
3038 | percent(l_ptr->stats.msg_length_profile[2], profile_total), | |
3039 | percent(l_ptr->stats.msg_length_profile[3], profile_total), | |
3040 | percent(l_ptr->stats.msg_length_profile[4], profile_total), | |
3041 | percent(l_ptr->stats.msg_length_profile[5], profile_total), | |
3042 | percent(l_ptr->stats.msg_length_profile[6], profile_total)); | |
3043 | tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n", | |
3044 | l_ptr->stats.recv_states, | |
3045 | l_ptr->stats.recv_probes, | |
3046 | l_ptr->stats.recv_nacks, | |
3047 | l_ptr->stats.deferred_recv, | |
3048 | l_ptr->stats.duplicates); | |
3049 | tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n", | |
3050 | l_ptr->stats.sent_states, | |
3051 | l_ptr->stats.sent_probes, | |
3052 | l_ptr->stats.sent_nacks, | |
3053 | l_ptr->stats.sent_acks, | |
3054 | l_ptr->stats.retransmitted); | |
3055 | tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n", | |
3056 | l_ptr->stats.bearer_congs, | |
3057 | l_ptr->stats.link_congs, | |
3058 | l_ptr->stats.max_queue_sz, | |
3059 | l_ptr->stats.queue_sz_counts | |
3060 | ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts) | |
3061 | : 0); | |
3062 | ||
4323add6 PL |
3063 | tipc_node_unlock(node); |
3064 | read_unlock_bh(&tipc_net_lock); | |
3065 | return tipc_printbuf_validate(&pb); | |
b97bf3fd PL |
3066 | } |
3067 | ||
3068 | #define MAX_LINK_STATS_INFO 2000 | |
3069 | ||
4323add6 | 3070 | struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
3071 | { |
3072 | struct sk_buff *buf; | |
3073 | struct tlv_desc *rep_tlv; | |
3074 | int str_len; | |
3075 | ||
3076 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME)) | |
4323add6 | 3077 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 3078 | |
4323add6 | 3079 | buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO)); |
b97bf3fd PL |
3080 | if (!buf) |
3081 | return NULL; | |
3082 | ||
3083 | rep_tlv = (struct tlv_desc *)buf->data; | |
3084 | ||
4323add6 PL |
3085 | str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area), |
3086 | (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO); | |
b97bf3fd PL |
3087 | if (!str_len) { |
3088 | buf_discard(buf); | |
4323add6 | 3089 | return tipc_cfg_reply_error_string("link not found"); |
b97bf3fd PL |
3090 | } |
3091 | ||
3092 | skb_put(buf, TLV_SPACE(str_len)); | |
3093 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
3094 | ||
3095 | return buf; | |
3096 | } | |
3097 | ||
3098 | #if 0 | |
3099 | int link_control(const char *name, u32 op, u32 val) | |
3100 | { | |
3101 | int res = -EINVAL; | |
3102 | struct link *l_ptr; | |
3103 | u32 bearer_id; | |
3104 | struct node * node; | |
3105 | u32 a; | |
3106 | ||
3107 | a = link_name2addr(name, &bearer_id); | |
4323add6 PL |
3108 | read_lock_bh(&tipc_net_lock); |
3109 | node = tipc_node_find(a); | |
b97bf3fd | 3110 | if (node) { |
4323add6 | 3111 | tipc_node_lock(node); |
b97bf3fd PL |
3112 | l_ptr = node->links[bearer_id]; |
3113 | if (l_ptr) { | |
3114 | if (op == TIPC_REMOVE_LINK) { | |
3115 | struct bearer *b_ptr = l_ptr->b_ptr; | |
3116 | spin_lock_bh(&b_ptr->publ.lock); | |
4323add6 | 3117 | tipc_link_delete(l_ptr); |
b97bf3fd PL |
3118 | spin_unlock_bh(&b_ptr->publ.lock); |
3119 | } | |
3120 | if (op == TIPC_CMD_BLOCK_LINK) { | |
4323add6 | 3121 | tipc_link_reset(l_ptr); |
b97bf3fd PL |
3122 | l_ptr->blocked = 1; |
3123 | } | |
3124 | if (op == TIPC_CMD_UNBLOCK_LINK) { | |
3125 | l_ptr->blocked = 0; | |
3126 | } | |
3127 | res = TIPC_OK; | |
3128 | } | |
4323add6 | 3129 | tipc_node_unlock(node); |
b97bf3fd | 3130 | } |
4323add6 | 3131 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
3132 | return res; |
3133 | } | |
3134 | #endif | |
3135 | ||
3136 | /** | |
4323add6 | 3137 | * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination |
b97bf3fd PL |
3138 | * @dest: network address of destination node |
3139 | * @selector: used to select from set of active links | |
3140 | * | |
3141 | * If no active link can be found, uses default maximum packet size. | |
3142 | */ | |
3143 | ||
4323add6 | 3144 | u32 tipc_link_get_max_pkt(u32 dest, u32 selector) |
b97bf3fd PL |
3145 | { |
3146 | struct node *n_ptr; | |
3147 | struct link *l_ptr; | |
3148 | u32 res = MAX_PKT_DEFAULT; | |
3149 | ||
3150 | if (dest == tipc_own_addr) | |
3151 | return MAX_MSG_SIZE; | |
3152 | ||
4323add6 PL |
3153 | read_lock_bh(&tipc_net_lock); |
3154 | n_ptr = tipc_node_select(dest, selector); | |
b97bf3fd | 3155 | if (n_ptr) { |
4323add6 | 3156 | tipc_node_lock(n_ptr); |
b97bf3fd PL |
3157 | l_ptr = n_ptr->active_links[selector & 1]; |
3158 | if (l_ptr) | |
3159 | res = link_max_pkt(l_ptr); | |
4323add6 | 3160 | tipc_node_unlock(n_ptr); |
b97bf3fd | 3161 | } |
4323add6 | 3162 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
3163 | return res; |
3164 | } | |
3165 | ||
3166 | #if 0 | |
3167 | static void link_dump_rec_queue(struct link *l_ptr) | |
3168 | { | |
3169 | struct sk_buff *crs; | |
3170 | ||
3171 | if (!l_ptr->oldest_deferred_in) { | |
3172 | info("Reception queue empty\n"); | |
3173 | return; | |
3174 | } | |
3175 | info("Contents of Reception queue:\n"); | |
3176 | crs = l_ptr->oldest_deferred_in; | |
3177 | while (crs) { | |
3178 | if (crs->data == (void *)0x0000a3a3) { | |
3179 | info("buffer %x invalid\n", crs); | |
3180 | return; | |
3181 | } | |
3182 | msg_dbg(buf_msg(crs), "In rec queue: \n"); | |
3183 | crs = crs->next; | |
3184 | } | |
3185 | } | |
3186 | #endif | |
3187 | ||
3188 | static void link_dump_send_queue(struct link *l_ptr) | |
3189 | { | |
3190 | if (l_ptr->next_out) { | |
3191 | info("\nContents of unsent queue:\n"); | |
3192 | dbg_print_buf_chain(l_ptr->next_out); | |
3193 | } | |
3194 | info("\nContents of send queue:\n"); | |
3195 | if (l_ptr->first_out) { | |
3196 | dbg_print_buf_chain(l_ptr->first_out); | |
3197 | } | |
3198 | info("Empty send queue\n"); | |
3199 | } | |
3200 | ||
3201 | static void link_print(struct link *l_ptr, struct print_buf *buf, | |
3202 | const char *str) | |
3203 | { | |
3204 | tipc_printf(buf, str); | |
3205 | if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr)) | |
3206 | return; | |
3207 | tipc_printf(buf, "Link %x<%s>:", | |
3208 | l_ptr->addr, l_ptr->b_ptr->publ.name); | |
3209 | tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no)); | |
3210 | tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no)); | |
3211 | tipc_printf(buf, "SQUE"); | |
3212 | if (l_ptr->first_out) { | |
3213 | tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out))); | |
3214 | if (l_ptr->next_out) | |
3215 | tipc_printf(buf, "%u..", | |
3216 | msg_seqno(buf_msg(l_ptr->next_out))); | |
3217 | tipc_printf(buf, "%u]", | |
3218 | msg_seqno(buf_msg | |
3219 | (l_ptr->last_out)), l_ptr->out_queue_size); | |
3220 | if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) - | |
3221 | msg_seqno(buf_msg(l_ptr->first_out))) | |
3222 | != (l_ptr->out_queue_size - 1)) | |
3223 | || (l_ptr->last_out->next != 0)) { | |
3224 | tipc_printf(buf, "\nSend queue inconsistency\n"); | |
3225 | tipc_printf(buf, "first_out= %x ", l_ptr->first_out); | |
3226 | tipc_printf(buf, "next_out= %x ", l_ptr->next_out); | |
3227 | tipc_printf(buf, "last_out= %x ", l_ptr->last_out); | |
3228 | link_dump_send_queue(l_ptr); | |
3229 | } | |
3230 | } else | |
3231 | tipc_printf(buf, "[]"); | |
3232 | tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size); | |
3233 | if (l_ptr->oldest_deferred_in) { | |
3234 | u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in)); | |
3235 | u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in)); | |
3236 | tipc_printf(buf, ":RQUE[%u..%u]", o, n); | |
3237 | if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) { | |
3238 | tipc_printf(buf, ":RQSIZ(%u)", | |
3239 | l_ptr->deferred_inqueue_sz); | |
3240 | } | |
3241 | } | |
3242 | if (link_working_unknown(l_ptr)) | |
3243 | tipc_printf(buf, ":WU"); | |
3244 | if (link_reset_reset(l_ptr)) | |
3245 | tipc_printf(buf, ":RR"); | |
3246 | if (link_reset_unknown(l_ptr)) | |
3247 | tipc_printf(buf, ":RU"); | |
3248 | if (link_working_working(l_ptr)) | |
3249 | tipc_printf(buf, ":WW"); | |
3250 | tipc_printf(buf, "\n"); | |
3251 | } | |
3252 |