2 * net/tipc/bcast.c: TIPC broadcast code
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2004, Intel Corporation.
6 * Copyright (c) 2005, Wind River Systems
7 * Copyright (c) 2005-2006, Ericsson AB
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this
14 * list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * Neither the names of the copyright holders nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
43 #include "node_subscr.h"
44 #include "name_distr.h"
46 #include "name_table.h"
50 #define MAX_PKT_DEFAULT_MCAST 1500 /* bcast link max packet size (fixed) */
52 #define BCLINK_WIN_DEFAULT 20 /* bcast link window size (default) */
54 #define BCLINK_LOG_BUF_SIZE 0
57 * struct bcbearer_pair - a pair of bearers used by broadcast link
58 * @primary: pointer to primary bearer
59 * @secondary: pointer to secondary bearer
61 * Bearers must have same priority and same set of reachable destinations
65 struct bcbearer_pair {
66 struct bearer *primary;
67 struct bearer *secondary;
71 * struct bcbearer - bearer used by broadcast link
72 * @bearer: (non-standard) broadcast bearer structure
73 * @media: (non-standard) broadcast media structure
74 * @bpairs: array of bearer pairs
75 * @bpairs_temp: array of bearer pairs used during creation of "bpairs"
81 struct bcbearer_pair bpairs[MAX_BEARERS];
82 struct bcbearer_pair bpairs_temp[TIPC_NUM_LINK_PRI];
86 * struct bclink - link used for broadcast messages
87 * @link: (non-standard) broadcast link structure
88 * @node: (non-standard) node structure representing b'cast link's peer node
90 * Handles sequence numbering, fragmentation, bundling, etc.
99 static struct bcbearer *bcbearer = NULL;
100 static struct bclink *bclink = NULL;
101 static struct link *bcl = NULL;
102 static spinlock_t bc_lock = SPIN_LOCK_UNLOCKED;
104 char bc_link_name[] = "multicast-link";
107 static inline u32 buf_seqno(struct sk_buff *buf)
109 return msg_seqno(buf_msg(buf));
112 static inline u32 bcbuf_acks(struct sk_buff *buf)
114 return (u32)TIPC_SKB_CB(buf)->handle;
117 static inline void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
119 TIPC_SKB_CB(buf)->handle = (void *)acks;
122 static inline void bcbuf_decr_acks(struct sk_buff *buf)
124 bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
129 * bclink_set_gap - set gap according to contents of current deferred pkt queue
131 * Called with 'node' locked, bc_lock unlocked
134 static inline void bclink_set_gap(struct node *n_ptr)
136 struct sk_buff *buf = n_ptr->bclink.deferred_head;
138 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to =
139 mod(n_ptr->bclink.last_in);
140 if (unlikely(buf != NULL))
141 n_ptr->bclink.gap_to = mod(buf_seqno(buf) - 1);
145 * bclink_ack_allowed - test if ACK or NACK message can be sent at this moment
147 * This mechanism endeavours to prevent all nodes in network from trying
148 * to ACK or NACK at the same time.
150 * Note: TIPC uses a different trigger to distribute ACKs than it does to
151 * distribute NACKs, but tries to use the same spacing (divide by 16).
154 static inline int bclink_ack_allowed(u32 n)
156 return((n % TIPC_MIN_LINK_WIN) == tipc_own_tag);
161 * bclink_retransmit_pkt - retransmit broadcast packets
162 * @after: sequence number of last packet to *not* retransmit
163 * @to: sequence number of last packet to retransmit
165 * Called with 'node' locked, bc_lock unlocked
168 static void bclink_retransmit_pkt(u32 after, u32 to)
172 spin_lock_bh(&bc_lock);
173 buf = bcl->first_out;
174 while (buf && less_eq(buf_seqno(buf), after)) {
178 link_retransmit(bcl, buf, mod(to - after));
179 spin_unlock_bh(&bc_lock);
183 * bclink_acknowledge - handle acknowledgement of broadcast packets
184 * @n_ptr: node that sent acknowledgement info
185 * @acked: broadcast sequence # that has been acknowledged
187 * Node is locked, bc_lock unlocked.
190 void bclink_acknowledge(struct node *n_ptr, u32 acked)
193 struct sk_buff *next;
194 unsigned int released = 0;
196 if (less_eq(acked, n_ptr->bclink.acked))
199 spin_lock_bh(&bc_lock);
201 /* Skip over packets that node has previously acknowledged */
203 crs = bcl->first_out;
204 while (crs && less_eq(buf_seqno(crs), n_ptr->bclink.acked)) {
208 /* Update packets that node is now acknowledging */
210 while (crs && less_eq(buf_seqno(crs), acked)) {
212 bcbuf_decr_acks(crs);
213 if (bcbuf_acks(crs) == 0) {
214 bcl->first_out = next;
215 bcl->out_queue_size--;
221 n_ptr->bclink.acked = acked;
223 /* Try resolving broadcast link congestion, if necessary */
225 if (unlikely(bcl->next_out))
226 link_push_queue(bcl);
227 if (unlikely(released && !list_empty(&bcl->waiting_ports)))
228 link_wakeup_ports(bcl, 0);
229 spin_unlock_bh(&bc_lock);
233 * bclink_send_ack - unicast an ACK msg
235 * net_lock and node lock set
238 static void bclink_send_ack(struct node *n_ptr)
240 struct link *l_ptr = n_ptr->active_links[n_ptr->addr & 1];
243 link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
247 * bclink_send_nack- broadcast a NACK msg
249 * net_lock and node lock set
252 static void bclink_send_nack(struct node *n_ptr)
255 struct tipc_msg *msg;
257 if (!less(n_ptr->bclink.gap_after, n_ptr->bclink.gap_to))
260 buf = buf_acquire(INT_H_SIZE);
263 msg_init(msg, BCAST_PROTOCOL, STATE_MSG,
264 TIPC_OK, INT_H_SIZE, n_ptr->addr);
265 msg_set_mc_netid(msg, tipc_net_id);
266 msg_set_bcast_ack(msg, mod(n_ptr->bclink.last_in));
267 msg_set_bcgap_after(msg, n_ptr->bclink.gap_after);
268 msg_set_bcgap_to(msg, n_ptr->bclink.gap_to);
269 msg_set_bcast_tag(msg, tipc_own_tag);
271 if (bearer_send(&bcbearer->bearer, buf, 0)) {
272 bcl->stats.sent_nacks++;
275 bearer_schedule(bcl->b_ptr, bcl);
276 bcl->proto_msg_queue = buf;
277 bcl->stats.bearer_congs++;
281 * Ensure we doesn't send another NACK msg to the node
282 * until 16 more deferred messages arrive from it
283 * (i.e. helps prevent all nodes from NACK'ing at same time)
286 n_ptr->bclink.nack_sync = tipc_own_tag;
291 * bclink_check_gap - send a NACK if a sequence gap exists
293 * net_lock and node lock set
296 void bclink_check_gap(struct node *n_ptr, u32 last_sent)
298 if (!n_ptr->bclink.supported ||
299 less_eq(last_sent, mod(n_ptr->bclink.last_in)))
302 bclink_set_gap(n_ptr);
303 if (n_ptr->bclink.gap_after == n_ptr->bclink.gap_to)
304 n_ptr->bclink.gap_to = last_sent;
305 bclink_send_nack(n_ptr);
309 * bclink_peek_nack - process a NACK msg meant for another node
314 void bclink_peek_nack(u32 dest, u32 sender_tag, u32 gap_after, u32 gap_to)
316 struct node *n_ptr = node_find(dest);
319 if (unlikely(!n_ptr || !node_is_up(n_ptr)))
323 * Modify gap to suppress unnecessary NACKs from this node
325 my_after = n_ptr->bclink.gap_after;
326 my_to = n_ptr->bclink.gap_to;
328 if (less_eq(gap_after, my_after)) {
329 if (less(my_after, gap_to) && less(gap_to, my_to))
330 n_ptr->bclink.gap_after = gap_to;
331 else if (less_eq(my_to, gap_to))
332 n_ptr->bclink.gap_to = n_ptr->bclink.gap_after;
333 } else if (less_eq(gap_after, my_to)) {
334 if (less_eq(my_to, gap_to))
335 n_ptr->bclink.gap_to = gap_after;
338 * Expand gap if missing bufs not in deferred queue:
340 struct sk_buff *buf = n_ptr->bclink.deferred_head;
341 u32 prev = n_ptr->bclink.gap_to;
343 for (; buf; buf = buf->next) {
344 u32 seqno = buf_seqno(buf);
346 if (mod(seqno - prev) != 1)
348 if (seqno == gap_after)
353 n_ptr->bclink.gap_to = gap_after;
356 * Some nodes may send a complementary NACK now:
358 if (bclink_ack_allowed(sender_tag + 1)) {
359 if (n_ptr->bclink.gap_to != n_ptr->bclink.gap_after) {
360 bclink_send_nack(n_ptr);
361 bclink_set_gap(n_ptr);
368 * bclink_send_msg - broadcast a packet to all nodes in cluster
371 int bclink_send_msg(struct sk_buff *buf)
375 spin_lock_bh(&bc_lock);
377 res = link_send_buf(bcl, buf);
378 if (unlikely(res == -ELINKCONG))
381 bcl->stats.sent_info++;
383 if (bcl->out_queue_size > bcl->stats.max_queue_sz)
384 bcl->stats.max_queue_sz = bcl->out_queue_size;
385 bcl->stats.queue_sz_counts++;
386 bcl->stats.accu_queue_sz += bcl->out_queue_size;
388 spin_unlock_bh(&bc_lock);
393 * bclink_recv_pkt - receive a broadcast packet, and deliver upwards
395 * net_lock is read_locked, no other locks set
398 void bclink_recv_pkt(struct sk_buff *buf)
400 struct tipc_msg *msg = buf_msg(buf);
401 struct node* node = node_find(msg_prevnode(msg));
404 struct sk_buff *deferred;
406 msg_dbg(msg, "<BC<<<");
408 if (unlikely(!node || !node_is_up(node) || !node->bclink.supported ||
409 (msg_mc_netid(msg) != tipc_net_id))) {
414 if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
415 msg_dbg(msg, "<BCNACK<<<");
416 if (msg_destnode(msg) == tipc_own_addr) {
418 bclink_acknowledge(node, msg_bcast_ack(msg));
420 bcl->stats.recv_nacks++;
421 bclink_retransmit_pkt(msg_bcgap_after(msg),
424 bclink_peek_nack(msg_destnode(msg),
426 msg_bcgap_after(msg),
435 deferred = node->bclink.deferred_head;
436 next_in = mod(node->bclink.last_in + 1);
437 seqno = msg_seqno(msg);
439 if (likely(seqno == next_in)) {
440 bcl->stats.recv_info++;
441 node->bclink.last_in++;
442 bclink_set_gap(node);
443 if (unlikely(bclink_ack_allowed(seqno))) {
444 bclink_send_ack(node);
445 bcl->stats.sent_acks++;
447 if (likely(msg_isdata(msg))) {
449 port_recv_mcast(buf, NULL);
450 } else if (msg_user(msg) == MSG_BUNDLER) {
451 bcl->stats.recv_bundles++;
452 bcl->stats.recv_bundled += msg_msgcnt(msg);
454 link_recv_bundle(buf);
455 } else if (msg_user(msg) == MSG_FRAGMENTER) {
456 bcl->stats.recv_fragments++;
457 if (link_recv_fragment(&node->bclink.defragm,
459 bcl->stats.recv_fragmented++;
466 if (deferred && (buf_seqno(deferred) == mod(next_in + 1))) {
470 node->bclink.deferred_head = deferred->next;
474 } else if (less(next_in, seqno)) {
475 u32 gap_after = node->bclink.gap_after;
476 u32 gap_to = node->bclink.gap_to;
478 if (link_defer_pkt(&node->bclink.deferred_head,
479 &node->bclink.deferred_tail,
481 node->bclink.nack_sync++;
482 bcl->stats.deferred_recv++;
483 if (seqno == mod(gap_after + 1))
484 node->bclink.gap_after = seqno;
485 else if (less(gap_after, seqno) && less(seqno, gap_to))
486 node->bclink.gap_to = seqno;
488 if (bclink_ack_allowed(node->bclink.nack_sync)) {
489 if (gap_to != gap_after)
490 bclink_send_nack(node);
491 bclink_set_gap(node);
494 bcl->stats.duplicates++;
500 u32 bclink_get_last_sent(void)
502 u32 last_sent = mod(bcl->next_out_no - 1);
505 last_sent = mod(buf_seqno(bcl->next_out) - 1);
509 u32 bclink_acks_missing(struct node *n_ptr)
511 return (n_ptr->bclink.supported &&
512 (bclink_get_last_sent() != n_ptr->bclink.acked));
517 * bcbearer_send - send a packet through the broadcast pseudo-bearer
519 * Send through as many bearers as necessary to reach all nodes
520 * that support TIPC multicasting.
522 * Returns 0 if packet sent successfully, non-zero if not
525 int bcbearer_send(struct sk_buff *buf,
526 struct tipc_bearer *unused1,
527 struct tipc_media_addr *unused2)
529 static int send_count = 0;
531 struct node_map remains;
532 struct node_map remains_new;
536 /* Prepare buffer for broadcasting (if first time trying to send it) */
538 if (likely(!msg_non_seq(buf_msg(buf)))) {
539 struct tipc_msg *msg;
541 assert(cluster_bcast_nodes.count != 0);
542 bcbuf_set_acks(buf, cluster_bcast_nodes.count);
544 msg_set_non_seq(msg);
545 msg_set_mc_netid(msg, tipc_net_id);
548 /* Determine if bearer pairs should be swapped following this attempt */
550 if ((swap_time = (++send_count >= 10)))
553 /* Send buffer over bearers until all targets reached */
555 remains = cluster_bcast_nodes;
557 for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
558 struct bearer *p = bcbearer->bpairs[bp_index].primary;
559 struct bearer *s = bcbearer->bpairs[bp_index].secondary;
562 break; /* no more bearers to try */
564 nmap_diff(&remains, &p->nodes, &remains_new);
565 if (remains_new.count == remains.count)
566 continue; /* bearer pair doesn't add anything */
568 if (!p->publ.blocked &&
569 !p->media->send_msg(buf, &p->publ, &p->media->bcast_addr)) {
570 if (swap_time && s && !s->publ.blocked)
576 if (!s || s->publ.blocked ||
577 s->media->send_msg(buf, &s->publ, &s->media->bcast_addr))
578 continue; /* unable to send using bearer pair */
580 bcbearer->bpairs[bp_index].primary = s;
581 bcbearer->bpairs[bp_index].secondary = p;
583 if (remains_new.count == 0)
586 remains = remains_new;
589 /* Unable to reach all targets */
591 bcbearer->bearer.publ.blocked = 1;
592 bcl->stats.bearer_congs++;
597 * bcbearer_sort - create sets of bearer pairs used by broadcast bearer
600 void bcbearer_sort(void)
602 struct bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
603 struct bcbearer_pair *bp_curr;
607 spin_lock_bh(&bc_lock);
609 /* Group bearers by priority (can assume max of two per priority) */
611 memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
613 for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
614 struct bearer *b = &bearers[b_index];
616 if (!b->active || !b->nodes.count)
619 if (!bp_temp[b->priority].primary)
620 bp_temp[b->priority].primary = b;
622 bp_temp[b->priority].secondary = b;
625 /* Create array of bearer pairs for broadcasting */
627 bp_curr = bcbearer->bpairs;
628 memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
630 for (pri = (TIPC_NUM_LINK_PRI - 1); pri >= 0; pri--) {
632 if (!bp_temp[pri].primary)
635 bp_curr->primary = bp_temp[pri].primary;
637 if (bp_temp[pri].secondary) {
638 if (nmap_equal(&bp_temp[pri].primary->nodes,
639 &bp_temp[pri].secondary->nodes)) {
640 bp_curr->secondary = bp_temp[pri].secondary;
643 bp_curr->primary = bp_temp[pri].secondary;
650 spin_unlock_bh(&bc_lock);
654 * bcbearer_push - resolve bearer congestion
656 * Forces bclink to push out any unsent packets, until all packets are gone
657 * or congestion reoccurs.
658 * No locks set when function called
661 void bcbearer_push(void)
663 struct bearer *b_ptr;
665 spin_lock_bh(&bc_lock);
666 b_ptr = &bcbearer->bearer;
667 if (b_ptr->publ.blocked) {
668 b_ptr->publ.blocked = 0;
669 bearer_lock_push(b_ptr);
671 spin_unlock_bh(&bc_lock);
675 int bclink_stats(char *buf, const u32 buf_size)
682 printbuf_init(&pb, buf, buf_size);
684 spin_lock_bh(&bc_lock);
686 tipc_printf(&pb, "Link <%s>\n"
687 " Window:%u packets\n",
688 bcl->name, bcl->queue_limit[0]);
689 tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
690 bcl->stats.recv_info,
691 bcl->stats.recv_fragments,
692 bcl->stats.recv_fragmented,
693 bcl->stats.recv_bundles,
694 bcl->stats.recv_bundled);
695 tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
696 bcl->stats.sent_info,
697 bcl->stats.sent_fragments,
698 bcl->stats.sent_fragmented,
699 bcl->stats.sent_bundles,
700 bcl->stats.sent_bundled);
701 tipc_printf(&pb, " RX naks:%u defs:%u dups:%u\n",
702 bcl->stats.recv_nacks,
703 bcl->stats.deferred_recv,
704 bcl->stats.duplicates);
705 tipc_printf(&pb, " TX naks:%u acks:%u dups:%u\n",
706 bcl->stats.sent_nacks,
707 bcl->stats.sent_acks,
708 bcl->stats.retransmitted);
709 tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
710 bcl->stats.bearer_congs,
711 bcl->stats.link_congs,
712 bcl->stats.max_queue_sz,
713 bcl->stats.queue_sz_counts
714 ? (bcl->stats.accu_queue_sz / bcl->stats.queue_sz_counts)
717 spin_unlock_bh(&bc_lock);
718 return printbuf_validate(&pb);
721 int bclink_reset_stats(void)
726 spin_lock_bh(&bc_lock);
727 memset(&bcl->stats, 0, sizeof(bcl->stats));
728 spin_unlock_bh(&bc_lock);
732 int bclink_set_queue_limits(u32 limit)
736 if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
739 spin_lock_bh(&bc_lock);
740 link_set_queue_limits(bcl, limit);
741 spin_unlock_bh(&bc_lock);
745 int bclink_init(void)
747 bcbearer = kmalloc(sizeof(*bcbearer), GFP_ATOMIC);
748 bclink = kmalloc(sizeof(*bclink), GFP_ATOMIC);
749 if (!bcbearer || !bclink) {
751 warn("Memory squeeze; Failed to create multicast link\n");
759 memset(bcbearer, 0, sizeof(struct bcbearer));
760 INIT_LIST_HEAD(&bcbearer->bearer.cong_links);
761 bcbearer->bearer.media = &bcbearer->media;
762 bcbearer->media.send_msg = bcbearer_send;
763 sprintf(bcbearer->media.name, "tipc-multicast");
766 memset(bclink, 0, sizeof(struct bclink));
767 INIT_LIST_HEAD(&bcl->waiting_ports);
768 bcl->next_out_no = 1;
769 bclink->node.lock = SPIN_LOCK_UNLOCKED;
770 bcl->owner = &bclink->node;
771 bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
772 link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
773 bcl->b_ptr = &bcbearer->bearer;
774 bcl->state = WORKING_WORKING;
775 sprintf(bcl->name, bc_link_name);
777 if (BCLINK_LOG_BUF_SIZE) {
778 char *pb = kmalloc(BCLINK_LOG_BUF_SIZE, GFP_ATOMIC);
782 printbuf_init(&bcl->print_buf, pb, BCLINK_LOG_BUF_SIZE);
788 void bclink_stop(void)
790 spin_lock_bh(&bc_lock);
793 if (BCLINK_LOG_BUF_SIZE)
794 kfree(bcl->print_buf.buf);
801 spin_unlock_bh(&bc_lock);