2 * net/tipc/link.c: TIPC link code
4 * Copyright (c) 1996-2007, Ericsson AB
5 * Copyright (c) 2004-2007, 2010-2011, 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.
40 #include "name_distr.h"
46 * Out-of-range value for link session numbers
49 #define INVALID_SESSION 0x10000
55 #define STARTING_EVT 856384768 /* link processing trigger */
56 #define TRAFFIC_MSG_EVT 560815u /* rx'd ??? */
57 #define TIMEOUT_EVT 560817u /* link timer expired */
60 * The following two 'message types' is really just implementation
61 * data conveniently stored in the message header.
62 * They must not be considered part of the protocol
68 * State value stored in 'exp_msg_count'
71 #define START_CHANGEOVER 100000u
74 * struct link_name - deconstructed link name
75 * @addr_local: network address of node at this end
76 * @if_local: name of interface at this end
77 * @addr_peer: network address of node at far end
78 * @if_peer: name of interface at far end
83 char if_local[TIPC_MAX_IF_NAME];
85 char if_peer[TIPC_MAX_IF_NAME];
88 static void link_handle_out_of_seq_msg(struct link *l_ptr,
90 static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf);
91 static int link_recv_changeover_msg(struct link **l_ptr, struct sk_buff **buf);
92 static void link_set_supervision_props(struct link *l_ptr, u32 tolerance);
93 static int link_send_sections_long(struct tipc_port *sender,
94 struct iovec const *msg_sect,
95 u32 num_sect, u32 destnode);
96 static void link_check_defragm_bufs(struct link *l_ptr);
97 static void link_state_event(struct link *l_ptr, u32 event);
98 static void link_reset_statistics(struct link *l_ptr);
99 static void link_print(struct link *l_ptr, const char *str);
100 static void link_start(struct link *l_ptr);
101 static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf);
104 * Simple link routines
107 static unsigned int align(unsigned int i)
109 return (i + 3) & ~3u;
112 static void link_init_max_pkt(struct link *l_ptr)
116 max_pkt = (l_ptr->b_ptr->mtu & ~3);
117 if (max_pkt > MAX_MSG_SIZE)
118 max_pkt = MAX_MSG_SIZE;
120 l_ptr->max_pkt_target = max_pkt;
121 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
122 l_ptr->max_pkt = l_ptr->max_pkt_target;
124 l_ptr->max_pkt = MAX_PKT_DEFAULT;
126 l_ptr->max_pkt_probes = 0;
129 static u32 link_next_sent(struct link *l_ptr)
132 return msg_seqno(buf_msg(l_ptr->next_out));
133 return mod(l_ptr->next_out_no);
136 static u32 link_last_sent(struct link *l_ptr)
138 return mod(link_next_sent(l_ptr) - 1);
142 * Simple non-static link routines (i.e. referenced outside this file)
145 int tipc_link_is_up(struct link *l_ptr)
149 return link_working_working(l_ptr) || link_working_unknown(l_ptr);
152 int tipc_link_is_active(struct link *l_ptr)
154 return (l_ptr->owner->active_links[0] == l_ptr) ||
155 (l_ptr->owner->active_links[1] == l_ptr);
159 * link_name_validate - validate & (optionally) deconstruct link name
160 * @name - ptr to link name string
161 * @name_parts - ptr to area for link name components (or NULL if not needed)
163 * Returns 1 if link name is valid, otherwise 0.
166 static int link_name_validate(const char *name, struct link_name *name_parts)
168 char name_copy[TIPC_MAX_LINK_NAME];
174 u32 z_local, c_local, n_local;
175 u32 z_peer, c_peer, n_peer;
179 /* copy link name & ensure length is OK */
181 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
182 /* need above in case non-Posix strncpy() doesn't pad with nulls */
183 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
184 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
187 /* ensure all component parts of link name are present */
189 addr_local = name_copy;
190 if_local = strchr(addr_local, ':');
191 if (if_local == NULL)
194 addr_peer = strchr(if_local, '-');
195 if (addr_peer == NULL)
198 if_local_len = addr_peer - if_local;
199 if_peer = strchr(addr_peer, ':');
203 if_peer_len = strlen(if_peer) + 1;
205 /* validate component parts of link name */
207 if ((sscanf(addr_local, "%u.%u.%u%c",
208 &z_local, &c_local, &n_local, &dummy) != 3) ||
209 (sscanf(addr_peer, "%u.%u.%u%c",
210 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
211 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
212 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
213 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
214 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME) ||
215 (strspn(if_local, tipc_alphabet) != (if_local_len - 1)) ||
216 (strspn(if_peer, tipc_alphabet) != (if_peer_len - 1)))
219 /* return link name components, if necessary */
222 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
223 strcpy(name_parts->if_local, if_local);
224 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
225 strcpy(name_parts->if_peer, if_peer);
231 * link_timeout - handle expiration of link timer
232 * @l_ptr: pointer to link
234 * This routine must not grab "tipc_net_lock" to avoid a potential deadlock conflict
235 * with tipc_link_delete(). (There is no risk that the node will be deleted by
236 * another thread because tipc_link_delete() always cancels the link timer before
237 * tipc_node_delete() is called.)
240 static void link_timeout(struct link *l_ptr)
242 tipc_node_lock(l_ptr->owner);
244 /* update counters used in statistical profiling of send traffic */
246 l_ptr->stats.accu_queue_sz += l_ptr->out_queue_size;
247 l_ptr->stats.queue_sz_counts++;
249 if (l_ptr->first_out) {
250 struct tipc_msg *msg = buf_msg(l_ptr->first_out);
251 u32 length = msg_size(msg);
253 if ((msg_user(msg) == MSG_FRAGMENTER) &&
254 (msg_type(msg) == FIRST_FRAGMENT)) {
255 length = msg_size(msg_get_wrapped(msg));
258 l_ptr->stats.msg_lengths_total += length;
259 l_ptr->stats.msg_length_counts++;
261 l_ptr->stats.msg_length_profile[0]++;
262 else if (length <= 256)
263 l_ptr->stats.msg_length_profile[1]++;
264 else if (length <= 1024)
265 l_ptr->stats.msg_length_profile[2]++;
266 else if (length <= 4096)
267 l_ptr->stats.msg_length_profile[3]++;
268 else if (length <= 16384)
269 l_ptr->stats.msg_length_profile[4]++;
270 else if (length <= 32768)
271 l_ptr->stats.msg_length_profile[5]++;
273 l_ptr->stats.msg_length_profile[6]++;
277 /* do all other link processing performed on a periodic basis */
279 link_check_defragm_bufs(l_ptr);
281 link_state_event(l_ptr, TIMEOUT_EVT);
284 tipc_link_push_queue(l_ptr);
286 tipc_node_unlock(l_ptr->owner);
289 static void link_set_timer(struct link *l_ptr, u32 time)
291 k_start_timer(&l_ptr->timer, time);
295 * tipc_link_create - create a new link
296 * @b_ptr: pointer to associated bearer
297 * @peer: network address of node at other end of link
298 * @media_addr: media address to use when sending messages over link
300 * Returns pointer to link.
303 struct link *tipc_link_create(struct tipc_bearer *b_ptr, const u32 peer,
304 const struct tipc_media_addr *media_addr)
307 struct tipc_msg *msg;
310 l_ptr = kzalloc(sizeof(*l_ptr), GFP_ATOMIC);
312 warn("Link creation failed, no memory\n");
317 if_name = strchr(b_ptr->name, ':') + 1;
318 sprintf(l_ptr->name, "%u.%u.%u:%s-%u.%u.%u:",
319 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
320 tipc_node(tipc_own_addr),
322 tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
323 /* note: peer i/f is appended to link name by reset/activate */
324 memcpy(&l_ptr->media_addr, media_addr, sizeof(*media_addr));
325 l_ptr->checkpoint = 1;
326 l_ptr->b_ptr = b_ptr;
327 link_set_supervision_props(l_ptr, b_ptr->media->tolerance);
328 l_ptr->state = RESET_UNKNOWN;
330 l_ptr->pmsg = (struct tipc_msg *)&l_ptr->proto_msg;
332 tipc_msg_init(msg, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, l_ptr->addr);
333 msg_set_size(msg, sizeof(l_ptr->proto_msg));
334 msg_set_session(msg, (tipc_random & 0xffff));
335 msg_set_bearer_id(msg, b_ptr->identity);
336 strcpy((char *)msg_data(msg), if_name);
338 l_ptr->priority = b_ptr->priority;
339 tipc_link_set_queue_limits(l_ptr, b_ptr->media->window);
341 link_init_max_pkt(l_ptr);
343 l_ptr->next_out_no = 1;
344 INIT_LIST_HEAD(&l_ptr->waiting_ports);
346 link_reset_statistics(l_ptr);
348 l_ptr->owner = tipc_node_attach_link(l_ptr);
354 k_init_timer(&l_ptr->timer, (Handler)link_timeout, (unsigned long)l_ptr);
355 list_add_tail(&l_ptr->link_list, &b_ptr->links);
356 tipc_k_signal((Handler)link_start, (unsigned long)l_ptr);
362 * tipc_link_delete - delete a link
363 * @l_ptr: pointer to link
365 * Note: 'tipc_net_lock' is write_locked, bearer is locked.
366 * This routine must not grab the node lock until after link timer cancellation
367 * to avoid a potential deadlock situation.
370 void tipc_link_delete(struct link *l_ptr)
373 err("Attempt to delete non-existent link\n");
377 k_cancel_timer(&l_ptr->timer);
379 tipc_node_lock(l_ptr->owner);
380 tipc_link_reset(l_ptr);
381 tipc_node_detach_link(l_ptr->owner, l_ptr);
382 tipc_link_stop(l_ptr);
383 list_del_init(&l_ptr->link_list);
384 tipc_node_unlock(l_ptr->owner);
385 k_term_timer(&l_ptr->timer);
389 static void link_start(struct link *l_ptr)
391 tipc_node_lock(l_ptr->owner);
392 link_state_event(l_ptr, STARTING_EVT);
393 tipc_node_unlock(l_ptr->owner);
397 * link_schedule_port - schedule port for deferred sending
398 * @l_ptr: pointer to link
399 * @origport: reference to sending port
400 * @sz: amount of data to be sent
402 * Schedules port for renewed sending of messages after link congestion
406 static int link_schedule_port(struct link *l_ptr, u32 origport, u32 sz)
408 struct tipc_port *p_ptr;
410 spin_lock_bh(&tipc_port_list_lock);
411 p_ptr = tipc_port_lock(origport);
415 if (!list_empty(&p_ptr->wait_list))
417 p_ptr->congested = 1;
418 p_ptr->waiting_pkts = 1 + ((sz - 1) / l_ptr->max_pkt);
419 list_add_tail(&p_ptr->wait_list, &l_ptr->waiting_ports);
420 l_ptr->stats.link_congs++;
422 tipc_port_unlock(p_ptr);
424 spin_unlock_bh(&tipc_port_list_lock);
428 void tipc_link_wakeup_ports(struct link *l_ptr, int all)
430 struct tipc_port *p_ptr;
431 struct tipc_port *temp_p_ptr;
432 int win = l_ptr->queue_limit[0] - l_ptr->out_queue_size;
438 if (!spin_trylock_bh(&tipc_port_list_lock))
440 if (link_congested(l_ptr))
442 list_for_each_entry_safe(p_ptr, temp_p_ptr, &l_ptr->waiting_ports,
446 list_del_init(&p_ptr->wait_list);
447 spin_lock_bh(p_ptr->lock);
448 p_ptr->congested = 0;
449 p_ptr->wakeup(p_ptr);
450 win -= p_ptr->waiting_pkts;
451 spin_unlock_bh(p_ptr->lock);
455 spin_unlock_bh(&tipc_port_list_lock);
459 * link_release_outqueue - purge link's outbound message queue
460 * @l_ptr: pointer to link
463 static void link_release_outqueue(struct link *l_ptr)
465 struct sk_buff *buf = l_ptr->first_out;
466 struct sk_buff *next;
473 l_ptr->first_out = NULL;
474 l_ptr->out_queue_size = 0;
478 * tipc_link_reset_fragments - purge link's inbound message fragments queue
479 * @l_ptr: pointer to link
482 void tipc_link_reset_fragments(struct link *l_ptr)
484 struct sk_buff *buf = l_ptr->defragm_buf;
485 struct sk_buff *next;
492 l_ptr->defragm_buf = NULL;
496 * tipc_link_stop - purge all inbound and outbound messages associated with link
497 * @l_ptr: pointer to link
500 void tipc_link_stop(struct link *l_ptr)
503 struct sk_buff *next;
505 buf = l_ptr->oldest_deferred_in;
512 buf = l_ptr->first_out;
519 tipc_link_reset_fragments(l_ptr);
521 buf_discard(l_ptr->proto_msg_queue);
522 l_ptr->proto_msg_queue = NULL;
525 /* LINK EVENT CODE IS NOT SUPPORTED AT PRESENT */
526 #define link_send_event(fcn, l_ptr, up) do { } while (0)
528 void tipc_link_reset(struct link *l_ptr)
531 u32 prev_state = l_ptr->state;
532 u32 checkpoint = l_ptr->next_in_no;
533 int was_active_link = tipc_link_is_active(l_ptr);
535 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
537 /* Link is down, accept any session */
538 l_ptr->peer_session = INVALID_SESSION;
540 /* Prepare for max packet size negotiation */
541 link_init_max_pkt(l_ptr);
543 l_ptr->state = RESET_UNKNOWN;
545 if ((prev_state == RESET_UNKNOWN) || (prev_state == RESET_RESET))
548 tipc_node_link_down(l_ptr->owner, l_ptr);
549 tipc_bearer_remove_dest(l_ptr->b_ptr, l_ptr->addr);
551 if (was_active_link && tipc_node_has_active_links(l_ptr->owner) &&
552 l_ptr->owner->permit_changeover) {
553 l_ptr->reset_checkpoint = checkpoint;
554 l_ptr->exp_msg_count = START_CHANGEOVER;
557 /* Clean up all queues: */
559 link_release_outqueue(l_ptr);
560 buf_discard(l_ptr->proto_msg_queue);
561 l_ptr->proto_msg_queue = NULL;
562 buf = l_ptr->oldest_deferred_in;
564 struct sk_buff *next = buf->next;
568 if (!list_empty(&l_ptr->waiting_ports))
569 tipc_link_wakeup_ports(l_ptr, 1);
571 l_ptr->retransm_queue_head = 0;
572 l_ptr->retransm_queue_size = 0;
573 l_ptr->last_out = NULL;
574 l_ptr->first_out = NULL;
575 l_ptr->next_out = NULL;
576 l_ptr->unacked_window = 0;
577 l_ptr->checkpoint = 1;
578 l_ptr->next_out_no = 1;
579 l_ptr->deferred_inqueue_sz = 0;
580 l_ptr->oldest_deferred_in = NULL;
581 l_ptr->newest_deferred_in = NULL;
582 l_ptr->fsm_msg_cnt = 0;
583 l_ptr->stale_count = 0;
584 link_reset_statistics(l_ptr);
586 link_send_event(tipc_cfg_link_event, l_ptr, 0);
587 if (!in_own_cluster(l_ptr->addr))
588 link_send_event(tipc_disc_link_event, l_ptr, 0);
592 static void link_activate(struct link *l_ptr)
594 l_ptr->next_in_no = l_ptr->stats.recv_info = 1;
595 tipc_node_link_up(l_ptr->owner, l_ptr);
596 tipc_bearer_add_dest(l_ptr->b_ptr, l_ptr->addr);
597 link_send_event(tipc_cfg_link_event, l_ptr, 1);
598 if (!in_own_cluster(l_ptr->addr))
599 link_send_event(tipc_disc_link_event, l_ptr, 1);
603 * link_state_event - link finite state machine
604 * @l_ptr: pointer to link
605 * @event: state machine event to process
608 static void link_state_event(struct link *l_ptr, unsigned event)
611 u32 cont_intv = l_ptr->continuity_interval;
613 if (!l_ptr->started && (event != STARTING_EVT))
614 return; /* Not yet. */
616 if (link_blocked(l_ptr)) {
617 if (event == TIMEOUT_EVT)
618 link_set_timer(l_ptr, cont_intv);
619 return; /* Changeover going on */
622 switch (l_ptr->state) {
623 case WORKING_WORKING:
625 case TRAFFIC_MSG_EVT:
629 if (l_ptr->next_in_no != l_ptr->checkpoint) {
630 l_ptr->checkpoint = l_ptr->next_in_no;
631 if (tipc_bclink_acks_missing(l_ptr->owner)) {
632 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
634 l_ptr->fsm_msg_cnt++;
635 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
636 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
638 l_ptr->fsm_msg_cnt++;
640 link_set_timer(l_ptr, cont_intv);
643 l_ptr->state = WORKING_UNKNOWN;
644 l_ptr->fsm_msg_cnt = 0;
645 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
646 l_ptr->fsm_msg_cnt++;
647 link_set_timer(l_ptr, cont_intv / 4);
650 info("Resetting link <%s>, requested by peer\n",
652 tipc_link_reset(l_ptr);
653 l_ptr->state = RESET_RESET;
654 l_ptr->fsm_msg_cnt = 0;
655 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
656 l_ptr->fsm_msg_cnt++;
657 link_set_timer(l_ptr, cont_intv);
660 err("Unknown link event %u in WW state\n", event);
663 case WORKING_UNKNOWN:
665 case TRAFFIC_MSG_EVT:
667 l_ptr->state = WORKING_WORKING;
668 l_ptr->fsm_msg_cnt = 0;
669 link_set_timer(l_ptr, cont_intv);
672 info("Resetting link <%s>, requested by peer "
673 "while probing\n", l_ptr->name);
674 tipc_link_reset(l_ptr);
675 l_ptr->state = RESET_RESET;
676 l_ptr->fsm_msg_cnt = 0;
677 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
678 l_ptr->fsm_msg_cnt++;
679 link_set_timer(l_ptr, cont_intv);
682 if (l_ptr->next_in_no != l_ptr->checkpoint) {
683 l_ptr->state = WORKING_WORKING;
684 l_ptr->fsm_msg_cnt = 0;
685 l_ptr->checkpoint = l_ptr->next_in_no;
686 if (tipc_bclink_acks_missing(l_ptr->owner)) {
687 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
689 l_ptr->fsm_msg_cnt++;
691 link_set_timer(l_ptr, cont_intv);
692 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
693 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
695 l_ptr->fsm_msg_cnt++;
696 link_set_timer(l_ptr, cont_intv / 4);
697 } else { /* Link has failed */
698 warn("Resetting link <%s>, peer not responding\n",
700 tipc_link_reset(l_ptr);
701 l_ptr->state = RESET_UNKNOWN;
702 l_ptr->fsm_msg_cnt = 0;
703 tipc_link_send_proto_msg(l_ptr, RESET_MSG,
705 l_ptr->fsm_msg_cnt++;
706 link_set_timer(l_ptr, cont_intv);
710 err("Unknown link event %u in WU state\n", event);
715 case TRAFFIC_MSG_EVT:
718 other = l_ptr->owner->active_links[0];
719 if (other && link_working_unknown(other))
721 l_ptr->state = WORKING_WORKING;
722 l_ptr->fsm_msg_cnt = 0;
723 link_activate(l_ptr);
724 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
725 l_ptr->fsm_msg_cnt++;
726 link_set_timer(l_ptr, cont_intv);
729 l_ptr->state = RESET_RESET;
730 l_ptr->fsm_msg_cnt = 0;
731 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 1, 0, 0, 0, 0);
732 l_ptr->fsm_msg_cnt++;
733 link_set_timer(l_ptr, cont_intv);
739 tipc_link_send_proto_msg(l_ptr, RESET_MSG, 0, 0, 0, 0, 0);
740 l_ptr->fsm_msg_cnt++;
741 link_set_timer(l_ptr, cont_intv);
744 err("Unknown link event %u in RU state\n", event);
749 case TRAFFIC_MSG_EVT:
751 other = l_ptr->owner->active_links[0];
752 if (other && link_working_unknown(other))
754 l_ptr->state = WORKING_WORKING;
755 l_ptr->fsm_msg_cnt = 0;
756 link_activate(l_ptr);
757 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 1, 0, 0, 0, 0);
758 l_ptr->fsm_msg_cnt++;
759 link_set_timer(l_ptr, cont_intv);
764 tipc_link_send_proto_msg(l_ptr, ACTIVATE_MSG, 0, 0, 0, 0, 0);
765 l_ptr->fsm_msg_cnt++;
766 link_set_timer(l_ptr, cont_intv);
769 err("Unknown link event %u in RR state\n", event);
773 err("Unknown link state %u/%u\n", l_ptr->state, event);
778 * link_bundle_buf(): Append contents of a buffer to
779 * the tail of an existing one.
782 static int link_bundle_buf(struct link *l_ptr,
783 struct sk_buff *bundler,
786 struct tipc_msg *bundler_msg = buf_msg(bundler);
787 struct tipc_msg *msg = buf_msg(buf);
788 u32 size = msg_size(msg);
789 u32 bundle_size = msg_size(bundler_msg);
790 u32 to_pos = align(bundle_size);
791 u32 pad = to_pos - bundle_size;
793 if (msg_user(bundler_msg) != MSG_BUNDLER)
795 if (msg_type(bundler_msg) != OPEN_MSG)
797 if (skb_tailroom(bundler) < (pad + size))
799 if (l_ptr->max_pkt < (to_pos + size))
802 skb_put(bundler, pad + size);
803 skb_copy_to_linear_data_offset(bundler, to_pos, buf->data, size);
804 msg_set_size(bundler_msg, to_pos + size);
805 msg_set_msgcnt(bundler_msg, msg_msgcnt(bundler_msg) + 1);
807 l_ptr->stats.sent_bundled++;
811 static void link_add_to_outqueue(struct link *l_ptr,
813 struct tipc_msg *msg)
815 u32 ack = mod(l_ptr->next_in_no - 1);
816 u32 seqno = mod(l_ptr->next_out_no++);
818 msg_set_word(msg, 2, ((ack << 16) | seqno));
819 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
821 if (l_ptr->first_out) {
822 l_ptr->last_out->next = buf;
823 l_ptr->last_out = buf;
825 l_ptr->first_out = l_ptr->last_out = buf;
827 l_ptr->out_queue_size++;
828 if (l_ptr->out_queue_size > l_ptr->stats.max_queue_sz)
829 l_ptr->stats.max_queue_sz = l_ptr->out_queue_size;
833 * tipc_link_send_buf() is the 'full path' for messages, called from
834 * inside TIPC when the 'fast path' in tipc_send_buf
835 * has failed, and from link_send()
838 int tipc_link_send_buf(struct link *l_ptr, struct sk_buff *buf)
840 struct tipc_msg *msg = buf_msg(buf);
841 u32 size = msg_size(msg);
842 u32 dsz = msg_data_sz(msg);
843 u32 queue_size = l_ptr->out_queue_size;
844 u32 imp = tipc_msg_tot_importance(msg);
845 u32 queue_limit = l_ptr->queue_limit[imp];
846 u32 max_packet = l_ptr->max_pkt;
848 msg_set_prevnode(msg, tipc_own_addr); /* If routed message */
850 /* Match msg importance against queue limits: */
852 if (unlikely(queue_size >= queue_limit)) {
853 if (imp <= TIPC_CRITICAL_IMPORTANCE) {
854 return link_schedule_port(l_ptr, msg_origport(msg),
858 if (imp > CONN_MANAGER) {
859 warn("Resetting link <%s>, send queue full", l_ptr->name);
860 tipc_link_reset(l_ptr);
865 /* Fragmentation needed ? */
867 if (size > max_packet)
868 return link_send_long_buf(l_ptr, buf);
870 /* Packet can be queued or sent: */
872 if (likely(!tipc_bearer_congested(l_ptr->b_ptr, l_ptr) &&
873 !link_congested(l_ptr))) {
874 link_add_to_outqueue(l_ptr, buf, msg);
876 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr))) {
877 l_ptr->unacked_window = 0;
879 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
880 l_ptr->stats.bearer_congs++;
881 l_ptr->next_out = buf;
885 /* Congestion: can message be bundled ?: */
887 if ((msg_user(msg) != CHANGEOVER_PROTOCOL) &&
888 (msg_user(msg) != MSG_FRAGMENTER)) {
890 /* Try adding message to an existing bundle */
892 if (l_ptr->next_out &&
893 link_bundle_buf(l_ptr, l_ptr->last_out, buf)) {
894 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
898 /* Try creating a new bundle */
900 if (size <= max_packet * 2 / 3) {
901 struct sk_buff *bundler = tipc_buf_acquire(max_packet);
902 struct tipc_msg bundler_hdr;
905 tipc_msg_init(&bundler_hdr, MSG_BUNDLER, OPEN_MSG,
906 INT_H_SIZE, l_ptr->addr);
907 skb_copy_to_linear_data(bundler, &bundler_hdr,
909 skb_trim(bundler, INT_H_SIZE);
910 link_bundle_buf(l_ptr, bundler, buf);
913 l_ptr->stats.sent_bundles++;
917 if (!l_ptr->next_out)
918 l_ptr->next_out = buf;
919 link_add_to_outqueue(l_ptr, buf, msg);
920 tipc_bearer_resolve_congestion(l_ptr->b_ptr, l_ptr);
925 * tipc_link_send(): same as tipc_link_send_buf(), but the link to use has
926 * not been selected yet, and the the owner node is not locked
927 * Called by TIPC internal users, e.g. the name distributor
930 int tipc_link_send(struct sk_buff *buf, u32 dest, u32 selector)
933 struct tipc_node *n_ptr;
934 int res = -ELINKCONG;
936 read_lock_bh(&tipc_net_lock);
937 n_ptr = tipc_node_find(dest);
939 tipc_node_lock(n_ptr);
940 l_ptr = n_ptr->active_links[selector & 1];
942 res = tipc_link_send_buf(l_ptr, buf);
945 tipc_node_unlock(n_ptr);
949 read_unlock_bh(&tipc_net_lock);
954 * link_send_buf_fast: Entry for data messages where the
955 * destination link is known and the header is complete,
956 * inclusive total message length. Very time critical.
957 * Link is locked. Returns user data length.
960 static int link_send_buf_fast(struct link *l_ptr, struct sk_buff *buf,
963 struct tipc_msg *msg = buf_msg(buf);
964 int res = msg_data_sz(msg);
966 if (likely(!link_congested(l_ptr))) {
967 if (likely(msg_size(msg) <= l_ptr->max_pkt)) {
968 if (likely(list_empty(&l_ptr->b_ptr->cong_links))) {
969 link_add_to_outqueue(l_ptr, buf, msg);
970 if (likely(tipc_bearer_send(l_ptr->b_ptr, buf,
971 &l_ptr->media_addr))) {
972 l_ptr->unacked_window = 0;
975 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
976 l_ptr->stats.bearer_congs++;
977 l_ptr->next_out = buf;
981 *used_max_pkt = l_ptr->max_pkt;
983 return tipc_link_send_buf(l_ptr, buf); /* All other cases */
987 * tipc_send_buf_fast: Entry for data messages where the
988 * destination node is known and the header is complete,
989 * inclusive total message length.
990 * Returns user data length.
992 int tipc_send_buf_fast(struct sk_buff *buf, u32 destnode)
995 struct tipc_node *n_ptr;
997 u32 selector = msg_origport(buf_msg(buf)) & 1;
1000 if (destnode == tipc_own_addr)
1001 return tipc_port_recv_msg(buf);
1003 read_lock_bh(&tipc_net_lock);
1004 n_ptr = tipc_node_find(destnode);
1005 if (likely(n_ptr)) {
1006 tipc_node_lock(n_ptr);
1007 l_ptr = n_ptr->active_links[selector];
1008 if (likely(l_ptr)) {
1009 res = link_send_buf_fast(l_ptr, buf, &dummy);
1010 tipc_node_unlock(n_ptr);
1011 read_unlock_bh(&tipc_net_lock);
1014 tipc_node_unlock(n_ptr);
1016 read_unlock_bh(&tipc_net_lock);
1017 res = msg_data_sz(buf_msg(buf));
1018 tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1024 * tipc_link_send_sections_fast: Entry for messages where the
1025 * destination processor is known and the header is complete,
1026 * except for total message length.
1027 * Returns user data length or errno.
1029 int tipc_link_send_sections_fast(struct tipc_port *sender,
1030 struct iovec const *msg_sect,
1034 struct tipc_msg *hdr = &sender->phdr;
1036 struct sk_buff *buf;
1037 struct tipc_node *node;
1039 u32 selector = msg_origport(hdr) & 1;
1043 * Try building message using port's max_pkt hint.
1044 * (Must not hold any locks while building message.)
1047 res = tipc_msg_build(hdr, msg_sect, num_sect, sender->max_pkt,
1048 !sender->user_port, &buf);
1050 read_lock_bh(&tipc_net_lock);
1051 node = tipc_node_find(destaddr);
1053 tipc_node_lock(node);
1054 l_ptr = node->active_links[selector];
1055 if (likely(l_ptr)) {
1057 res = link_send_buf_fast(l_ptr, buf,
1059 if (unlikely(res < 0))
1062 tipc_node_unlock(node);
1063 read_unlock_bh(&tipc_net_lock);
1067 /* Exit if build request was invalid */
1069 if (unlikely(res < 0))
1072 /* Exit if link (or bearer) is congested */
1074 if (link_congested(l_ptr) ||
1075 !list_empty(&l_ptr->b_ptr->cong_links)) {
1076 res = link_schedule_port(l_ptr,
1082 * Message size exceeds max_pkt hint; update hint,
1083 * then re-try fast path or fragment the message
1086 sender->max_pkt = l_ptr->max_pkt;
1087 tipc_node_unlock(node);
1088 read_unlock_bh(&tipc_net_lock);
1091 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
1094 return link_send_sections_long(sender, msg_sect,
1095 num_sect, destaddr);
1097 tipc_node_unlock(node);
1099 read_unlock_bh(&tipc_net_lock);
1101 /* Couldn't find a link to the destination node */
1104 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1106 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1112 * link_send_sections_long(): Entry for long messages where the
1113 * destination node is known and the header is complete,
1114 * inclusive total message length.
1115 * Link and bearer congestion status have been checked to be ok,
1116 * and are ignored if they change.
1118 * Note that fragments do not use the full link MTU so that they won't have
1119 * to undergo refragmentation if link changeover causes them to be sent
1120 * over another link with an additional tunnel header added as prefix.
1121 * (Refragmentation will still occur if the other link has a smaller MTU.)
1123 * Returns user data length or errno.
1125 static int link_send_sections_long(struct tipc_port *sender,
1126 struct iovec const *msg_sect,
1131 struct tipc_node *node;
1132 struct tipc_msg *hdr = &sender->phdr;
1133 u32 dsz = msg_data_sz(hdr);
1134 u32 max_pkt, fragm_sz, rest;
1135 struct tipc_msg fragm_hdr;
1136 struct sk_buff *buf, *buf_chain, *prev;
1137 u32 fragm_crs, fragm_rest, hsz, sect_rest;
1138 const unchar *sect_crs;
1144 max_pkt = sender->max_pkt - INT_H_SIZE;
1145 /* leave room for tunnel header in case of link changeover */
1146 fragm_sz = max_pkt - INT_H_SIZE;
1147 /* leave room for fragmentation header in each fragment */
1155 /* Prepare reusable fragment header: */
1157 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
1158 INT_H_SIZE, msg_destnode(hdr));
1159 msg_set_link_selector(&fragm_hdr, sender->ref);
1160 msg_set_size(&fragm_hdr, max_pkt);
1161 msg_set_fragm_no(&fragm_hdr, 1);
1163 /* Prepare header of first fragment: */
1165 buf_chain = buf = tipc_buf_acquire(max_pkt);
1169 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
1170 hsz = msg_hdr_sz(hdr);
1171 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, hdr, hsz);
1173 /* Chop up message: */
1175 fragm_crs = INT_H_SIZE + hsz;
1176 fragm_rest = fragm_sz - hsz;
1178 do { /* For all sections */
1182 sect_rest = msg_sect[++curr_sect].iov_len;
1183 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base;
1186 if (sect_rest < fragm_rest)
1191 if (likely(!sender->user_port)) {
1192 if (copy_from_user(buf->data + fragm_crs, sect_crs, sz)) {
1194 for (; buf_chain; buf_chain = buf) {
1195 buf = buf_chain->next;
1196 buf_discard(buf_chain);
1201 skb_copy_to_linear_data_offset(buf, fragm_crs,
1209 if (!fragm_rest && rest) {
1211 /* Initiate new fragment: */
1212 if (rest <= fragm_sz) {
1214 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
1216 msg_set_type(&fragm_hdr, FRAGMENT);
1218 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
1219 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
1221 buf = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
1227 skb_copy_to_linear_data(buf, &fragm_hdr, INT_H_SIZE);
1228 fragm_crs = INT_H_SIZE;
1229 fragm_rest = fragm_sz;
1234 * Now we have a buffer chain. Select a link and check
1235 * that packet size is still OK
1237 node = tipc_node_find(destaddr);
1239 tipc_node_lock(node);
1240 l_ptr = node->active_links[sender->ref & 1];
1242 tipc_node_unlock(node);
1245 if (l_ptr->max_pkt < max_pkt) {
1246 sender->max_pkt = l_ptr->max_pkt;
1247 tipc_node_unlock(node);
1248 for (; buf_chain; buf_chain = buf) {
1249 buf = buf_chain->next;
1250 buf_discard(buf_chain);
1256 for (; buf_chain; buf_chain = buf) {
1257 buf = buf_chain->next;
1258 buf_discard(buf_chain);
1260 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect,
1264 /* Append whole chain to send queue: */
1267 l_ptr->long_msg_seq_no = mod(l_ptr->long_msg_seq_no + 1);
1268 if (!l_ptr->next_out)
1269 l_ptr->next_out = buf_chain;
1270 l_ptr->stats.sent_fragmented++;
1272 struct sk_buff *next = buf->next;
1273 struct tipc_msg *msg = buf_msg(buf);
1275 l_ptr->stats.sent_fragments++;
1276 msg_set_long_msgno(msg, l_ptr->long_msg_seq_no);
1277 link_add_to_outqueue(l_ptr, buf, msg);
1281 /* Send it, if possible: */
1283 tipc_link_push_queue(l_ptr);
1284 tipc_node_unlock(node);
1289 * tipc_link_push_packet: Push one unsent packet to the media
1291 u32 tipc_link_push_packet(struct link *l_ptr)
1293 struct sk_buff *buf = l_ptr->first_out;
1294 u32 r_q_size = l_ptr->retransm_queue_size;
1295 u32 r_q_head = l_ptr->retransm_queue_head;
1297 /* Step to position where retransmission failed, if any, */
1298 /* consider that buffers may have been released in meantime */
1300 if (r_q_size && buf) {
1301 u32 last = lesser(mod(r_q_head + r_q_size),
1302 link_last_sent(l_ptr));
1303 u32 first = msg_seqno(buf_msg(buf));
1305 while (buf && less(first, r_q_head)) {
1306 first = mod(first + 1);
1309 l_ptr->retransm_queue_head = r_q_head = first;
1310 l_ptr->retransm_queue_size = r_q_size = mod(last - first);
1313 /* Continue retransmission now, if there is anything: */
1315 if (r_q_size && buf) {
1316 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1317 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
1318 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1319 l_ptr->retransm_queue_head = mod(++r_q_head);
1320 l_ptr->retransm_queue_size = --r_q_size;
1321 l_ptr->stats.retransmitted++;
1324 l_ptr->stats.bearer_congs++;
1329 /* Send deferred protocol message, if any: */
1331 buf = l_ptr->proto_msg_queue;
1333 msg_set_ack(buf_msg(buf), mod(l_ptr->next_in_no - 1));
1334 msg_set_bcast_ack(buf_msg(buf), l_ptr->owner->bclink.last_in);
1335 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1336 l_ptr->unacked_window = 0;
1338 l_ptr->proto_msg_queue = NULL;
1341 l_ptr->stats.bearer_congs++;
1346 /* Send one deferred data message, if send window not full: */
1348 buf = l_ptr->next_out;
1350 struct tipc_msg *msg = buf_msg(buf);
1351 u32 next = msg_seqno(msg);
1352 u32 first = msg_seqno(buf_msg(l_ptr->first_out));
1354 if (mod(next - first) < l_ptr->queue_limit[0]) {
1355 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1356 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1357 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1358 if (msg_user(msg) == MSG_BUNDLER)
1359 msg_set_type(msg, CLOSED_MSG);
1360 l_ptr->next_out = buf->next;
1363 l_ptr->stats.bearer_congs++;
1368 return PUSH_FINISHED;
1372 * push_queue(): push out the unsent messages of a link where
1373 * congestion has abated. Node is locked
1375 void tipc_link_push_queue(struct link *l_ptr)
1379 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr))
1383 res = tipc_link_push_packet(l_ptr);
1386 if (res == PUSH_FAILED)
1387 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1390 static void link_reset_all(unsigned long addr)
1392 struct tipc_node *n_ptr;
1393 char addr_string[16];
1396 read_lock_bh(&tipc_net_lock);
1397 n_ptr = tipc_node_find((u32)addr);
1399 read_unlock_bh(&tipc_net_lock);
1400 return; /* node no longer exists */
1403 tipc_node_lock(n_ptr);
1405 warn("Resetting all links to %s\n",
1406 tipc_addr_string_fill(addr_string, n_ptr->addr));
1408 for (i = 0; i < MAX_BEARERS; i++) {
1409 if (n_ptr->links[i]) {
1410 link_print(n_ptr->links[i], "Resetting link\n");
1411 tipc_link_reset(n_ptr->links[i]);
1415 tipc_node_unlock(n_ptr);
1416 read_unlock_bh(&tipc_net_lock);
1419 static void link_retransmit_failure(struct link *l_ptr, struct sk_buff *buf)
1421 struct tipc_msg *msg = buf_msg(buf);
1423 warn("Retransmission failure on link <%s>\n", l_ptr->name);
1427 /* Handle failure on standard link */
1429 link_print(l_ptr, "Resetting link\n");
1430 tipc_link_reset(l_ptr);
1434 /* Handle failure on broadcast link */
1436 struct tipc_node *n_ptr;
1437 char addr_string[16];
1439 info("Msg seq number: %u, ", msg_seqno(msg));
1440 info("Outstanding acks: %lu\n",
1441 (unsigned long) TIPC_SKB_CB(buf)->handle);
1443 n_ptr = tipc_bclink_retransmit_to();
1444 tipc_node_lock(n_ptr);
1446 tipc_addr_string_fill(addr_string, n_ptr->addr);
1447 info("Multicast link info for %s\n", addr_string);
1448 info("Supported: %d, ", n_ptr->bclink.supported);
1449 info("Acked: %u\n", n_ptr->bclink.acked);
1450 info("Last in: %u, ", n_ptr->bclink.last_in);
1451 info("Gap after: %u, ", n_ptr->bclink.gap_after);
1452 info("Gap to: %u\n", n_ptr->bclink.gap_to);
1453 info("Nack sync: %u\n\n", n_ptr->bclink.nack_sync);
1455 tipc_k_signal((Handler)link_reset_all, (unsigned long)n_ptr->addr);
1457 tipc_node_unlock(n_ptr);
1459 l_ptr->stale_count = 0;
1463 void tipc_link_retransmit(struct link *l_ptr, struct sk_buff *buf,
1466 struct tipc_msg *msg;
1473 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
1474 if (l_ptr->retransm_queue_size == 0) {
1475 l_ptr->retransm_queue_head = msg_seqno(msg);
1476 l_ptr->retransm_queue_size = retransmits;
1478 err("Unexpected retransmit on link %s (qsize=%d)\n",
1479 l_ptr->name, l_ptr->retransm_queue_size);
1483 /* Detect repeated retransmit failures on uncongested bearer */
1485 if (l_ptr->last_retransmitted == msg_seqno(msg)) {
1486 if (++l_ptr->stale_count > 100) {
1487 link_retransmit_failure(l_ptr, buf);
1491 l_ptr->last_retransmitted = msg_seqno(msg);
1492 l_ptr->stale_count = 1;
1496 while (retransmits && (buf != l_ptr->next_out) && buf) {
1498 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1499 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
1500 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1503 l_ptr->stats.retransmitted++;
1505 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1506 l_ptr->stats.bearer_congs++;
1507 l_ptr->retransm_queue_head = msg_seqno(buf_msg(buf));
1508 l_ptr->retransm_queue_size = retransmits;
1513 l_ptr->retransm_queue_head = l_ptr->retransm_queue_size = 0;
1517 * link_insert_deferred_queue - insert deferred messages back into receive chain
1520 static struct sk_buff *link_insert_deferred_queue(struct link *l_ptr,
1521 struct sk_buff *buf)
1525 if (l_ptr->oldest_deferred_in == NULL)
1528 seq_no = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1529 if (seq_no == mod(l_ptr->next_in_no)) {
1530 l_ptr->newest_deferred_in->next = buf;
1531 buf = l_ptr->oldest_deferred_in;
1532 l_ptr->oldest_deferred_in = NULL;
1533 l_ptr->deferred_inqueue_sz = 0;
1539 * link_recv_buf_validate - validate basic format of received message
1541 * This routine ensures a TIPC message has an acceptable header, and at least
1542 * as much data as the header indicates it should. The routine also ensures
1543 * that the entire message header is stored in the main fragment of the message
1544 * buffer, to simplify future access to message header fields.
1546 * Note: Having extra info present in the message header or data areas is OK.
1547 * TIPC will ignore the excess, under the assumption that it is optional info
1548 * introduced by a later release of the protocol.
1551 static int link_recv_buf_validate(struct sk_buff *buf)
1553 static u32 min_data_hdr_size[8] = {
1554 SHORT_H_SIZE, MCAST_H_SIZE, LONG_H_SIZE, DIR_MSG_H_SIZE,
1555 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1558 struct tipc_msg *msg;
1564 if (unlikely(buf->len < MIN_H_SIZE))
1567 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1571 if (unlikely(msg_version(msg) != TIPC_VERSION))
1574 size = msg_size(msg);
1575 hdr_size = msg_hdr_sz(msg);
1576 min_hdr_size = msg_isdata(msg) ?
1577 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE;
1579 if (unlikely((hdr_size < min_hdr_size) ||
1580 (size < hdr_size) ||
1581 (buf->len < size) ||
1582 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1585 return pskb_may_pull(buf, hdr_size);
1589 * tipc_recv_msg - process TIPC messages arriving from off-node
1590 * @head: pointer to message buffer chain
1591 * @tb_ptr: pointer to bearer message arrived on
1593 * Invoked with no locks held. Bearer pointer must point to a valid bearer
1594 * structure (i.e. cannot be NULL), but bearer can be inactive.
1597 void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1599 read_lock_bh(&tipc_net_lock);
1601 struct tipc_node *n_ptr;
1603 struct sk_buff *crs;
1604 struct sk_buff *buf = head;
1605 struct tipc_msg *msg;
1613 /* Ensure bearer is still enabled */
1615 if (unlikely(!b_ptr->active))
1618 /* Ensure message is well-formed */
1620 if (unlikely(!link_recv_buf_validate(buf)))
1623 /* Ensure message data is a single contiguous unit */
1625 if (unlikely(buf_linearize(buf)))
1628 /* Handle arrival of a non-unicast link message */
1632 if (unlikely(msg_non_seq(msg))) {
1633 if (msg_user(msg) == LINK_CONFIG)
1634 tipc_disc_recv_msg(buf, b_ptr);
1636 tipc_bclink_recv_pkt(buf);
1640 if (unlikely(!msg_short(msg) &&
1641 (msg_destnode(msg) != tipc_own_addr)))
1644 /* Discard non-routeable messages destined for another node */
1646 if (unlikely(!msg_isdata(msg) &&
1647 (msg_destnode(msg) != tipc_own_addr))) {
1648 if ((msg_user(msg) != CONN_MANAGER) &&
1649 (msg_user(msg) != MSG_FRAGMENTER))
1653 /* Locate neighboring node that sent message */
1655 n_ptr = tipc_node_find(msg_prevnode(msg));
1656 if (unlikely(!n_ptr))
1658 tipc_node_lock(n_ptr);
1660 /* Don't talk to neighbor during cleanup after last session */
1662 if (n_ptr->cleanup_required) {
1663 tipc_node_unlock(n_ptr);
1667 /* Locate unicast link endpoint that should handle message */
1669 l_ptr = n_ptr->links[b_ptr->identity];
1670 if (unlikely(!l_ptr)) {
1671 tipc_node_unlock(n_ptr);
1675 /* Validate message sequence number info */
1677 seq_no = msg_seqno(msg);
1678 ackd = msg_ack(msg);
1680 /* Release acked messages */
1682 if (less(n_ptr->bclink.acked, msg_bcast_ack(msg))) {
1683 if (tipc_node_is_up(n_ptr) && n_ptr->bclink.supported)
1684 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
1687 crs = l_ptr->first_out;
1688 while ((crs != l_ptr->next_out) &&
1689 less_eq(msg_seqno(buf_msg(crs)), ackd)) {
1690 struct sk_buff *next = crs->next;
1697 l_ptr->first_out = crs;
1698 l_ptr->out_queue_size -= released;
1701 /* Try sending any messages link endpoint has pending */
1703 if (unlikely(l_ptr->next_out))
1704 tipc_link_push_queue(l_ptr);
1705 if (unlikely(!list_empty(&l_ptr->waiting_ports)))
1706 tipc_link_wakeup_ports(l_ptr, 0);
1707 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1708 l_ptr->stats.sent_acks++;
1709 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1712 /* Now (finally!) process the incoming message */
1715 if (likely(link_working_working(l_ptr))) {
1716 if (likely(seq_no == mod(l_ptr->next_in_no))) {
1717 l_ptr->next_in_no++;
1718 if (unlikely(l_ptr->oldest_deferred_in))
1719 head = link_insert_deferred_queue(l_ptr,
1721 if (likely(msg_is_dest(msg, tipc_own_addr))) {
1723 if (likely(msg_isdata(msg))) {
1724 tipc_node_unlock(n_ptr);
1725 tipc_port_recv_msg(buf);
1728 switch (msg_user(msg)) {
1730 l_ptr->stats.recv_bundles++;
1731 l_ptr->stats.recv_bundled +=
1733 tipc_node_unlock(n_ptr);
1734 tipc_link_recv_bundle(buf);
1736 case ROUTE_DISTRIBUTOR:
1737 tipc_node_unlock(n_ptr);
1740 case NAME_DISTRIBUTOR:
1741 tipc_node_unlock(n_ptr);
1742 tipc_named_recv(buf);
1745 tipc_node_unlock(n_ptr);
1746 tipc_port_recv_proto_msg(buf);
1748 case MSG_FRAGMENTER:
1749 l_ptr->stats.recv_fragments++;
1750 if (tipc_link_recv_fragment(&l_ptr->defragm_buf,
1752 l_ptr->stats.recv_fragmented++;
1756 case CHANGEOVER_PROTOCOL:
1757 type = msg_type(msg);
1758 if (link_recv_changeover_msg(&l_ptr, &buf)) {
1760 seq_no = msg_seqno(msg);
1761 if (type == ORIGINAL_MSG)
1763 goto protocol_check;
1768 tipc_node_unlock(n_ptr);
1769 tipc_net_route_msg(buf);
1772 link_handle_out_of_seq_msg(l_ptr, buf);
1773 head = link_insert_deferred_queue(l_ptr, head);
1774 tipc_node_unlock(n_ptr);
1778 if (msg_user(msg) == LINK_PROTOCOL) {
1779 link_recv_proto_msg(l_ptr, buf);
1780 head = link_insert_deferred_queue(l_ptr, head);
1781 tipc_node_unlock(n_ptr);
1784 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1786 if (link_working_working(l_ptr)) {
1787 /* Re-insert in front of queue */
1790 tipc_node_unlock(n_ptr);
1793 tipc_node_unlock(n_ptr);
1797 read_unlock_bh(&tipc_net_lock);
1801 * link_defer_buf(): Sort a received out-of-sequence packet
1802 * into the deferred reception queue.
1803 * Returns the increase of the queue length,i.e. 0 or 1
1806 u32 tipc_link_defer_pkt(struct sk_buff **head,
1807 struct sk_buff **tail,
1808 struct sk_buff *buf)
1810 struct sk_buff *prev = NULL;
1811 struct sk_buff *crs = *head;
1812 u32 seq_no = msg_seqno(buf_msg(buf));
1817 if (*head == NULL) {
1818 *head = *tail = buf;
1823 if (less(msg_seqno(buf_msg(*tail)), seq_no)) {
1824 (*tail)->next = buf;
1829 /* Scan through queue and sort it in */
1831 struct tipc_msg *msg = buf_msg(crs);
1833 if (less(seq_no, msg_seqno(msg))) {
1841 if (seq_no == msg_seqno(msg))
1847 /* Message is a duplicate of an existing message */
1854 * link_handle_out_of_seq_msg - handle arrival of out-of-sequence packet
1857 static void link_handle_out_of_seq_msg(struct link *l_ptr,
1858 struct sk_buff *buf)
1860 u32 seq_no = msg_seqno(buf_msg(buf));
1862 if (likely(msg_user(buf_msg(buf)) == LINK_PROTOCOL)) {
1863 link_recv_proto_msg(l_ptr, buf);
1867 /* Record OOS packet arrival (force mismatch on next timeout) */
1869 l_ptr->checkpoint--;
1872 * Discard packet if a duplicate; otherwise add it to deferred queue
1873 * and notify peer of gap as per protocol specification
1876 if (less(seq_no, mod(l_ptr->next_in_no))) {
1877 l_ptr->stats.duplicates++;
1882 if (tipc_link_defer_pkt(&l_ptr->oldest_deferred_in,
1883 &l_ptr->newest_deferred_in, buf)) {
1884 l_ptr->deferred_inqueue_sz++;
1885 l_ptr->stats.deferred_recv++;
1886 if ((l_ptr->deferred_inqueue_sz % 16) == 1)
1887 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1889 l_ptr->stats.duplicates++;
1893 * Send protocol message to the other endpoint.
1895 void tipc_link_send_proto_msg(struct link *l_ptr, u32 msg_typ, int probe_msg,
1896 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu)
1898 struct sk_buff *buf = NULL;
1899 struct tipc_msg *msg = l_ptr->pmsg;
1900 u32 msg_size = sizeof(l_ptr->proto_msg);
1902 if (link_blocked(l_ptr))
1904 msg_set_type(msg, msg_typ);
1905 msg_set_net_plane(msg, l_ptr->b_ptr->net_plane);
1906 msg_set_bcast_ack(msg, mod(l_ptr->owner->bclink.last_in));
1907 msg_set_last_bcast(msg, tipc_bclink_get_last_sent());
1909 if (msg_typ == STATE_MSG) {
1910 u32 next_sent = mod(l_ptr->next_out_no);
1912 if (!tipc_link_is_up(l_ptr))
1914 if (l_ptr->next_out)
1915 next_sent = msg_seqno(buf_msg(l_ptr->next_out));
1916 msg_set_next_sent(msg, next_sent);
1917 if (l_ptr->oldest_deferred_in) {
1918 u32 rec = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
1919 gap = mod(rec - mod(l_ptr->next_in_no));
1921 msg_set_seq_gap(msg, gap);
1923 l_ptr->stats.sent_nacks++;
1924 msg_set_link_tolerance(msg, tolerance);
1925 msg_set_linkprio(msg, priority);
1926 msg_set_max_pkt(msg, ack_mtu);
1927 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1928 msg_set_probe(msg, probe_msg != 0);
1930 u32 mtu = l_ptr->max_pkt;
1932 if ((mtu < l_ptr->max_pkt_target) &&
1933 link_working_working(l_ptr) &&
1934 l_ptr->fsm_msg_cnt) {
1935 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1936 if (l_ptr->max_pkt_probes == 10) {
1937 l_ptr->max_pkt_target = (msg_size - 4);
1938 l_ptr->max_pkt_probes = 0;
1939 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1941 l_ptr->max_pkt_probes++;
1944 l_ptr->stats.sent_probes++;
1946 l_ptr->stats.sent_states++;
1947 } else { /* RESET_MSG or ACTIVATE_MSG */
1948 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1));
1949 msg_set_seq_gap(msg, 0);
1950 msg_set_next_sent(msg, 1);
1951 msg_set_probe(msg, 0);
1952 msg_set_link_tolerance(msg, l_ptr->tolerance);
1953 msg_set_linkprio(msg, l_ptr->priority);
1954 msg_set_max_pkt(msg, l_ptr->max_pkt_target);
1957 if (tipc_node_has_redundant_links(l_ptr->owner))
1958 msg_set_redundant_link(msg);
1960 msg_clear_redundant_link(msg);
1961 msg_set_linkprio(msg, l_ptr->priority);
1963 /* Ensure sequence number will not fit : */
1965 msg_set_seqno(msg, mod(l_ptr->next_out_no + (0xffff/2)));
1969 if (tipc_bearer_congested(l_ptr->b_ptr, l_ptr)) {
1970 if (!l_ptr->proto_msg_queue) {
1971 l_ptr->proto_msg_queue =
1972 tipc_buf_acquire(sizeof(l_ptr->proto_msg));
1974 buf = l_ptr->proto_msg_queue;
1977 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
1981 /* Message can be sent */
1983 buf = tipc_buf_acquire(msg_size);
1987 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
1988 msg_set_size(buf_msg(buf), msg_size);
1990 if (tipc_bearer_send(l_ptr->b_ptr, buf, &l_ptr->media_addr)) {
1991 l_ptr->unacked_window = 0;
1996 /* New congestion */
1997 tipc_bearer_schedule(l_ptr->b_ptr, l_ptr);
1998 l_ptr->proto_msg_queue = buf;
1999 l_ptr->stats.bearer_congs++;
2003 * Receive protocol message :
2004 * Note that network plane id propagates through the network, and may
2005 * change at any time. The node with lowest address rules
2008 static void link_recv_proto_msg(struct link *l_ptr, struct sk_buff *buf)
2014 struct tipc_msg *msg = buf_msg(buf);
2016 if (link_blocked(l_ptr))
2019 /* record unnumbered packet arrival (force mismatch on next timeout) */
2021 l_ptr->checkpoint--;
2023 if (l_ptr->b_ptr->net_plane != msg_net_plane(msg))
2024 if (tipc_own_addr > msg_prevnode(msg))
2025 l_ptr->b_ptr->net_plane = msg_net_plane(msg);
2027 l_ptr->owner->permit_changeover = msg_redundant_link(msg);
2029 switch (msg_type(msg)) {
2032 if (!link_working_unknown(l_ptr) &&
2033 (l_ptr->peer_session != INVALID_SESSION)) {
2034 if (msg_session(msg) == l_ptr->peer_session)
2035 break; /* duplicate: ignore */
2039 /* Update link settings according other endpoint's values */
2041 strcpy((strrchr(l_ptr->name, ':') + 1), (char *)msg_data(msg));
2043 msg_tol = msg_link_tolerance(msg);
2044 if (msg_tol > l_ptr->tolerance)
2045 link_set_supervision_props(l_ptr, msg_tol);
2047 if (msg_linkprio(msg) > l_ptr->priority)
2048 l_ptr->priority = msg_linkprio(msg);
2050 max_pkt_info = msg_max_pkt(msg);
2052 if (max_pkt_info < l_ptr->max_pkt_target)
2053 l_ptr->max_pkt_target = max_pkt_info;
2054 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
2055 l_ptr->max_pkt = l_ptr->max_pkt_target;
2057 l_ptr->max_pkt = l_ptr->max_pkt_target;
2059 l_ptr->owner->bclink.supported = (max_pkt_info != 0);
2061 link_state_event(l_ptr, msg_type(msg));
2063 l_ptr->peer_session = msg_session(msg);
2064 l_ptr->peer_bearer_id = msg_bearer_id(msg);
2066 /* Synchronize broadcast sequence numbers */
2067 if (!tipc_node_has_redundant_links(l_ptr->owner))
2068 l_ptr->owner->bclink.last_in = mod(msg_last_bcast(msg));
2072 msg_tol = msg_link_tolerance(msg);
2074 link_set_supervision_props(l_ptr, msg_tol);
2076 if (msg_linkprio(msg) &&
2077 (msg_linkprio(msg) != l_ptr->priority)) {
2078 warn("Resetting link <%s>, priority change %u->%u\n",
2079 l_ptr->name, l_ptr->priority, msg_linkprio(msg));
2080 l_ptr->priority = msg_linkprio(msg);
2081 tipc_link_reset(l_ptr); /* Enforce change to take effect */
2084 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
2085 l_ptr->stats.recv_states++;
2086 if (link_reset_unknown(l_ptr))
2089 if (less_eq(mod(l_ptr->next_in_no), msg_next_sent(msg))) {
2090 rec_gap = mod(msg_next_sent(msg) -
2091 mod(l_ptr->next_in_no));
2094 max_pkt_ack = msg_max_pkt(msg);
2095 if (max_pkt_ack > l_ptr->max_pkt) {
2096 l_ptr->max_pkt = max_pkt_ack;
2097 l_ptr->max_pkt_probes = 0;
2101 if (msg_probe(msg)) {
2102 l_ptr->stats.recv_probes++;
2103 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
2104 max_pkt_ack = msg_size(msg);
2107 /* Protocol message before retransmits, reduce loss risk */
2109 tipc_bclink_check_gap(l_ptr->owner, msg_last_bcast(msg));
2111 if (rec_gap || (msg_probe(msg))) {
2112 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2113 0, rec_gap, 0, 0, max_pkt_ack);
2115 if (msg_seq_gap(msg)) {
2116 l_ptr->stats.recv_nacks++;
2117 tipc_link_retransmit(l_ptr, l_ptr->first_out,
2128 * tipc_link_tunnel(): Send one message via a link belonging to
2129 * another bearer. Owner node is locked.
2131 static void tipc_link_tunnel(struct link *l_ptr,
2132 struct tipc_msg *tunnel_hdr,
2133 struct tipc_msg *msg,
2136 struct link *tunnel;
2137 struct sk_buff *buf;
2138 u32 length = msg_size(msg);
2140 tunnel = l_ptr->owner->active_links[selector & 1];
2141 if (!tipc_link_is_up(tunnel)) {
2142 warn("Link changeover error, "
2143 "tunnel link no longer available\n");
2146 msg_set_size(tunnel_hdr, length + INT_H_SIZE);
2147 buf = tipc_buf_acquire(length + INT_H_SIZE);
2149 warn("Link changeover error, "
2150 "unable to send tunnel msg\n");
2153 skb_copy_to_linear_data(buf, tunnel_hdr, INT_H_SIZE);
2154 skb_copy_to_linear_data_offset(buf, INT_H_SIZE, msg, length);
2155 tipc_link_send_buf(tunnel, buf);
2161 * changeover(): Send whole message queue via the remaining link
2162 * Owner node is locked.
2165 void tipc_link_changeover(struct link *l_ptr)
2167 u32 msgcount = l_ptr->out_queue_size;
2168 struct sk_buff *crs = l_ptr->first_out;
2169 struct link *tunnel = l_ptr->owner->active_links[0];
2170 struct tipc_msg tunnel_hdr;
2176 if (!l_ptr->owner->permit_changeover) {
2177 warn("Link changeover error, "
2178 "peer did not permit changeover\n");
2182 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2183 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr);
2184 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2185 msg_set_msgcnt(&tunnel_hdr, msgcount);
2187 if (!l_ptr->first_out) {
2188 struct sk_buff *buf;
2190 buf = tipc_buf_acquire(INT_H_SIZE);
2192 skb_copy_to_linear_data(buf, &tunnel_hdr, INT_H_SIZE);
2193 msg_set_size(&tunnel_hdr, INT_H_SIZE);
2194 tipc_link_send_buf(tunnel, buf);
2196 warn("Link changeover error, "
2197 "unable to send changeover msg\n");
2202 split_bundles = (l_ptr->owner->active_links[0] !=
2203 l_ptr->owner->active_links[1]);
2206 struct tipc_msg *msg = buf_msg(crs);
2208 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
2209 struct tipc_msg *m = msg_get_wrapped(msg);
2210 unchar *pos = (unchar *)m;
2212 msgcount = msg_msgcnt(msg);
2213 while (msgcount--) {
2214 msg_set_seqno(m, msg_seqno(msg));
2215 tipc_link_tunnel(l_ptr, &tunnel_hdr, m,
2216 msg_link_selector(m));
2217 pos += align(msg_size(m));
2218 m = (struct tipc_msg *)pos;
2221 tipc_link_tunnel(l_ptr, &tunnel_hdr, msg,
2222 msg_link_selector(msg));
2228 void tipc_link_send_duplicate(struct link *l_ptr, struct link *tunnel)
2230 struct sk_buff *iter;
2231 struct tipc_msg tunnel_hdr;
2233 tipc_msg_init(&tunnel_hdr, CHANGEOVER_PROTOCOL,
2234 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr);
2235 msg_set_msgcnt(&tunnel_hdr, l_ptr->out_queue_size);
2236 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
2237 iter = l_ptr->first_out;
2239 struct sk_buff *outbuf;
2240 struct tipc_msg *msg = buf_msg(iter);
2241 u32 length = msg_size(msg);
2243 if (msg_user(msg) == MSG_BUNDLER)
2244 msg_set_type(msg, CLOSED_MSG);
2245 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */
2246 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
2247 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
2248 outbuf = tipc_buf_acquire(length + INT_H_SIZE);
2249 if (outbuf == NULL) {
2250 warn("Link changeover error, "
2251 "unable to send duplicate msg\n");
2254 skb_copy_to_linear_data(outbuf, &tunnel_hdr, INT_H_SIZE);
2255 skb_copy_to_linear_data_offset(outbuf, INT_H_SIZE, iter->data,
2257 tipc_link_send_buf(tunnel, outbuf);
2258 if (!tipc_link_is_up(l_ptr))
2267 * buf_extract - extracts embedded TIPC message from another message
2268 * @skb: encapsulating message buffer
2269 * @from_pos: offset to extract from
2271 * Returns a new message buffer containing an embedded message. The
2272 * encapsulating message itself is left unchanged.
2275 static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
2277 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
2278 u32 size = msg_size(msg);
2281 eb = tipc_buf_acquire(size);
2283 skb_copy_to_linear_data(eb, msg, size);
2288 * link_recv_changeover_msg(): Receive tunneled packet sent
2289 * via other link. Node is locked. Return extracted buffer.
2292 static int link_recv_changeover_msg(struct link **l_ptr,
2293 struct sk_buff **buf)
2295 struct sk_buff *tunnel_buf = *buf;
2296 struct link *dest_link;
2297 struct tipc_msg *msg;
2298 struct tipc_msg *tunnel_msg = buf_msg(tunnel_buf);
2299 u32 msg_typ = msg_type(tunnel_msg);
2300 u32 msg_count = msg_msgcnt(tunnel_msg);
2302 dest_link = (*l_ptr)->owner->links[msg_bearer_id(tunnel_msg)];
2305 if (dest_link == *l_ptr) {
2306 err("Unexpected changeover message on link <%s>\n",
2311 msg = msg_get_wrapped(tunnel_msg);
2313 if (msg_typ == DUPLICATE_MSG) {
2314 if (less(msg_seqno(msg), mod(dest_link->next_in_no)))
2316 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2318 warn("Link changeover error, duplicate msg dropped\n");
2321 buf_discard(tunnel_buf);
2325 /* First original message ?: */
2327 if (tipc_link_is_up(dest_link)) {
2328 info("Resetting link <%s>, changeover initiated by peer\n",
2330 tipc_link_reset(dest_link);
2331 dest_link->exp_msg_count = msg_count;
2334 } else if (dest_link->exp_msg_count == START_CHANGEOVER) {
2335 dest_link->exp_msg_count = msg_count;
2340 /* Receive original message */
2342 if (dest_link->exp_msg_count == 0) {
2343 warn("Link switchover error, "
2344 "got too many tunnelled messages\n");
2347 dest_link->exp_msg_count--;
2348 if (less(msg_seqno(msg), dest_link->reset_checkpoint)) {
2351 *buf = buf_extract(tunnel_buf, INT_H_SIZE);
2353 buf_discard(tunnel_buf);
2356 warn("Link changeover error, original msg dropped\n");
2361 buf_discard(tunnel_buf);
2366 * Bundler functionality:
2368 void tipc_link_recv_bundle(struct sk_buff *buf)
2370 u32 msgcount = msg_msgcnt(buf_msg(buf));
2371 u32 pos = INT_H_SIZE;
2372 struct sk_buff *obuf;
2374 while (msgcount--) {
2375 obuf = buf_extract(buf, pos);
2377 warn("Link unable to unbundle message(s)\n");
2380 pos += align(msg_size(buf_msg(obuf)));
2381 tipc_net_route_msg(obuf);
2387 * Fragmentation/defragmentation:
2392 * link_send_long_buf: Entry for buffers needing fragmentation.
2393 * The buffer is complete, inclusive total message length.
2394 * Returns user data length.
2396 static int link_send_long_buf(struct link *l_ptr, struct sk_buff *buf)
2398 struct tipc_msg *inmsg = buf_msg(buf);
2399 struct tipc_msg fragm_hdr;
2400 u32 insize = msg_size(inmsg);
2401 u32 dsz = msg_data_sz(inmsg);
2402 unchar *crs = buf->data;
2404 u32 pack_sz = l_ptr->max_pkt;
2405 u32 fragm_sz = pack_sz - INT_H_SIZE;
2409 if (msg_short(inmsg))
2410 destaddr = l_ptr->addr;
2412 destaddr = msg_destnode(inmsg);
2414 if (msg_routed(inmsg))
2415 msg_set_prevnode(inmsg, tipc_own_addr);
2417 /* Prepare reusable fragment header: */
2419 tipc_msg_init(&fragm_hdr, MSG_FRAGMENTER, FIRST_FRAGMENT,
2420 INT_H_SIZE, destaddr);
2421 msg_set_link_selector(&fragm_hdr, msg_link_selector(inmsg));
2422 msg_set_long_msgno(&fragm_hdr, mod(l_ptr->long_msg_seq_no++));
2423 msg_set_fragm_no(&fragm_hdr, fragm_no);
2424 l_ptr->stats.sent_fragmented++;
2426 /* Chop up message: */
2429 struct sk_buff *fragm;
2431 if (rest <= fragm_sz) {
2433 msg_set_type(&fragm_hdr, LAST_FRAGMENT);
2435 fragm = tipc_buf_acquire(fragm_sz + INT_H_SIZE);
2436 if (fragm == NULL) {
2437 warn("Link unable to fragment message\n");
2441 msg_set_size(&fragm_hdr, fragm_sz + INT_H_SIZE);
2442 skb_copy_to_linear_data(fragm, &fragm_hdr, INT_H_SIZE);
2443 skb_copy_to_linear_data_offset(fragm, INT_H_SIZE, crs,
2445 /* Send queued messages first, if any: */
2447 l_ptr->stats.sent_fragments++;
2448 tipc_link_send_buf(l_ptr, fragm);
2449 if (!tipc_link_is_up(l_ptr))
2451 msg_set_fragm_no(&fragm_hdr, ++fragm_no);
2454 msg_set_type(&fragm_hdr, FRAGMENT);
2462 * A pending message being re-assembled must store certain values
2463 * to handle subsequent fragments correctly. The following functions
2464 * help storing these values in unused, available fields in the
2465 * pending message. This makes dynamic memory allocation unecessary.
2468 static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
2470 msg_set_seqno(buf_msg(buf), seqno);
2473 static u32 get_fragm_size(struct sk_buff *buf)
2475 return msg_ack(buf_msg(buf));
2478 static void set_fragm_size(struct sk_buff *buf, u32 sz)
2480 msg_set_ack(buf_msg(buf), sz);
2483 static u32 get_expected_frags(struct sk_buff *buf)
2485 return msg_bcast_ack(buf_msg(buf));
2488 static void set_expected_frags(struct sk_buff *buf, u32 exp)
2490 msg_set_bcast_ack(buf_msg(buf), exp);
2493 static u32 get_timer_cnt(struct sk_buff *buf)
2495 return msg_reroute_cnt(buf_msg(buf));
2498 static void incr_timer_cnt(struct sk_buff *buf)
2500 msg_incr_reroute_cnt(buf_msg(buf));
2504 * tipc_link_recv_fragment(): Called with node lock on. Returns
2505 * the reassembled buffer if message is complete.
2507 int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb,
2508 struct tipc_msg **m)
2510 struct sk_buff *prev = NULL;
2511 struct sk_buff *fbuf = *fb;
2512 struct tipc_msg *fragm = buf_msg(fbuf);
2513 struct sk_buff *pbuf = *pending;
2514 u32 long_msg_seq_no = msg_long_msgno(fragm);
2518 /* Is there an incomplete message waiting for this fragment? */
2520 while (pbuf && ((msg_seqno(buf_msg(pbuf)) != long_msg_seq_no) ||
2521 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) {
2526 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2527 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2528 u32 msg_sz = msg_size(imsg);
2529 u32 fragm_sz = msg_data_sz(fragm);
2530 u32 exp_fragm_cnt = msg_sz/fragm_sz + !!(msg_sz % fragm_sz);
2531 u32 max = TIPC_MAX_USER_MSG_SIZE + LONG_H_SIZE;
2532 if (msg_type(imsg) == TIPC_MCAST_MSG)
2533 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2534 if (msg_size(imsg) > max) {
2538 pbuf = tipc_buf_acquire(msg_size(imsg));
2540 pbuf->next = *pending;
2542 skb_copy_to_linear_data(pbuf, imsg,
2543 msg_data_sz(fragm));
2544 /* Prepare buffer for subsequent fragments. */
2546 set_long_msg_seqno(pbuf, long_msg_seq_no);
2547 set_fragm_size(pbuf, fragm_sz);
2548 set_expected_frags(pbuf, exp_fragm_cnt - 1);
2550 warn("Link unable to reassemble fragmented message\n");
2554 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2555 u32 dsz = msg_data_sz(fragm);
2556 u32 fsz = get_fragm_size(pbuf);
2557 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2558 u32 exp_frags = get_expected_frags(pbuf) - 1;
2559 skb_copy_to_linear_data_offset(pbuf, crs,
2560 msg_data(fragm), dsz);
2563 /* Is message complete? */
2565 if (exp_frags == 0) {
2567 prev->next = pbuf->next;
2569 *pending = pbuf->next;
2570 msg_reset_reroute_cnt(buf_msg(pbuf));
2575 set_expected_frags(pbuf, exp_frags);
2583 * link_check_defragm_bufs - flush stale incoming message fragments
2584 * @l_ptr: pointer to link
2587 static void link_check_defragm_bufs(struct link *l_ptr)
2589 struct sk_buff *prev = NULL;
2590 struct sk_buff *next = NULL;
2591 struct sk_buff *buf = l_ptr->defragm_buf;
2595 if (!link_working_working(l_ptr))
2598 u32 cnt = get_timer_cnt(buf);
2602 incr_timer_cnt(buf);
2606 prev->next = buf->next;
2608 l_ptr->defragm_buf = buf->next;
2617 static void link_set_supervision_props(struct link *l_ptr, u32 tolerance)
2619 if ((tolerance < TIPC_MIN_LINK_TOL) || (tolerance > TIPC_MAX_LINK_TOL))
2622 l_ptr->tolerance = tolerance;
2623 l_ptr->continuity_interval =
2624 ((tolerance / 4) > 500) ? 500 : tolerance / 4;
2625 l_ptr->abort_limit = tolerance / (l_ptr->continuity_interval / 4);
2629 void tipc_link_set_queue_limits(struct link *l_ptr, u32 window)
2631 /* Data messages from this node, inclusive FIRST_FRAGM */
2632 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window;
2633 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4;
2634 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5;
2635 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6;
2636 /* Transiting data messages,inclusive FIRST_FRAGM */
2637 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300;
2638 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600;
2639 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
2640 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
2641 l_ptr->queue_limit[CONN_MANAGER] = 1200;
2642 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
2643 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
2644 /* FRAGMENT and LAST_FRAGMENT packets */
2645 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
2649 * link_find_link - locate link by name
2650 * @name - ptr to link name string
2651 * @node - ptr to area to be filled with ptr to associated node
2653 * Caller must hold 'tipc_net_lock' to ensure node and bearer are not deleted;
2654 * this also prevents link deletion.
2656 * Returns pointer to link (or 0 if invalid link name).
2659 static struct link *link_find_link(const char *name, struct tipc_node **node)
2661 struct link_name link_name_parts;
2662 struct tipc_bearer *b_ptr;
2665 if (!link_name_validate(name, &link_name_parts))
2668 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local);
2672 *node = tipc_node_find(link_name_parts.addr_peer);
2676 l_ptr = (*node)->links[b_ptr->identity];
2677 if (!l_ptr || strcmp(l_ptr->name, name))
2683 struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
2686 struct tipc_link_config *args;
2689 struct tipc_node *node;
2692 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_CONFIG))
2693 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2695 args = (struct tipc_link_config *)TLV_DATA(req_tlv_area);
2696 new_value = ntohl(args->value);
2698 if (!strcmp(args->name, tipc_bclink_name)) {
2699 if ((cmd == TIPC_CMD_SET_LINK_WINDOW) &&
2700 (tipc_bclink_set_queue_limits(new_value) == 0))
2701 return tipc_cfg_reply_none();
2702 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
2703 " (cannot change setting on broadcast link)");
2706 read_lock_bh(&tipc_net_lock);
2707 l_ptr = link_find_link(args->name, &node);
2709 read_unlock_bh(&tipc_net_lock);
2710 return tipc_cfg_reply_error_string("link not found");
2713 tipc_node_lock(node);
2716 case TIPC_CMD_SET_LINK_TOL:
2717 if ((new_value >= TIPC_MIN_LINK_TOL) &&
2718 (new_value <= TIPC_MAX_LINK_TOL)) {
2719 link_set_supervision_props(l_ptr, new_value);
2720 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2721 0, 0, new_value, 0, 0);
2725 case TIPC_CMD_SET_LINK_PRI:
2726 if ((new_value >= TIPC_MIN_LINK_PRI) &&
2727 (new_value <= TIPC_MAX_LINK_PRI)) {
2728 l_ptr->priority = new_value;
2729 tipc_link_send_proto_msg(l_ptr, STATE_MSG,
2730 0, 0, 0, new_value, 0);
2734 case TIPC_CMD_SET_LINK_WINDOW:
2735 if ((new_value >= TIPC_MIN_LINK_WIN) &&
2736 (new_value <= TIPC_MAX_LINK_WIN)) {
2737 tipc_link_set_queue_limits(l_ptr, new_value);
2742 tipc_node_unlock(node);
2744 read_unlock_bh(&tipc_net_lock);
2746 return tipc_cfg_reply_error_string("cannot change link setting");
2748 return tipc_cfg_reply_none();
2752 * link_reset_statistics - reset link statistics
2753 * @l_ptr: pointer to link
2756 static void link_reset_statistics(struct link *l_ptr)
2758 memset(&l_ptr->stats, 0, sizeof(l_ptr->stats));
2759 l_ptr->stats.sent_info = l_ptr->next_out_no;
2760 l_ptr->stats.recv_info = l_ptr->next_in_no;
2763 struct sk_buff *tipc_link_cmd_reset_stats(const void *req_tlv_area, int req_tlv_space)
2767 struct tipc_node *node;
2769 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2770 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2772 link_name = (char *)TLV_DATA(req_tlv_area);
2773 if (!strcmp(link_name, tipc_bclink_name)) {
2774 if (tipc_bclink_reset_stats())
2775 return tipc_cfg_reply_error_string("link not found");
2776 return tipc_cfg_reply_none();
2779 read_lock_bh(&tipc_net_lock);
2780 l_ptr = link_find_link(link_name, &node);
2782 read_unlock_bh(&tipc_net_lock);
2783 return tipc_cfg_reply_error_string("link not found");
2786 tipc_node_lock(node);
2787 link_reset_statistics(l_ptr);
2788 tipc_node_unlock(node);
2789 read_unlock_bh(&tipc_net_lock);
2790 return tipc_cfg_reply_none();
2794 * percent - convert count to a percentage of total (rounding up or down)
2797 static u32 percent(u32 count, u32 total)
2799 return (count * 100 + (total / 2)) / total;
2803 * tipc_link_stats - print link statistics
2805 * @buf: print buffer area
2806 * @buf_size: size of print buffer area
2808 * Returns length of print buffer data string (or 0 if error)
2811 static int tipc_link_stats(const char *name, char *buf, const u32 buf_size)
2813 struct print_buf pb;
2815 struct tipc_node *node;
2817 u32 profile_total = 0;
2819 if (!strcmp(name, tipc_bclink_name))
2820 return tipc_bclink_stats(buf, buf_size);
2822 tipc_printbuf_init(&pb, buf, buf_size);
2824 read_lock_bh(&tipc_net_lock);
2825 l_ptr = link_find_link(name, &node);
2827 read_unlock_bh(&tipc_net_lock);
2830 tipc_node_lock(node);
2832 if (tipc_link_is_active(l_ptr))
2834 else if (tipc_link_is_up(l_ptr))
2838 tipc_printf(&pb, "Link <%s>\n"
2839 " %s MTU:%u Priority:%u Tolerance:%u ms"
2840 " Window:%u packets\n",
2841 l_ptr->name, status, l_ptr->max_pkt,
2842 l_ptr->priority, l_ptr->tolerance, l_ptr->queue_limit[0]);
2843 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
2844 l_ptr->next_in_no - l_ptr->stats.recv_info,
2845 l_ptr->stats.recv_fragments,
2846 l_ptr->stats.recv_fragmented,
2847 l_ptr->stats.recv_bundles,
2848 l_ptr->stats.recv_bundled);
2849 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
2850 l_ptr->next_out_no - l_ptr->stats.sent_info,
2851 l_ptr->stats.sent_fragments,
2852 l_ptr->stats.sent_fragmented,
2853 l_ptr->stats.sent_bundles,
2854 l_ptr->stats.sent_bundled);
2855 profile_total = l_ptr->stats.msg_length_counts;
2858 tipc_printf(&pb, " TX profile sample:%u packets average:%u octets\n"
2859 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% "
2860 "-16354:%u%% -32768:%u%% -66000:%u%%\n",
2861 l_ptr->stats.msg_length_counts,
2862 l_ptr->stats.msg_lengths_total / profile_total,
2863 percent(l_ptr->stats.msg_length_profile[0], profile_total),
2864 percent(l_ptr->stats.msg_length_profile[1], profile_total),
2865 percent(l_ptr->stats.msg_length_profile[2], profile_total),
2866 percent(l_ptr->stats.msg_length_profile[3], profile_total),
2867 percent(l_ptr->stats.msg_length_profile[4], profile_total),
2868 percent(l_ptr->stats.msg_length_profile[5], profile_total),
2869 percent(l_ptr->stats.msg_length_profile[6], profile_total));
2870 tipc_printf(&pb, " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
2871 l_ptr->stats.recv_states,
2872 l_ptr->stats.recv_probes,
2873 l_ptr->stats.recv_nacks,
2874 l_ptr->stats.deferred_recv,
2875 l_ptr->stats.duplicates);
2876 tipc_printf(&pb, " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
2877 l_ptr->stats.sent_states,
2878 l_ptr->stats.sent_probes,
2879 l_ptr->stats.sent_nacks,
2880 l_ptr->stats.sent_acks,
2881 l_ptr->stats.retransmitted);
2882 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
2883 l_ptr->stats.bearer_congs,
2884 l_ptr->stats.link_congs,
2885 l_ptr->stats.max_queue_sz,
2886 l_ptr->stats.queue_sz_counts
2887 ? (l_ptr->stats.accu_queue_sz / l_ptr->stats.queue_sz_counts)
2890 tipc_node_unlock(node);
2891 read_unlock_bh(&tipc_net_lock);
2892 return tipc_printbuf_validate(&pb);
2895 #define MAX_LINK_STATS_INFO 2000
2897 struct sk_buff *tipc_link_cmd_show_stats(const void *req_tlv_area, int req_tlv_space)
2899 struct sk_buff *buf;
2900 struct tlv_desc *rep_tlv;
2903 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_LINK_NAME))
2904 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
2906 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_LINK_STATS_INFO));
2910 rep_tlv = (struct tlv_desc *)buf->data;
2912 str_len = tipc_link_stats((char *)TLV_DATA(req_tlv_area),
2913 (char *)TLV_DATA(rep_tlv), MAX_LINK_STATS_INFO);
2916 return tipc_cfg_reply_error_string("link not found");
2919 skb_put(buf, TLV_SPACE(str_len));
2920 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2926 * tipc_link_get_max_pkt - get maximum packet size to use when sending to destination
2927 * @dest: network address of destination node
2928 * @selector: used to select from set of active links
2930 * If no active link can be found, uses default maximum packet size.
2933 u32 tipc_link_get_max_pkt(u32 dest, u32 selector)
2935 struct tipc_node *n_ptr;
2937 u32 res = MAX_PKT_DEFAULT;
2939 if (dest == tipc_own_addr)
2940 return MAX_MSG_SIZE;
2942 read_lock_bh(&tipc_net_lock);
2943 n_ptr = tipc_node_find(dest);
2945 tipc_node_lock(n_ptr);
2946 l_ptr = n_ptr->active_links[selector & 1];
2948 res = l_ptr->max_pkt;
2949 tipc_node_unlock(n_ptr);
2951 read_unlock_bh(&tipc_net_lock);
2955 static void link_print(struct link *l_ptr, const char *str)
2957 char print_area[256];
2958 struct print_buf pb;
2959 struct print_buf *buf = &pb;
2961 tipc_printbuf_init(buf, print_area, sizeof(print_area));
2963 tipc_printf(buf, str);
2964 tipc_printf(buf, "Link %x<%s>:",
2965 l_ptr->addr, l_ptr->b_ptr->name);
2967 #ifdef CONFIG_TIPC_DEBUG
2968 if (link_reset_reset(l_ptr) || link_reset_unknown(l_ptr))
2971 tipc_printf(buf, ": NXO(%u):", mod(l_ptr->next_out_no));
2972 tipc_printf(buf, "NXI(%u):", mod(l_ptr->next_in_no));
2973 tipc_printf(buf, "SQUE");
2974 if (l_ptr->first_out) {
2975 tipc_printf(buf, "[%u..", msg_seqno(buf_msg(l_ptr->first_out)));
2976 if (l_ptr->next_out)
2977 tipc_printf(buf, "%u..",
2978 msg_seqno(buf_msg(l_ptr->next_out)));
2979 tipc_printf(buf, "%u]", msg_seqno(buf_msg(l_ptr->last_out)));
2980 if ((mod(msg_seqno(buf_msg(l_ptr->last_out)) -
2981 msg_seqno(buf_msg(l_ptr->first_out)))
2982 != (l_ptr->out_queue_size - 1)) ||
2983 (l_ptr->last_out->next != NULL)) {
2984 tipc_printf(buf, "\nSend queue inconsistency\n");
2985 tipc_printf(buf, "first_out= %p ", l_ptr->first_out);
2986 tipc_printf(buf, "next_out= %p ", l_ptr->next_out);
2987 tipc_printf(buf, "last_out= %p ", l_ptr->last_out);
2990 tipc_printf(buf, "[]");
2991 tipc_printf(buf, "SQSIZ(%u)", l_ptr->out_queue_size);
2992 if (l_ptr->oldest_deferred_in) {
2993 u32 o = msg_seqno(buf_msg(l_ptr->oldest_deferred_in));
2994 u32 n = msg_seqno(buf_msg(l_ptr->newest_deferred_in));
2995 tipc_printf(buf, ":RQUE[%u..%u]", o, n);
2996 if (l_ptr->deferred_inqueue_sz != mod((n + 1) - o)) {
2997 tipc_printf(buf, ":RQSIZ(%u)",
2998 l_ptr->deferred_inqueue_sz);
3004 if (link_working_unknown(l_ptr))
3005 tipc_printf(buf, ":WU");
3006 else if (link_reset_reset(l_ptr))
3007 tipc_printf(buf, ":RR");
3008 else if (link_reset_unknown(l_ptr))
3009 tipc_printf(buf, ":RU");
3010 else if (link_working_working(l_ptr))
3011 tipc_printf(buf, ":WW");
3012 tipc_printf(buf, "\n");
3014 tipc_printbuf_validate(buf);
3015 info("%s", print_area);