2 * net/tipc/link.c: TIPC link code
4 * Copyright (c) 1996-2007, 2012-2014, Ericsson AB
5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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
34 * POSSIBILITY OF SUCH DAMAGE.
41 #include "name_distr.h"
45 #include <linux/pkt_sched.h>
48 * Error message prefixes
50 static const char *link_co_err = "Link changeover error, ";
51 static const char *link_rst_msg = "Resetting link ";
52 static const char *link_unk_evt = "Unknown link event ";
55 * Out-of-range value for link session numbers
57 #define INVALID_SESSION 0x10000
62 #define STARTING_EVT 856384768 /* link processing trigger */
63 #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
64 #define TIMEOUT_EVT 560817u /* link timer expired */
67 * The following two 'message types' is really just implementation
68 * data conveniently stored in the message header.
69 * They must not be considered part of the protocol
75 * State value stored in 'exp_msg_count'
77 #define START_CHANGEOVER 100000u
79 static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
81 static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf);
82 static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
83 struct sk_buff **buf);
84 static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
85 static void link_state_event(struct tipc_link *l_ptr, u32 event);
86 static void link_reset_statistics(struct tipc_link *l_ptr);
87 static void link_print(struct tipc_link *l_ptr, const char *str);
88 static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf);
89 static void tipc_link_sync_xmit(struct tipc_link *l);
90 static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
93 * Simple link routines
95 static unsigned int align(unsigned int i)
100 static void link_init_max_pkt(struct tipc_link *l_ptr)
102 struct tipc_bearer *b_ptr;
106 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
111 max_pkt = (b_ptr->mtu & ~3);
114 if (max_pkt > MAX_MSG_SIZE)
115 max_pkt = MAX_MSG_SIZE;
117 l_ptr->max_pkt_target = max_pkt;
118 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
119 l_ptr->max_pkt = l_ptr->max_pkt_target;
121 l_ptr->max_pkt = MAX_PKT_DEFAULT;
123 l_ptr->max_pkt_probes = 0;
126 static u32 link_next_sent(struct tipc_link *l_ptr)
129 return buf_seqno(l_ptr->next_out);
130 return mod(l_ptr->next_out_no);
133 static u32 link_last_sent(struct tipc_link *l_ptr)
135 return mod(link_next_sent(l_ptr) - 1);
139 * Simple non-static link routines (i.e. referenced outside this file)
141 int tipc_link_is_up(struct tipc_link *l_ptr)
145 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
148 int tipc_link_is_active(struct tipc_link *l_ptr)
150 return (l_ptr->owner->active_links[0] == l_ptr) ||
151 (l_ptr->owner->active_links[1] == l_ptr);
155 * link_timeout - handle expiration of link timer
156 * @l_ptr: pointer to link
158 static void link_timeout(struct tipc_link *l_ptr)
160 tipc_node_lock(l_ptr->owner);
162 /* update counters used in statistical profiling of send traffic */
163 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
164 l_ptr->stats.queue_sz_counts++;
166 if (l_ptr->first_out) {
167 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
168 u32 length = msg_size(msg);
170 if ((msg_user(msg) == MSG_FRAGMENTER) &&
171 (msg_type(msg) == FIRST_FRAGMENT)) {
172 length = msg_size(msg_get_wrapped(msg));
175 l_ptr->stats.msg_lengths_total += length;
176 l_ptr->stats.msg_length_counts++;
178 l_ptr->stats.msg_length_profile[0]++;
179 else if (length <= 256)
180 l_ptr->stats.msg_length_profile[1]++;
181 else if (length <= 1024)
182 l_ptr->stats.msg_length_profile[2]++;
183 else if (length <= 4096)
184 l_ptr->stats.msg_length_profile[3]++;
185 else if (length <= 16384)
186 l_ptr->stats.msg_length_profile[4]++;
187 else if (length <= 32768)
188 l_ptr->stats.msg_length_profile[5]++;
190 l_ptr->stats.msg_length_profile[6]++;
194 /* do all other link processing performed on a periodic basis */
196 link_state_event(l_ptr, TIMEOUT_EVT);
199 tipc_link_push_queue(l_ptr);
201 tipc_node_unlock(l_ptr->owner);
204 static void link_set_timer(struct tipc_link *l_ptr, u32 time)
206 k_start_timer(&l_ptr->timer, time);
210 * tipc_link_create - create a new link
211 * @n_ptr: pointer to associated node
212 * @b_ptr: pointer to associated bearer
213 * @media_addr: media address to use when sending messages over link
215 * Returns pointer to link.
217 struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
218 struct tipc_bearer *b_ptr,
219 const struct tipc_media_addr *media_addr)
221 struct tipc_link *l_ptr;
222 struct tipc_msg *msg;
224 char addr_string[16];
225 u32 peer = n_ptr->addr;
227 if (n_ptr->link_cnt >= 2) {
228 tipc_addr_string_fill(addr_string, n_ptr->addr);
229 pr_err("Attempt to establish third link to %s\n", addr_string);
233 if (n_ptr->links[b_ptr->identity]) {
234 tipc_addr_string_fill(addr_string, n_ptr->addr);
235 pr_err("Attempt to establish second link on <%s> to %s\n",
236 b_ptr->name, addr_string);
240 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
242 pr_warn("Link creation failed, no memory\n");
247 if_name = strchr(b_ptr->name, ':') + 1;
248 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
249 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
250 tipc_node(tipc_own_addr),
252 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
253 /* note: peer i/f name is updated by reset/activate message */
254 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
255 l_ptr->owner = n_ptr;
256 l_ptr->checkpoint = 1;
257 l_ptr->peer_session = INVALID_SESSION;
258 l_ptr->bearer_id = b_ptr->identity;
259 link_set_supervision_props(l_ptr, b_ptr->tolerance);
260 l_ptr->state = RESET_UNKNOWN;
262 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
264 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
265 msg_set_size(msg, sizeof(l_ptr->proto_msg));
266 msg_set_session(msg, (tipc_random & 0xffff));
267 msg_set_bearer_id(msg, b_ptr->identity);
268 strcpy((char *)msg_data(msg), if_name);
270 l_ptr->priority = b_ptr->priority;
271 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
273 l_ptr->net_plane = b_ptr->net_plane;
274 link_init_max_pkt(l_ptr);
276 l_ptr->next_out_no = 1;
277 INIT_LIST_HEAD(&l_ptr->waiting_ports);
279 link_reset_statistics(l_ptr);
281 tipc_node_attach_link(n_ptr, l_ptr);
283 k_init_timer(&l_ptr->timer, (Handler)link_timeout,
284 (unsigned long)l_ptr);
286 link_state_event(l_ptr, STARTING_EVT);
291 void tipc_link_delete_list(unsigned int bearer_id, bool shutting_down)
293 struct tipc_link *l_ptr;
294 struct tipc_node *n_ptr;
297 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
298 tipc_node_lock(n_ptr);
299 l_ptr = n_ptr->links[bearer_id];
301 tipc_link_reset(l_ptr);
302 if (shutting_down || !tipc_node_is_up(n_ptr)) {
303 tipc_node_detach_link(l_ptr->owner, l_ptr);
304 tipc_link_reset_fragments(l_ptr);
305 tipc_node_unlock(n_ptr);
307 /* Nobody else can access this link now: */
308 del_timer_sync(&l_ptr->timer);
311 /* Detach/delete when failover is finished: */
312 l_ptr->flags |= LINK_STOPPED;
313 tipc_node_unlock(n_ptr);
314 del_timer_sync(&l_ptr->timer);
318 tipc_node_unlock(n_ptr);
324 * link_schedule_port - schedule port for deferred sending
325 * @l_ptr: pointer to link
326 * @origport: reference to sending port
327 * @sz: amount of data to be sent
329 * Schedules port for renewed sending of messages after link congestion
332 static int link_schedule_port(struct tipc_link *l_ptr, u32 origport, u32 sz)
334 struct tipc_port *p_ptr;
335 struct tipc_sock *tsk;
337 spin_lock_bh(&tipc_port_list_lock);
338 p_ptr = tipc_port_lock(origport);
340 if (!list_empty(&p_ptr->wait_list))
342 tsk = tipc_port_to_sock(p_ptr);
344 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
345 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
346 l_ptr->stats.link_congs++;
348 tipc_port_unlock(p_ptr);
350 spin_unlock_bh(&tipc_port_list_lock);
354 void tipc_link_wakeup_ports(struct tipc_link *l_ptr, int all)
356 struct tipc_port *p_ptr;
357 struct tipc_sock *tsk;
358 struct tipc_port *temp_p_ptr;
359 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
365 if (!spin_trylock_bh(&tipc_port_list_lock))
367 if (link_congested(l_ptr))
369 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
373 tsk = tipc_port_to_sock(p_ptr);
374 list_del_init(&p_ptr->wait_list);
375 spin_lock_bh(p_ptr->lock);
377 tipc_sock_wakeup(tsk);
378 win -= p_ptr->waiting_pkts;
379 spin_unlock_bh(p_ptr->lock);
383 spin_unlock_bh(&tipc_port_list_lock);
387 * link_release_outqueue - purge link's outbound message queue
388 * @l_ptr: pointer to link
390 static void link_release_outqueue(struct tipc_link *l_ptr)
392 kfree_skb_list(l_ptr->first_out);
393 l_ptr->first_out = NULL;
394 l_ptr->out_queue_size = 0;
398 * tipc_link_reset_fragments - purge link's inbound message fragments queue
399 * @l_ptr: pointer to link
401 void tipc_link_reset_fragments(struct tipc_link *l_ptr)
403 kfree_skb(l_ptr->reasm_buf);
404 l_ptr->reasm_buf = NULL;
408 * tipc_link_purge_queues - purge all pkt queues associated with link
409 * @l_ptr: pointer to link
411 void tipc_link_purge_queues(struct tipc_link *l_ptr)
413 kfree_skb_list(l_ptr->oldest_deferred_in);
414 kfree_skb_list(l_ptr->first_out);
415 tipc_link_reset_fragments(l_ptr);
416 kfree_skb(l_ptr->proto_msg_queue);
417 l_ptr->proto_msg_queue = NULL;
420 void tipc_link_reset(struct tipc_link *l_ptr)
422 u32 prev_state = l_ptr->state;
423 u32 checkpoint = l_ptr->next_in_no;
424 int was_active_link = tipc_link_is_active(l_ptr);
426 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
428 /* Link is down, accept any session */
429 l_ptr->peer_session = INVALID_SESSION;
431 /* Prepare for max packet size negotiation */
432 link_init_max_pkt(l_ptr);
434 l_ptr->state = RESET_UNKNOWN;
436 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
439 tipc_node_link_down(l_ptr->owner, l_ptr);
440 tipc_bearer_remove_dest(l_ptr->bearer_id, l_ptr->addr);
442 if (was_active_link && tipc_node_active_links(l_ptr->owner)) {
443 l_ptr->reset_checkpoint = checkpoint;
444 l_ptr->exp_msg_count = START_CHANGEOVER;
447 /* Clean up all queues: */
448 link_release_outqueue(l_ptr);
449 kfree_skb(l_ptr->proto_msg_queue);
450 l_ptr->proto_msg_queue = NULL;
451 kfree_skb_list(l_ptr->oldest_deferred_in);
452 if (!list_empty(&l_ptr->waiting_ports))
453 tipc_link_wakeup_ports(l_ptr, 1);
455 l_ptr->retransm_queue_head = 0;
456 l_ptr->retransm_queue_size = 0;
457 l_ptr->last_out = NULL;
458 l_ptr->first_out = NULL;
459 l_ptr->next_out = NULL;
460 l_ptr->unacked_window = 0;
461 l_ptr->checkpoint = 1;
462 l_ptr->next_out_no = 1;
463 l_ptr->deferred_inqueue_sz = 0;
464 l_ptr->oldest_deferred_in = NULL;
465 l_ptr->newest_deferred_in = NULL;
466 l_ptr->fsm_msg_cnt = 0;
467 l_ptr->stale_count = 0;
468 link_reset_statistics(l_ptr);
471 void tipc_link_reset_list(unsigned int bearer_id)
473 struct tipc_link *l_ptr;
474 struct tipc_node *n_ptr;
477 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
478 tipc_node_lock(n_ptr);
479 l_ptr = n_ptr->links[bearer_id];
481 tipc_link_reset(l_ptr);
482 tipc_node_unlock(n_ptr);
487 static void link_activate(struct tipc_link *l_ptr)
489 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
490 tipc_node_link_up(l_ptr->owner, l_ptr);
491 tipc_bearer_add_dest(l_ptr->bearer_id, l_ptr->addr);
495 * link_state_event - link finite state machine
496 * @l_ptr: pointer to link
497 * @event: state machine event to process
499 static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
501 struct tipc_link *other;
502 u32 cont_intv = l_ptr->continuity_interval;
504 if (l_ptr->flags & LINK_STOPPED)
507 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
508 return; /* Not yet. */
510 /* Check whether changeover is going on */
511 if (l_ptr->exp_msg_count) {
512 if (event == TIMEOUT_EVT)
513 link_set_timer(l_ptr, cont_intv);
517 switch (l_ptr->state) {
518 case WORKING_WORKING:
520 case TRAFFIC_MSG_EVT:
524 if (l_ptr->next_in_no != l_ptr->checkpoint) {
525 l_ptr->checkpoint = l_ptr->next_in_no;
526 if (tipc_bclink_acks_missing(l_ptr->owner)) {
527 tipc_link_proto_xmit(l_ptr, STATE_MSG,
529 l_ptr->fsm_msg_cnt++;
530 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
531 tipc_link_proto_xmit(l_ptr, STATE_MSG,
533 l_ptr->fsm_msg_cnt++;
535 link_set_timer(l_ptr, cont_intv);
538 l_ptr->state = WORKING_UNKNOWN;
539 l_ptr->fsm_msg_cnt = 0;
540 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
541 l_ptr->fsm_msg_cnt++;
542 link_set_timer(l_ptr, cont_intv / 4);
545 pr_info("%s<%s>, requested by peer\n", link_rst_msg,
547 tipc_link_reset(l_ptr);
548 l_ptr->state = RESET_RESET;
549 l_ptr->fsm_msg_cnt = 0;
550 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
552 l_ptr->fsm_msg_cnt++;
553 link_set_timer(l_ptr, cont_intv);
556 pr_err("%s%u in WW state\n", link_unk_evt, event);
559 case WORKING_UNKNOWN:
561 case TRAFFIC_MSG_EVT:
563 l_ptr->state = WORKING_WORKING;
564 l_ptr->fsm_msg_cnt = 0;
565 link_set_timer(l_ptr, cont_intv);
568 pr_info("%s<%s>, requested by peer while probing\n",
569 link_rst_msg, l_ptr->name);
570 tipc_link_reset(l_ptr);
571 l_ptr->state = RESET_RESET;
572 l_ptr->fsm_msg_cnt = 0;
573 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
575 l_ptr->fsm_msg_cnt++;
576 link_set_timer(l_ptr, cont_intv);
579 if (l_ptr->next_in_no != l_ptr->checkpoint) {
580 l_ptr->state = WORKING_WORKING;
581 l_ptr->fsm_msg_cnt = 0;
582 l_ptr->checkpoint = l_ptr->next_in_no;
583 if (tipc_bclink_acks_missing(l_ptr->owner)) {
584 tipc_link_proto_xmit(l_ptr, STATE_MSG,
586 l_ptr->fsm_msg_cnt++;
588 link_set_timer(l_ptr, cont_intv);
589 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
590 tipc_link_proto_xmit(l_ptr, STATE_MSG,
592 l_ptr->fsm_msg_cnt++;
593 link_set_timer(l_ptr, cont_intv / 4);
594 } else { /* Link has failed */
595 pr_warn("%s<%s>, peer not responding\n",
596 link_rst_msg, l_ptr->name);
597 tipc_link_reset(l_ptr);
598 l_ptr->state = RESET_UNKNOWN;
599 l_ptr->fsm_msg_cnt = 0;
600 tipc_link_proto_xmit(l_ptr, RESET_MSG,
602 l_ptr->fsm_msg_cnt++;
603 link_set_timer(l_ptr, cont_intv);
607 pr_err("%s%u in WU state\n", link_unk_evt, event);
612 case TRAFFIC_MSG_EVT:
615 other = l_ptr->owner->active_links[0];
616 if (other && link_working_unknown(other))
618 l_ptr->state = WORKING_WORKING;
619 l_ptr->fsm_msg_cnt = 0;
620 link_activate(l_ptr);
621 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
622 l_ptr->fsm_msg_cnt++;
623 if (l_ptr->owner->working_links == 1)
624 tipc_link_sync_xmit(l_ptr);
625 link_set_timer(l_ptr, cont_intv);
628 l_ptr->state = RESET_RESET;
629 l_ptr->fsm_msg_cnt = 0;
630 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
632 l_ptr->fsm_msg_cnt++;
633 link_set_timer(l_ptr, cont_intv);
636 l_ptr->flags |= LINK_STARTED;
639 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
640 l_ptr->fsm_msg_cnt++;
641 link_set_timer(l_ptr, cont_intv);
644 pr_err("%s%u in RU state\n", link_unk_evt, event);
649 case TRAFFIC_MSG_EVT:
651 other = l_ptr->owner->active_links[0];
652 if (other && link_working_unknown(other))
654 l_ptr->state = WORKING_WORKING;
655 l_ptr->fsm_msg_cnt = 0;
656 link_activate(l_ptr);
657 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
658 l_ptr->fsm_msg_cnt++;
659 if (l_ptr->owner->working_links == 1)
660 tipc_link_sync_xmit(l_ptr);
661 link_set_timer(l_ptr, cont_intv);
666 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
668 l_ptr->fsm_msg_cnt++;
669 link_set_timer(l_ptr, cont_intv);
672 pr_err("%s%u in RR state\n", link_unk_evt, event);
676 pr_err("Unknown link state %u/%u\n", l_ptr->state, event);
681 * link_bundle_buf(): Append contents of a buffer to
682 * the tail of an existing one.
684 static int link_bundle_buf(struct tipc_link *l_ptr, struct sk_buff *bundler,
687 struct tipc_msg *bundler_msg = buf_msg(bundler);
688 struct tipc_msg *msg = buf_msg(buf);
689 u32 size = msg_size(msg);
690 u32 bundle_size = msg_size(bundler_msg);
691 u32 to_pos = align(bundle_size);
692 u32 pad = to_pos - bundle_size;
694 if (msg_user(bundler_msg) != MSG_BUNDLER)
696 if (msg_type(bundler_msg) != OPEN_MSG)
698 if (skb_tailroom(bundler) < (pad + size))
700 if (l_ptr->max_pkt < (to_pos + size))
703 skb_put(bundler, pad + size);
704 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
705 msg_set_size(bundler_msg, to_pos + size);
706 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
708 l_ptr->stats.sent_bundled++;
712 static void link_add_to_outqueue(struct tipc_link *l_ptr,
714 struct tipc_msg *msg)
716 u32 ack = mod(l_ptr->next_in_no - 1);
717 u32 seqno = mod(l_ptr->next_out_no++);
719 msg_set_word(msg, 2, ((ack << 16) | seqno));
720 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
722 if (l_ptr->first_out) {
723 l_ptr->last_out->next = buf;
724 l_ptr->last_out = buf;
726 l_ptr->first_out = l_ptr->last_out = buf;
728 l_ptr->out_queue_size++;
729 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
730 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
733 static void link_add_chain_to_outqueue(struct tipc_link *l_ptr,
734 struct sk_buff *buf_chain,
738 struct tipc_msg *msg;
740 if (!l_ptr->next_out)
741 l_ptr->next_out = buf_chain;
744 buf_chain = buf_chain->next;
747 msg_set_long_msgno(msg, long_msgno);
748 link_add_to_outqueue(l_ptr, buf, msg);
753 * tipc_link_xmit() is the 'full path' for messages, called from
754 * inside TIPC when the 'fast path' in tipc_send_xmit
755 * has failed, and from link_send()
757 int __tipc_link_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
759 struct tipc_msg *msg = buf_msg(buf);
760 u32 size = msg_size(msg);
761 u32 dsz = msg_data_sz(msg);
762 u32 queue_size = l_ptr->out_queue_size;
763 u32 imp = tipc_msg_tot_importance(msg);
764 u32 queue_limit = l_ptr->queue_limit[imp];
765 u32 max_packet = l_ptr->max_pkt;
767 /* Match msg importance against queue limits: */
768 if (unlikely(queue_size >= queue_limit)) {
769 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
770 link_schedule_port(l_ptr, msg_origport(msg), size);
775 if (imp > CONN_MANAGER) {
776 pr_warn("%s<%s>, send queue full", link_rst_msg,
778 tipc_link_reset(l_ptr);
783 /* Fragmentation needed ? */
784 if (size > max_packet)
785 return tipc_link_frag_xmit(l_ptr, buf);
787 /* Packet can be queued or sent. */
788 if (likely(!link_congested(l_ptr))) {
789 link_add_to_outqueue(l_ptr, buf, msg);
791 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
792 l_ptr->unacked_window = 0;
795 /* Congestion: can message be bundled ? */
796 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
797 (msg_user(msg) != MSG_FRAGMENTER)) {
799 /* Try adding message to an existing bundle */
800 if (l_ptr->next_out &&
801 link_bundle_buf(l_ptr, l_ptr->last_out, buf))
804 /* Try creating a new bundle */
805 if (size <= max_packet * 2 / 3) {
806 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
807 struct tipc_msg bundler_hdr;
810 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
811 INT_H_SIZE, l_ptr->addr);
812 skb_copy_to_linear_data(bundler, &bundler_hdr,
814 skb_trim(bundler, INT_H_SIZE);
815 link_bundle_buf(l_ptr, bundler, buf);
818 l_ptr->stats.sent_bundles++;
822 if (!l_ptr->next_out)
823 l_ptr->next_out = buf;
824 link_add_to_outqueue(l_ptr, buf, msg);
829 * tipc_link_xmit(): same as __tipc_link_xmit(), but the link to use
830 * has not been selected yet, and the the owner node is not locked
831 * Called by TIPC internal users, e.g. the name distributor
833 int tipc_link_xmit(struct sk_buff *buf, u32 dest, u32 selector)
835 struct tipc_link *l_ptr;
836 struct tipc_node *n_ptr;
837 int res = -ELINKCONG;
839 n_ptr = tipc_node_find(dest);
841 tipc_node_lock(n_ptr);
842 l_ptr = n_ptr->active_links[selector & 1];
844 res = __tipc_link_xmit(l_ptr, buf);
847 tipc_node_unlock(n_ptr);
854 /* tipc_link_cong: determine return value and how to treat the
855 * sent buffer during link congestion.
856 * - For plain, errorless user data messages we keep the buffer and
858 * - For all other messages we discard the buffer and return -EHOSTUNREACH
859 * - For TIPC internal messages we also reset the link
861 static int tipc_link_cong(struct tipc_link *link, struct sk_buff *buf)
863 struct tipc_msg *msg = buf_msg(buf);
864 uint psz = msg_size(msg);
865 uint imp = tipc_msg_tot_importance(msg);
866 u32 oport = msg_tot_origport(msg);
868 if (likely(imp <= TIPC_CRITICAL_IMPORTANCE)) {
869 if (!msg_errcode(msg) && !msg_reroute_cnt(msg)) {
870 link_schedule_port(link, oport, psz);
874 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
875 tipc_link_reset(link);
878 return -EHOSTUNREACH;
882 * __tipc_link_xmit2(): same as tipc_link_xmit2, but destlink is known & locked
884 * @buf: chain of buffers containing message
885 * Consumes the buffer chain, except when returning -ELINKCONG
886 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket
887 * user data messages) or -EHOSTUNREACH (all other messages/senders)
888 * Only the socket functions tipc_send_stream() and tipc_send_packet() need
889 * to act on the return value, since they may need to do more send attempts.
891 int __tipc_link_xmit2(struct tipc_link *link, struct sk_buff *buf)
893 struct tipc_msg *msg = buf_msg(buf);
894 uint psz = msg_size(msg);
895 uint qsz = link->out_queue_size;
896 uint sndlim = link->queue_limit[0];
897 uint imp = tipc_msg_tot_importance(msg);
898 uint mtu = link->max_pkt;
899 uint ack = mod(link->next_in_no - 1);
900 uint seqno = link->next_out_no;
901 uint bc_last_in = link->owner->bclink.last_in;
902 struct tipc_media_addr *addr = &link->media_addr;
903 struct sk_buff *next = buf->next;
905 /* Match queue limits against msg importance: */
906 if (unlikely(qsz >= link->queue_limit[imp]))
907 return tipc_link_cong(link, buf);
909 /* Has valid packet limit been used ? */
910 if (unlikely(psz > mtu)) {
915 /* Prepare each packet for sending, and add to outqueue: */
919 msg_set_word(msg, 2, ((ack << 16) | mod(seqno)));
920 msg_set_bcast_ack(msg, bc_last_in);
922 if (!link->first_out) {
923 link->first_out = buf;
924 } else if (qsz < sndlim) {
925 link->last_out->next = buf;
926 } else if (tipc_msg_bundle(link->last_out, buf, mtu)) {
927 link->stats.sent_bundled++;
931 } else if (tipc_msg_make_bundle(&buf, mtu, link->addr)) {
932 link->stats.sent_bundled++;
933 link->stats.sent_bundles++;
934 link->last_out->next = buf;
936 link->next_out = buf;
938 link->last_out->next = buf;
940 link->next_out = buf;
943 /* Send packet if possible: */
944 if (likely(++qsz <= sndlim)) {
945 tipc_bearer_send(link->bearer_id, buf, addr);
946 link->next_out = next;
947 link->unacked_window = 0;
950 link->last_out = buf;
953 link->next_out_no = seqno;
954 link->out_queue_size = qsz;
959 * tipc_link_xmit2() is the general link level function for message sending
960 * @buf: chain of buffers containing message
961 * @dsz: amount of user data to be sent
962 * @dnode: address of destination node
963 * @selector: a number used for deterministic link selection
964 * Consumes the buffer chain, except when returning -ELINKCONG
965 * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
967 int tipc_link_xmit2(struct sk_buff *buf, u32 dnode, u32 selector)
969 struct tipc_link *link = NULL;
970 struct tipc_node *node;
971 int rc = -EHOSTUNREACH;
973 node = tipc_node_find(dnode);
975 tipc_node_lock(node);
976 link = node->active_links[selector & 1];
978 rc = __tipc_link_xmit2(link, buf);
979 tipc_node_unlock(node);
985 if (likely(in_own_node(dnode)))
986 return tipc_sk_rcv(buf);
993 * tipc_link_sync_xmit - synchronize broadcast link endpoints.
995 * Give a newly added peer node the sequence number where it should
996 * start receiving and acking broadcast packets.
998 * Called with node locked
1000 static void tipc_link_sync_xmit(struct tipc_link *l)
1002 struct sk_buff *buf;
1003 struct tipc_msg *msg;
1005 buf = tipc_buf_acquire(INT_H_SIZE);
1010 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG, INT_H_SIZE, l->addr);
1011 msg_set_last_bcast(msg, l->owner->bclink.acked);
1012 link_add_chain_to_outqueue(l, buf, 0);
1013 tipc_link_push_queue(l);
1017 * tipc_link_sync_rcv - synchronize broadcast link endpoints.
1018 * Receive the sequence number where we should start receiving and
1019 * acking broadcast packets from a newly added peer node, and open
1020 * up for reception of such packets.
1022 * Called with node locked
1024 static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
1026 struct tipc_msg *msg = buf_msg(buf);
1028 n->bclink.last_sent = n->bclink.last_in = msg_last_bcast(msg);
1029 n->bclink.recv_permitted = true;
1034 * tipc_link_names_xmit - send name table entries to new neighbor
1036 * Send routine for bulk delivery of name table messages when contact
1037 * with a new neighbor occurs. No link congestion checking is performed
1038 * because name table messages *must* be delivered. The messages must be
1039 * small enough not to require fragmentation.
1040 * Called without any locks held.
1042 void tipc_link_names_xmit(struct list_head *message_list, u32 dest)
1044 struct tipc_node *n_ptr;
1045 struct tipc_link *l_ptr;
1046 struct sk_buff *buf;
1047 struct sk_buff *temp_buf;
1049 if (list_empty(message_list))
1052 n_ptr = tipc_node_find(dest);
1054 tipc_node_lock(n_ptr);
1055 l_ptr = n_ptr->active_links[0];
1057 /* convert circular list to linear list */
1058 ((struct sk_buff *)message_list->prev)->next = NULL;
1059 link_add_chain_to_outqueue(l_ptr,
1060 (struct sk_buff *)message_list->next, 0);
1061 tipc_link_push_queue(l_ptr);
1062 INIT_LIST_HEAD(message_list);
1064 tipc_node_unlock(n_ptr);
1067 /* discard the messages if they couldn't be sent */
1068 list_for_each_safe(buf, temp_buf, ((struct sk_buff *)message_list)) {
1069 list_del((struct list_head *)buf);
1075 * tipc_link_push_packet: Push one unsent packet to the media
1077 static u32 tipc_link_push_packet(struct tipc_link *l_ptr)
1079 struct sk_buff *buf = l_ptr->first_out;
1080 u32 r_q_size = l_ptr->retransm_queue_size;
1081 u32 r_q_head = l_ptr->retransm_queue_head;
1083 /* Step to position where retransmission failed, if any, */
1084 /* consider that buffers may have been released in meantime */
1085 if (r_q_size && buf) {
1086 u32 last = lesser(mod(r_q_head + r_q_size),
1087 link_last_sent(l_ptr));
1088 u32 first = buf_seqno(buf);
1090 while (buf && less(first, r_q_head)) {
1091 first = mod(first + 1);
1094 l_ptr->retransm_queue_head = r_q_head = first;
1095 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1098 /* Continue retransmission now, if there is anything: */
1099 if (r_q_size && buf) {
1100 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1101 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
1102 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1103 l_ptr->retransm_queue_head = mod(++r_q_head);
1104 l_ptr->retransm_queue_size = --r_q_size;
1105 l_ptr->stats.retransmitted++;
1109 /* Send deferred protocol message, if any: */
1110 buf = l_ptr->proto_msg_queue;
1112 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1113 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
1114 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1115 l_ptr->unacked_window = 0;
1117 l_ptr->proto_msg_queue = NULL;
1121 /* Send one deferred data message, if send window not full: */
1122 buf = l_ptr->next_out;
1124 struct tipc_msg *msg = buf_msg(buf);
1125 u32 next = msg_seqno(msg);
1126 u32 first = buf_seqno(l_ptr->first_out);
1128 if (mod(next - first) < l_ptr->queue_limit[0]) {
1129 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1130 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1131 tipc_bearer_send(l_ptr->bearer_id, buf,
1132 &l_ptr->media_addr);
1133 if (msg_user(msg) == MSG_BUNDLER)
1134 msg_set_type(msg, BUNDLE_CLOSED);
1135 l_ptr->next_out = buf->next;
1143 * push_queue(): push out the unsent messages of a link where
1144 * congestion has abated. Node is locked
1146 void tipc_link_push_queue(struct tipc_link *l_ptr)
1151 res = tipc_link_push_packet(l_ptr);
1155 void tipc_link_reset_all(struct tipc_node *node)
1157 char addr_string[16];
1160 tipc_node_lock(node);
1162 pr_warn("Resetting all links to %s\n",
1163 tipc_addr_string_fill(addr_string, node->addr));
1165 for (i = 0; i < MAX_BEARERS; i++) {
1166 if (node->links[i]) {
1167 link_print(node->links[i], "Resetting link\n");
1168 tipc_link_reset(node->links[i]);
1172 tipc_node_unlock(node);
1175 static void link_retransmit_failure(struct tipc_link *l_ptr,
1176 struct sk_buff *buf)
1178 struct tipc_msg *msg = buf_msg(buf);
1180 pr_warn("Retransmission failure on link <%s>\n", l_ptr->name);
1183 /* Handle failure on standard link */
1184 link_print(l_ptr, "Resetting link\n");
1185 tipc_link_reset(l_ptr);
1188 /* Handle failure on broadcast link */
1189 struct tipc_node *n_ptr;
1190 char addr_string[16];
1192 pr_info("Msg seq number: %u, ", msg_seqno(msg));
1193 pr_cont("Outstanding acks: %lu\n",
1194 (unsigned long) TIPC_SKB_CB(buf)->handle);
1196 n_ptr = tipc_bclink_retransmit_to();
1197 tipc_node_lock(n_ptr);
1199 tipc_addr_string_fill(addr_string, n_ptr->addr);
1200 pr_info("Broadcast link info for %s\n", addr_string);
1201 pr_info("Reception permitted: %d, Acked: %u\n",
1202 n_ptr->bclink.recv_permitted,
1203 n_ptr->bclink.acked);
1204 pr_info("Last in: %u, Oos state: %u, Last sent: %u\n",
1205 n_ptr->bclink.last_in,
1206 n_ptr->bclink.oos_state,
1207 n_ptr->bclink.last_sent);
1209 tipc_node_unlock(n_ptr);
1211 tipc_bclink_set_flags(TIPC_BCLINK_RESET);
1212 l_ptr->stale_count = 0;
1216 void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *buf,
1219 struct tipc_msg *msg;
1226 /* Detect repeated retransmit failures */
1227 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1228 if (++l_ptr->stale_count > 100) {
1229 link_retransmit_failure(l_ptr, buf);
1233 l_ptr->last_retransmitted = msg_seqno(msg);
1234 l_ptr->stale_count = 1;
1237 while (retransmits && (buf != l_ptr->next_out) && buf) {
1239 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1240 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1241 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1244 l_ptr->stats.retransmitted++;
1247 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1251 * link_insert_deferred_queue - insert deferred messages back into receive chain
1253 static struct sk_buff *link_insert_deferred_queue(struct tipc_link *l_ptr,
1254 struct sk_buff *buf)
1258 if (l_ptr->oldest_deferred_in == NULL)
1261 seq_no = buf_seqno(l_ptr->oldest_deferred_in);
1262 if (seq_no == mod(l_ptr->next_in_no)) {
1263 l_ptr->newest_deferred_in->next = buf;
1264 buf = l_ptr->oldest_deferred_in;
1265 l_ptr->oldest_deferred_in = NULL;
1266 l_ptr->deferred_inqueue_sz = 0;
1272 * link_recv_buf_validate - validate basic format of received message
1274 * This routine ensures a TIPC message has an acceptable header, and at least
1275 * as much data as the header indicates it should. The routine also ensures
1276 * that the entire message header is stored in the main fragment of the message
1277 * buffer, to simplify future access to message header fields.
1279 * Note: Having extra info present in the message header or data areas is OK.
1280 * TIPC will ignore the excess, under the assumption that it is optional info
1281 * introduced by a later release of the protocol.
1283 static int link_recv_buf_validate(struct sk_buff *buf)
1285 static u32 min_data_hdr_size[8] = {
1286 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE,
1287 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1290 struct tipc_msg *msg;
1296 /* If this packet comes from the defer queue, the skb has already
1299 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1302 if (unlikely(buf->len < MIN_H_SIZE))
1305 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1309 if (unlikely(msg_version(msg) != TIPC_VERSION))
1312 size = msg_size(msg);
1313 hdr_size = msg_hdr_sz(msg);
1314 min_hdr_size = msg_isdata(msg) ?
1315 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1317 if (unlikely((hdr_size < min_hdr_size) ||
1318 (size < hdr_size) ||
1319 (buf->len < size) ||
1320 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1323 return pskb_may_pull(buf, hdr_size);
1327 * tipc_rcv - process TIPC packets/messages arriving from off-node
1328 * @head: pointer to message buffer chain
1329 * @b_ptr: pointer to bearer message arrived on
1331 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1332 * structure (i.e. cannot be NULL), but bearer can be inactive.
1334 void tipc_rcv(struct sk_buff *head, struct tipc_bearer *b_ptr)
1337 struct tipc_node *n_ptr;
1338 struct tipc_link *l_ptr;
1339 struct sk_buff *crs;
1340 struct sk_buff *buf = head;
1341 struct tipc_msg *msg;
1349 /* Ensure message is well-formed */
1350 if (unlikely(!link_recv_buf_validate(buf)))
1353 /* Ensure message data is a single contiguous unit */
1354 if (unlikely(skb_linearize(buf)))
1357 /* Handle arrival of a non-unicast link message */
1360 if (unlikely(msg_non_seq(msg))) {
1361 if (msg_user(msg) == LINK_CONFIG)
1362 tipc_disc_rcv(buf, b_ptr);
1364 tipc_bclink_rcv(buf);
1368 /* Discard unicast link messages destined for another node */
1369 if (unlikely(!msg_short(msg) &&
1370 (msg_destnode(msg) != tipc_own_addr)))
1373 /* Locate neighboring node that sent message */
1374 n_ptr = tipc_node_find(msg_prevnode(msg));
1375 if (unlikely(!n_ptr))
1377 tipc_node_lock(n_ptr);
1379 /* Locate unicast link endpoint that should handle message */
1380 l_ptr = n_ptr->links[b_ptr->identity];
1381 if (unlikely(!l_ptr))
1382 goto unlock_discard;
1384 /* Verify that communication with node is currently allowed */
1385 if ((n_ptr->action_flags & TIPC_WAIT_PEER_LINKS_DOWN) &&
1386 msg_user(msg) == LINK_PROTOCOL &&
1387 (msg_type(msg) == RESET_MSG ||
1388 msg_type(msg) == ACTIVATE_MSG) &&
1389 !msg_redundant_link(msg))
1390 n_ptr->action_flags &= ~TIPC_WAIT_PEER_LINKS_DOWN;
1392 if (tipc_node_blocked(n_ptr))
1393 goto unlock_discard;
1395 /* Validate message sequence number info */
1396 seq_no = msg_seqno(msg);
1397 ackd = msg_ack(msg);
1399 /* Release acked messages */
1400 if (n_ptr->bclink.recv_permitted)
1401 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
1403 crs = l_ptr->first_out;
1404 while ((crs != l_ptr->next_out) &&
1405 less_eq(buf_seqno(crs), ackd)) {
1406 struct sk_buff *next = crs->next;
1412 l_ptr->first_out = crs;
1413 l_ptr->out_queue_size -= released;
1416 /* Try sending any messages link endpoint has pending */
1417 if (unlikely(l_ptr->next_out))
1418 tipc_link_push_queue(l_ptr);
1420 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
1421 tipc_link_wakeup_ports(l_ptr, 0);
1423 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1424 l_ptr->stats.sent_acks++;
1425 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1428 /* Process the incoming packet */
1429 if (unlikely(!link_working_working(l_ptr))) {
1430 if (msg_user(msg) == LINK_PROTOCOL) {
1431 tipc_link_proto_rcv(l_ptr, buf);
1432 head = link_insert_deferred_queue(l_ptr, head);
1433 tipc_node_unlock(n_ptr);
1437 /* Traffic message. Conditionally activate link */
1438 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1440 if (link_working_working(l_ptr)) {
1441 /* Re-insert buffer in front of queue */
1444 tipc_node_unlock(n_ptr);
1447 goto unlock_discard;
1450 /* Link is now in state WORKING_WORKING */
1451 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
1452 link_handle_out_of_seq_msg(l_ptr, buf);
1453 head = link_insert_deferred_queue(l_ptr, head);
1454 tipc_node_unlock(n_ptr);
1457 l_ptr->next_in_no++;
1458 if (unlikely(l_ptr->oldest_deferred_in))
1459 head = link_insert_deferred_queue(l_ptr, head);
1461 /* Deliver packet/message to correct user: */
1462 if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL)) {
1463 if (!tipc_link_tunnel_rcv(n_ptr, &buf)) {
1464 tipc_node_unlock(n_ptr);
1468 } else if (msg_user(msg) == MSG_FRAGMENTER) {
1469 l_ptr->stats.recv_fragments++;
1470 if (tipc_buf_append(&l_ptr->reasm_buf, &buf)) {
1471 l_ptr->stats.recv_fragmented++;
1474 if (!l_ptr->reasm_buf)
1475 tipc_link_reset(l_ptr);
1476 tipc_node_unlock(n_ptr);
1481 switch (msg_user(msg)) {
1482 case TIPC_LOW_IMPORTANCE:
1483 case TIPC_MEDIUM_IMPORTANCE:
1484 case TIPC_HIGH_IMPORTANCE:
1485 case TIPC_CRITICAL_IMPORTANCE:
1487 tipc_node_unlock(n_ptr);
1491 l_ptr->stats.recv_bundles++;
1492 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1493 tipc_node_unlock(n_ptr);
1494 tipc_link_bundle_rcv(buf);
1496 case NAME_DISTRIBUTOR:
1497 n_ptr->bclink.recv_permitted = true;
1498 tipc_node_unlock(n_ptr);
1499 tipc_named_rcv(buf);
1501 case BCAST_PROTOCOL:
1502 tipc_link_sync_rcv(n_ptr, buf);
1508 tipc_node_unlock(n_ptr);
1511 tipc_node_unlock(n_ptr);
1518 * tipc_link_defer_pkt - Add out-of-sequence message to deferred reception queue
1520 * Returns increase in queue length (i.e. 0 or 1)
1522 u32 tipc_link_defer_pkt(struct sk_buff **head, struct sk_buff **tail,
1523 struct sk_buff *buf)
1525 struct sk_buff *queue_buf;
1526 struct sk_buff **prev;
1527 u32 seq_no = buf_seqno(buf);
1532 if (*head == NULL) {
1533 *head = *tail = buf;
1538 if (less(buf_seqno(*tail), seq_no)) {
1539 (*tail)->next = buf;
1544 /* Locate insertion point in queue, then insert; discard if duplicate */
1548 u32 curr_seqno = buf_seqno(queue_buf);
1550 if (seq_no == curr_seqno) {
1555 if (less(seq_no, curr_seqno))
1558 prev = &queue_buf->next;
1559 queue_buf = queue_buf->next;
1562 buf->next = queue_buf;
1568 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1570 static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
1571 struct sk_buff *buf)
1573 u32 seq_no = buf_seqno(buf);
1575 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1576 tipc_link_proto_rcv(l_ptr, buf);
1580 /* Record OOS packet arrival (force mismatch on next timeout) */
1581 l_ptr->checkpoint--;
1584 * Discard packet if a duplicate; otherwise add it to deferred queue
1585 * and notify peer of gap as per protocol specification
1587 if (less(seq_no, mod(l_ptr->next_in_no))) {
1588 l_ptr->stats.duplicates++;
1593 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1594 &l_ptr->newest_deferred_in, buf)) {
1595 l_ptr->deferred_inqueue_sz++;
1596 l_ptr->stats.deferred_recv++;
1597 TIPC_SKB_CB(buf)->deferred = true;
1598 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
1599 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1601 l_ptr->stats.duplicates++;
1605 * Send protocol message to the other endpoint.
1607 void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1608 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
1610 struct sk_buff *buf = NULL;
1611 struct tipc_msg *msg = l_ptr->pmsg;
1612 u32 msg_size = sizeof(l_ptr->proto_msg);
1615 /* Discard any previous message that was deferred due to congestion */
1616 if (l_ptr->proto_msg_queue) {
1617 kfree_skb(l_ptr->proto_msg_queue);
1618 l_ptr->proto_msg_queue = NULL;
1621 /* Don't send protocol message during link changeover */
1622 if (l_ptr->exp_msg_count)
1625 /* Abort non-RESET send if communication with node is prohibited */
1626 if ((tipc_node_blocked(l_ptr->owner)) && (msg_typ != RESET_MSG))
1629 /* Create protocol message with "out-of-sequence" sequence number */
1630 msg_set_type(msg, msg_typ);
1631 msg_set_net_plane(msg, l_ptr->net_plane);
1632 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1633 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
1635 if (msg_typ == STATE_MSG) {
1636 u32 next_sent = mod(l_ptr->next_out_no);
1638 if (!tipc_link_is_up(l_ptr))
1640 if (l_ptr->next_out)
1641 next_sent = buf_seqno(l_ptr->next_out);
1642 msg_set_next_sent(msg, next_sent);
1643 if (l_ptr->oldest_deferred_in) {
1644 u32 rec = buf_seqno(l_ptr->oldest_deferred_in);
1645 gap = mod(rec - mod(l_ptr->next_in_no));
1647 msg_set_seq_gap(msg, gap);
1649 l_ptr->stats.sent_nacks++;
1650 msg_set_link_tolerance(msg, tolerance);
1651 msg_set_linkprio(msg, priority);
1652 msg_set_max_pkt(msg, ack_mtu);
1653 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1654 msg_set_probe(msg, probe_msg != 0);
1656 u32 mtu = l_ptr->max_pkt;
1658 if ((mtu < l_ptr->max_pkt_target) &&
1659 link_working_working(l_ptr) &&
1660 l_ptr->fsm_msg_cnt) {
1661 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1662 if (l_ptr->max_pkt_probes == 10) {
1663 l_ptr->max_pkt_target = (msg_size - 4);
1664 l_ptr->max_pkt_probes = 0;
1665 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1667 l_ptr->max_pkt_probes++;
1670 l_ptr->stats.sent_probes++;
1672 l_ptr->stats.sent_states++;
1673 } else { /* RESET_MSG or ACTIVATE_MSG */
1674 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1675 msg_set_seq_gap(msg, 0);
1676 msg_set_next_sent(msg, 1);
1677 msg_set_probe(msg, 0);
1678 msg_set_link_tolerance(msg, l_ptr->tolerance);
1679 msg_set_linkprio(msg, l_ptr->priority);
1680 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1683 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
1684 msg_set_redundant_link(msg, r_flag);
1685 msg_set_linkprio(msg, l_ptr->priority);
1686 msg_set_size(msg, msg_size);
1688 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1690 buf = tipc_buf_acquire(msg_size);
1694 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
1695 buf->priority = TC_PRIO_CONTROL;
1697 tipc_bearer_send(l_ptr->bearer_id, buf, &l_ptr->media_addr);
1698 l_ptr->unacked_window = 0;
1703 * Receive protocol message :
1704 * Note that network plane id propagates through the network, and may
1705 * change at any time. The node with lowest address rules
1707 static void tipc_link_proto_rcv(struct tipc_link *l_ptr, struct sk_buff *buf)
1713 struct tipc_msg *msg = buf_msg(buf);
1715 /* Discard protocol message during link changeover */
1716 if (l_ptr->exp_msg_count)
1719 if (l_ptr->net_plane != msg_net_plane(msg))
1720 if (tipc_own_addr > msg_prevnode(msg))
1721 l_ptr->net_plane = msg_net_plane(msg);
1723 switch (msg_type(msg)) {
1726 if (!link_working_unknown(l_ptr) &&
1727 (l_ptr->peer_session != INVALID_SESSION)) {
1728 if (less_eq(msg_session(msg), l_ptr->peer_session))
1729 break; /* duplicate or old reset: ignore */
1732 if (!msg_redundant_link(msg) && (link_working_working(l_ptr) ||
1733 link_working_unknown(l_ptr))) {
1735 * peer has lost contact -- don't allow peer's links
1736 * to reactivate before we recognize loss & clean up
1738 l_ptr->owner->action_flags |= TIPC_WAIT_OWN_LINKS_DOWN;
1741 link_state_event(l_ptr, RESET_MSG);
1745 /* Update link settings according other endpoint's values */
1746 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
1748 msg_tol = msg_link_tolerance(msg);
1749 if (msg_tol > l_ptr->tolerance)
1750 link_set_supervision_props(l_ptr, msg_tol);
1752 if (msg_linkprio(msg) > l_ptr->priority)
1753 l_ptr->priority = msg_linkprio(msg);
1755 max_pkt_info = msg_max_pkt(msg);
1757 if (max_pkt_info < l_ptr->max_pkt_target)
1758 l_ptr->max_pkt_target = max_pkt_info;
1759 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1760 l_ptr->max_pkt = l_ptr->max_pkt_target;
1762 l_ptr->max_pkt = l_ptr->max_pkt_target;
1765 /* Synchronize broadcast link info, if not done previously */
1766 if (!tipc_node_is_up(l_ptr->owner)) {
1767 l_ptr->owner->bclink.last_sent =
1768 l_ptr->owner->bclink.last_in =
1769 msg_last_bcast(msg);
1770 l_ptr->owner->bclink.oos_state = 0;
1773 l_ptr->peer_session = msg_session(msg);
1774 l_ptr->peer_bearer_id = msg_bearer_id(msg);
1776 if (msg_type(msg) == ACTIVATE_MSG)
1777 link_state_event(l_ptr, ACTIVATE_MSG);
1781 msg_tol = msg_link_tolerance(msg);
1783 link_set_supervision_props(l_ptr, msg_tol);
1785 if (msg_linkprio(msg) &&
1786 (msg_linkprio(msg) != l_ptr->priority)) {
1787 pr_warn("%s<%s>, priority change %u->%u\n",
1788 link_rst_msg, l_ptr->name, l_ptr->priority,
1790 l_ptr->priority = msg_linkprio(msg);
1791 tipc_link_reset(l_ptr); /* Enforce change to take effect */
1795 /* Record reception; force mismatch at next timeout: */
1796 l_ptr->checkpoint--;
1798 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1799 l_ptr->stats.recv_states++;
1800 if (link_reset_unknown(l_ptr))
1803 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
1804 rec_gap = mod(msg_next_sent(msg) -
1805 mod(l_ptr->next_in_no));
1808 max_pkt_ack = msg_max_pkt(msg);
1809 if (max_pkt_ack > l_ptr->max_pkt) {
1810 l_ptr->max_pkt = max_pkt_ack;
1811 l_ptr->max_pkt_probes = 0;
1815 if (msg_probe(msg)) {
1816 l_ptr->stats.recv_probes++;
1817 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
1818 max_pkt_ack = msg_size(msg);
1821 /* Protocol message before retransmits, reduce loss risk */
1822 if (l_ptr->owner->bclink.recv_permitted)
1823 tipc_bclink_update_link_state(l_ptr->owner,
1824 msg_last_bcast(msg));
1826 if (rec_gap || (msg_probe(msg))) {
1827 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0,
1830 if (msg_seq_gap(msg)) {
1831 l_ptr->stats.recv_nacks++;
1832 tipc_link_retransmit(l_ptr, l_ptr->first_out,
1842 /* tipc_link_tunnel_xmit(): Tunnel one packet via a link belonging to
1843 * a different bearer. Owner node is locked.
1845 static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1846 struct tipc_msg *tunnel_hdr,
1847 struct tipc_msg *msg,
1850 struct tipc_link *tunnel;
1851 struct sk_buff *buf;
1852 u32 length = msg_size(msg);
1854 tunnel = l_ptr->owner->active_links[selector & 1];
1855 if (!tipc_link_is_up(tunnel)) {
1856 pr_warn("%stunnel link no longer available\n", link_co_err);
1859 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
1860 buf = tipc_buf_acquire(length + INT_H_SIZE);
1862 pr_warn("%sunable to send tunnel msg\n", link_co_err);
1865 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
1866 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
1867 __tipc_link_xmit(tunnel, buf);
1871 /* tipc_link_failover_send_queue(): A link has gone down, but a second
1872 * link is still active. We can do failover. Tunnel the failing link's
1873 * whole send queue via the remaining link. This way, we don't lose
1874 * any packets, and sequence order is preserved for subsequent traffic
1875 * sent over the remaining link. Owner node is locked.
1877 void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
1879 u32 msgcount = l_ptr->out_queue_size;
1880 struct sk_buff *crs = l_ptr->first_out;
1881 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
1882 struct tipc_msg tunnel_hdr;
1888 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
1889 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
1890 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1891 msg_set_msgcnt(&tunnel_hdr, msgcount);
1893 if (!l_ptr->first_out) {
1894 struct sk_buff *buf;
1896 buf = tipc_buf_acquire(INT_H_SIZE);
1898 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
1899 msg_set_size(&tunnel_hdr, INT_H_SIZE);
1900 __tipc_link_xmit(tunnel, buf);
1902 pr_warn("%sunable to send changeover msg\n",
1908 split_bundles = (l_ptr->owner->active_links[0] !=
1909 l_ptr->owner->active_links[1]);
1912 struct tipc_msg *msg = buf_msg(crs);
1914 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
1915 struct tipc_msg *m = msg_get_wrapped(msg);
1916 unchar *pos = (unchar *)m;
1918 msgcount = msg_msgcnt(msg);
1919 while (msgcount--) {
1920 msg_set_seqno(m, msg_seqno(msg));
1921 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, m,
1922 msg_link_selector(m));
1923 pos += align(msg_size(m));
1924 m = (struct tipc_msg *)pos;
1927 tipc_link_tunnel_xmit(l_ptr, &tunnel_hdr, msg,
1928 msg_link_selector(msg));
1934 /* tipc_link_dup_queue_xmit(): A second link has become active. Tunnel a
1935 * duplicate of the first link's send queue via the new link. This way, we
1936 * are guaranteed that currently queued packets from a socket are delivered
1937 * before future traffic from the same socket, even if this is using the
1938 * new link. The last arriving copy of each duplicate packet is dropped at
1939 * the receiving end by the regular protocol check, so packet cardinality
1940 * and sequence order is preserved per sender/receiver socket pair.
1941 * Owner node is locked.
1943 void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr,
1944 struct tipc_link *tunnel)
1946 struct sk_buff *iter;
1947 struct tipc_msg tunnel_hdr;
1949 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
1950 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
1951 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
1952 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1953 iter = l_ptr->first_out;
1955 struct sk_buff *outbuf;
1956 struct tipc_msg *msg = buf_msg(iter);
1957 u32 length = msg_size(msg);
1959 if (msg_user(msg) == MSG_BUNDLER)
1960 msg_set_type(msg, CLOSED_MSG);
1961 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
1962 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1963 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
1964 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
1965 if (outbuf == NULL) {
1966 pr_warn("%sunable to send duplicate msg\n",
1970 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
1971 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
1973 __tipc_link_xmit(tunnel, outbuf);
1974 if (!tipc_link_is_up(l_ptr))
1981 * buf_extract - extracts embedded TIPC message from another message
1982 * @skb: encapsulating message buffer
1983 * @from_pos: offset to extract from
1985 * Returns a new message buffer containing an embedded message. The
1986 * encapsulating message itself is left unchanged.
1988 static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1990 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1991 u32 size = msg_size(msg);
1994 eb = tipc_buf_acquire(size);
1996 skb_copy_to_linear_data(eb, msg, size);
2002 /* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
2003 * Owner node is locked.
2005 static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
2006 struct sk_buff *t_buf)
2008 struct sk_buff *buf;
2010 if (!tipc_link_is_up(l_ptr))
2013 buf = buf_extract(t_buf, INT_H_SIZE);
2015 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
2019 /* Add buffer to deferred queue, if applicable: */
2020 link_handle_out_of_seq_msg(l_ptr, buf);
2023 /* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet
2024 * Owner node is locked.
2026 static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr,
2027 struct sk_buff *t_buf)
2029 struct tipc_msg *t_msg = buf_msg(t_buf);
2030 struct sk_buff *buf = NULL;
2031 struct tipc_msg *msg;
2033 if (tipc_link_is_up(l_ptr))
2034 tipc_link_reset(l_ptr);
2036 /* First failover packet? */
2037 if (l_ptr->exp_msg_count == START_CHANGEOVER)
2038 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
2040 /* Should there be an inner packet? */
2041 if (l_ptr->exp_msg_count) {
2042 l_ptr->exp_msg_count--;
2043 buf = buf_extract(t_buf, INT_H_SIZE);
2045 pr_warn("%sno inner failover pkt\n", link_co_err);
2050 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) {
2055 if (msg_user(msg) == MSG_FRAGMENTER) {
2056 l_ptr->stats.recv_fragments++;
2057 tipc_buf_append(&l_ptr->reasm_buf, &buf);
2061 if ((l_ptr->exp_msg_count == 0) && (l_ptr->flags & LINK_STOPPED)) {
2062 tipc_node_detach_link(l_ptr->owner, l_ptr);
2068 /* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent
2069 * via other link as result of a failover (ORIGINAL_MSG) or
2070 * a new active link (DUPLICATE_MSG). Failover packets are
2071 * returned to the active link for delivery upwards.
2072 * Owner node is locked.
2074 static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
2075 struct sk_buff **buf)
2077 struct sk_buff *t_buf = *buf;
2078 struct tipc_link *l_ptr;
2079 struct tipc_msg *t_msg = buf_msg(t_buf);
2080 u32 bearer_id = msg_bearer_id(t_msg);
2084 if (bearer_id >= MAX_BEARERS)
2087 l_ptr = n_ptr->links[bearer_id];
2091 if (msg_type(t_msg) == DUPLICATE_MSG)
2092 tipc_link_dup_rcv(l_ptr, t_buf);
2093 else if (msg_type(t_msg) == ORIGINAL_MSG)
2094 *buf = tipc_link_failover_rcv(l_ptr, t_buf);
2096 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
2099 return *buf != NULL;
2103 * Bundler functionality:
2105 void tipc_link_bundle_rcv(struct sk_buff *buf)
2107 u32 msgcount = msg_msgcnt(buf_msg(buf));
2108 u32 pos = INT_H_SIZE;
2109 struct sk_buff *obuf;
2110 struct tipc_msg *omsg;
2112 while (msgcount--) {
2113 obuf = buf_extract(buf, pos);
2115 pr_warn("Link unable to unbundle message(s)\n");
2118 omsg = buf_msg(obuf);
2119 pos += align(msg_size(omsg));
2120 if (msg_isdata(omsg) || (msg_user(omsg) == CONN_MANAGER)) {
2122 } else if (msg_user(omsg) == NAME_DISTRIBUTOR) {
2123 tipc_named_rcv(obuf);
2125 pr_warn("Illegal bundled msg: %u\n", msg_user(omsg));
2133 * Fragmentation/defragmentation:
2137 * tipc_link_frag_xmit: Entry for buffers needing fragmentation.
2138 * The buffer is complete, inclusive total message length.
2139 * Returns user data length.
2141 static int tipc_link_frag_xmit(struct tipc_link *l_ptr, struct sk_buff *buf)
2143 struct sk_buff *buf_chain = NULL;
2144 struct sk_buff *buf_chain_tail = (struct sk_buff *)&buf_chain;
2145 struct tipc_msg *inmsg = buf_msg(buf);
2146 struct tipc_msg fragm_hdr;
2147 u32 insize = msg_size(inmsg);
2148 u32 dsz = msg_data_sz(inmsg);
2149 unchar *crs = buf->data;
2151 u32 pack_sz = l_ptr->max_pkt;
2152 u32 fragm_sz = pack_sz - INT_H_SIZE;
2156 if (msg_short(inmsg))
2157 destaddr = l_ptr->addr;
2159 destaddr = msg_destnode(inmsg);
2161 /* Prepare reusable fragment header: */
2162 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
2163 INT_H_SIZE, destaddr);
2165 /* Chop up message: */
2167 struct sk_buff *fragm;
2169 if (rest <= fragm_sz) {
2171 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2173 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
2174 if (fragm == NULL) {
2176 kfree_skb_list(buf_chain);
2179 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
2181 msg_set_fragm_no(&fragm_hdr, fragm_no);
2182 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2183 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2185 buf_chain_tail->next = fragm;
2186 buf_chain_tail = fragm;
2190 msg_set_type(&fragm_hdr, FRAGMENT);
2194 /* Append chain of fragments to send queue & send them */
2195 l_ptr->long_msg_seq_no++;
2196 link_add_chain_to_outqueue(l_ptr, buf_chain, l_ptr->long_msg_seq_no);
2197 l_ptr->stats.sent_fragments += fragm_no;
2198 l_ptr->stats.sent_fragmented++;
2199 tipc_link_push_queue(l_ptr);
2204 static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
2206 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2209 l_ptr->tolerance = tolerance;
2210 l_ptr->continuity_interval =
2211 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2212 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2215 void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
2217 /* Data messages from this node, inclusive FIRST_FRAGM */
2218 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2219 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2220 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2221 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
2222 /* Transiting data messages,inclusive FIRST_FRAGM */
2223 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2224 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2225 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2226 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
2227 l_ptr->queue_limit[CONN_MANAGER] = 1200;
2228 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2229 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2230 /* FRAGMENT and LAST_FRAGMENT packets */
2231 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2234 /* tipc_link_find_owner - locate owner node of link by link's name
2235 * @name: pointer to link name string
2236 * @bearer_id: pointer to index in 'node->links' array where the link was found.
2238 * Returns pointer to node owning the link, or 0 if no matching link is found.
2240 static struct tipc_node *tipc_link_find_owner(const char *link_name,
2241 unsigned int *bearer_id)
2243 struct tipc_link *l_ptr;
2244 struct tipc_node *n_ptr;
2245 struct tipc_node *found_node = 0;
2250 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
2251 tipc_node_lock(n_ptr);
2252 for (i = 0; i < MAX_BEARERS; i++) {
2253 l_ptr = n_ptr->links[i];
2254 if (l_ptr && !strcmp(l_ptr->name, link_name)) {
2260 tipc_node_unlock(n_ptr);
2270 * link_value_is_valid -- validate proposed link tolerance/priority/window
2272 * @cmd: value type (TIPC_CMD_SET_LINK_*)
2273 * @new_value: the new value
2275 * Returns 1 if value is within range, 0 if not.
2277 static int link_value_is_valid(u16 cmd, u32 new_value)
2280 case TIPC_CMD_SET_LINK_TOL:
2281 return (new_value >= TIPC_MIN_LINK_TOL) &&
2282 (new_value <= TIPC_MAX_LINK_TOL);
2283 case TIPC_CMD_SET_LINK_PRI:
2284 return (new_value <= TIPC_MAX_LINK_PRI);
2285 case TIPC_CMD_SET_LINK_WINDOW:
2286 return (new_value >= TIPC_MIN_LINK_WIN) &&
2287 (new_value <= TIPC_MAX_LINK_WIN);
2293 * link_cmd_set_value - change priority/tolerance/window for link/bearer/media
2294 * @name: ptr to link, bearer, or media name
2295 * @new_value: new value of link, bearer, or media setting
2296 * @cmd: which link, bearer, or media attribute to set (TIPC_CMD_SET_LINK_*)
2298 * Caller must hold RTNL lock to ensure link/bearer/media is not deleted.
2300 * Returns 0 if value updated and negative value on error.
2302 static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2304 struct tipc_node *node;
2305 struct tipc_link *l_ptr;
2306 struct tipc_bearer *b_ptr;
2307 struct tipc_media *m_ptr;
2311 node = tipc_link_find_owner(name, &bearer_id);
2313 tipc_node_lock(node);
2314 l_ptr = node->links[bearer_id];
2318 case TIPC_CMD_SET_LINK_TOL:
2319 link_set_supervision_props(l_ptr, new_value);
2320 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2323 case TIPC_CMD_SET_LINK_PRI:
2324 l_ptr->priority = new_value;
2325 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0,
2328 case TIPC_CMD_SET_LINK_WINDOW:
2329 tipc_link_set_queue_limits(l_ptr, new_value);
2336 tipc_node_unlock(node);
2340 b_ptr = tipc_bearer_find(name);
2343 case TIPC_CMD_SET_LINK_TOL:
2344 b_ptr->tolerance = new_value;
2346 case TIPC_CMD_SET_LINK_PRI:
2347 b_ptr->priority = new_value;
2349 case TIPC_CMD_SET_LINK_WINDOW:
2350 b_ptr->window = new_value;
2359 m_ptr = tipc_media_find(name);
2363 case TIPC_CMD_SET_LINK_TOL:
2364 m_ptr->tolerance = new_value;
2366 case TIPC_CMD_SET_LINK_PRI:
2367 m_ptr->priority = new_value;
2369 case TIPC_CMD_SET_LINK_WINDOW:
2370 m_ptr->window = new_value;
2379 struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
2382 struct tipc_link_config *args;
2386 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
2387 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2389 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2390 new_value = ntohl(args->value);
2392 if (!link_value_is_valid(cmd, new_value))
2393 return tipc_cfg_reply_error_string(
2394 "cannot change, value invalid");
2396 if (!strcmp(args->name, tipc_bclink_name)) {
2397 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
2398 (tipc_bclink_set_queue_limits(new_value) == 0))
2399 return tipc_cfg_reply_none();
2400 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
2401 " (cannot change setting on broadcast link)");
2404 res = link_cmd_set_value(args->name, new_value, cmd);
2406 return tipc_cfg_reply_error_string("cannot change link setting");
2408 return tipc_cfg_reply_none();
2412 * link_reset_statistics - reset link statistics
2413 * @l_ptr: pointer to link
2415 static void link_reset_statistics(struct tipc_link *l_ptr)
2417 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2418 l_ptr->stats.sent_info = l_ptr->next_out_no;
2419 l_ptr->stats.recv_info = l_ptr->next_in_no;
2422 struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
2425 struct tipc_link *l_ptr;
2426 struct tipc_node *node;
2427 unsigned int bearer_id;
2429 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2430 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2432 link_name = (char *)TLV_DATA(req_tlv_area);
2433 if (!strcmp(link_name, tipc_bclink_name)) {
2434 if (tipc_bclink_reset_stats())
2435 return tipc_cfg_reply_error_string("link not found");
2436 return tipc_cfg_reply_none();
2438 node = tipc_link_find_owner(link_name, &bearer_id);
2440 return tipc_cfg_reply_error_string("link not found");
2442 tipc_node_lock(node);
2443 l_ptr = node->links[bearer_id];
2445 tipc_node_unlock(node);
2446 return tipc_cfg_reply_error_string("link not found");
2448 link_reset_statistics(l_ptr);
2449 tipc_node_unlock(node);
2450 return tipc_cfg_reply_none();
2454 * percent - convert count to a percentage of total (rounding up or down)
2456 static u32 percent(u32 count, u32 total)
2458 return (count * 100 + (total / 2)) / total;
2462 * tipc_link_stats - print link statistics
2464 * @buf: print buffer area
2465 * @buf_size: size of print buffer area
2467 * Returns length of print buffer data string (or 0 if error)
2469 static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
2471 struct tipc_link *l;
2472 struct tipc_stats *s;
2473 struct tipc_node *node;
2475 u32 profile_total = 0;
2476 unsigned int bearer_id;
2479 if (!strcmp(name, tipc_bclink_name))
2480 return tipc_bclink_stats(buf, buf_size);
2482 node = tipc_link_find_owner(name, &bearer_id);
2486 tipc_node_lock(node);
2488 l = node->links[bearer_id];
2490 tipc_node_unlock(node);
2496 if (tipc_link_is_active(l))
2498 else if (tipc_link_is_up(l))
2503 ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
2504 " %s MTU:%u Priority:%u Tolerance:%u ms"
2505 " Window:%u packets\n",
2506 l->name, status, l->max_pkt, l->priority,
2507 l->tolerance, l->queue_limit[0]);
2509 ret += tipc_snprintf(buf + ret, buf_size - ret,
2510 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2511 l->next_in_no - s->recv_info, s->recv_fragments,
2512 s->recv_fragmented, s->recv_bundles,
2515 ret += tipc_snprintf(buf + ret, buf_size - ret,
2516 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2517 l->next_out_no - s->sent_info, s->sent_fragments,
2518 s->sent_fragmented, s->sent_bundles,
2521 profile_total = s->msg_length_counts;
2525 ret += tipc_snprintf(buf + ret, buf_size - ret,
2526 " TX profile sample:%u packets average:%u octets\n"
2527 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2528 "-16384:%u%% -32768:%u%% -66000:%u%%\n",
2529 s->msg_length_counts,
2530 s->msg_lengths_total / profile_total,
2531 percent(s->msg_length_profile[0], profile_total),
2532 percent(s->msg_length_profile[1], profile_total),
2533 percent(s->msg_length_profile[2], profile_total),
2534 percent(s->msg_length_profile[3], profile_total),
2535 percent(s->msg_length_profile[4], profile_total),
2536 percent(s->msg_length_profile[5], profile_total),
2537 percent(s->msg_length_profile[6], profile_total));
2539 ret += tipc_snprintf(buf + ret, buf_size - ret,
2540 " RX states:%u probes:%u naks:%u defs:%u"
2541 " dups:%u\n", s->recv_states, s->recv_probes,
2542 s->recv_nacks, s->deferred_recv, s->duplicates);
2544 ret += tipc_snprintf(buf + ret, buf_size - ret,
2545 " TX states:%u probes:%u naks:%u acks:%u"
2546 " dups:%u\n", s->sent_states, s->sent_probes,
2547 s->sent_nacks, s->sent_acks, s->retransmitted);
2549 ret += tipc_snprintf(buf + ret, buf_size - ret,
2550 " Congestion link:%u Send queue"
2551 " max:%u avg:%u\n", s->link_congs,
2552 s->max_queue_sz, s->queue_sz_counts ?
2553 (s->accu_queue_sz / s->queue_sz_counts) : 0);
2555 tipc_node_unlock(node);
2559 struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
2561 struct sk_buff *buf;
2562 struct tlv_desc *rep_tlv;
2567 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2568 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2570 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2574 rep_tlv = (struct tlv_desc *)buf->data;
2575 pb = TLV_DATA(rep_tlv);
2576 pb_len = ULTRA_STRING_MAX_LEN;
2577 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
2581 return tipc_cfg_reply_error_string("link not found");
2583 str_len += 1; /* for "\0" */
2584 skb_put(buf, TLV_SPACE(str_len));
2585 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2591 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
2592 * @dest: network address of destination node
2593 * @selector: used to select from set of active links
2595 * If no active link can be found, uses default maximum packet size.
2597 u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
2599 struct tipc_node *n_ptr;
2600 struct tipc_link *l_ptr;
2601 u32 res = MAX_PKT_DEFAULT;
2603 if (dest == tipc_own_addr)
2604 return MAX_MSG_SIZE;
2606 n_ptr = tipc_node_find(dest);
2608 tipc_node_lock(n_ptr);
2609 l_ptr = n_ptr->active_links[selector & 1];
2611 res = l_ptr->max_pkt;
2612 tipc_node_unlock(n_ptr);
2617 static void link_print(struct tipc_link *l_ptr, const char *str)
2619 struct tipc_bearer *b_ptr;
2622 b_ptr = rcu_dereference_rtnl(bearer_list[l_ptr->bearer_id]);
2624 pr_info("%s Link %x<%s>:", str, l_ptr->addr, b_ptr->name);
2627 if (link_working_unknown(l_ptr))
2629 else if (link_reset_reset(l_ptr))
2631 else if (link_reset_unknown(l_ptr))
2633 else if (link_working_working(l_ptr))