tipc: Remove obsolete native API files and exports
[linux-2.6.git] / net / tipc / bcast.c
1 /*
2  * net/tipc/bcast.c: TIPC broadcast code
3  *
4  * Copyright (c) 2004-2006, Ericsson AB
5  * Copyright (c) 2004, Intel Corporation.
6  * Copyright (c) 2005, Wind River Systems
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the names of the copyright holders nor the names of its
18  *    contributors may be used to endorse or promote products derived from
19  *    this software without specific prior written permission.
20  *
21  * Alternatively, this software may be distributed under the terms of the
22  * GNU General Public License ("GPL") version 2 as published by the Free
23  * Software Foundation.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include "core.h"
39 #include "msg.h"
40 #include "dbg.h"
41 #include "link.h"
42 #include "net.h"
43 #include "node.h"
44 #include "port.h"
45 #include "addr.h"
46 #include "node_subscr.h"
47 #include "name_distr.h"
48 #include "bearer.h"
49 #include "name_table.h"
50 #include "port.h"
51 #include "bcast.h"
52
53 #define MAX_PKT_DEFAULT_MCAST 1500      /* bcast link max packet size (fixed) */
54
55 #define BCLINK_WIN_DEFAULT 20           /* bcast link window size (default) */
56
57 #define BCLINK_LOG_BUF_SIZE 0
58
59 /*
60  * Loss rate for incoming broadcast frames; used to test retransmission code.
61  * Set to N to cause every N'th frame to be discarded; 0 => don't discard any.
62  */
63
64 #define TIPC_BCAST_LOSS_RATE 0
65
66 /**
67  * struct bcbearer_pair - a pair of bearers used by broadcast link
68  * @primary: pointer to primary bearer
69  * @secondary: pointer to secondary bearer
70  *
71  * Bearers must have same priority and same set of reachable destinations
72  * to be paired.
73  */
74
75 struct bcbearer_pair {
76         struct bearer *primary;
77         struct bearer *secondary;
78 };
79
80 /**
81  * struct bcbearer - bearer used by broadcast link
82  * @bearer: (non-standard) broadcast bearer structure
83  * @media: (non-standard) broadcast media structure
84  * @bpairs: array of bearer pairs
85  * @bpairs_temp: temporary array of bearer pairs used by tipc_bcbearer_sort()
86  * @remains: temporary node map used by tipc_bcbearer_send()
87  * @remains_new: temporary node map used tipc_bcbearer_send()
88  *
89  * Note: The fields labelled "temporary" are incorporated into the bearer
90  * to avoid consuming potentially limited stack space through the use of
91  * large local variables within multicast routines.  Concurrent access is
92  * prevented through use of the spinlock "bc_lock".
93  */
94
95 struct bcbearer {
96         struct bearer bearer;
97         struct media media;
98         struct bcbearer_pair bpairs[MAX_BEARERS];
99         struct bcbearer_pair bpairs_temp[TIPC_MAX_LINK_PRI + 1];
100         struct tipc_node_map remains;
101         struct tipc_node_map remains_new;
102 };
103
104 /**
105  * struct bclink - link used for broadcast messages
106  * @link: (non-standard) broadcast link structure
107  * @node: (non-standard) node structure representing b'cast link's peer node
108  *
109  * Handles sequence numbering, fragmentation, bundling, etc.
110  */
111
112 struct bclink {
113         struct link link;
114         struct tipc_node node;
115 };
116
117
118 static struct bcbearer *bcbearer = NULL;
119 static struct bclink *bclink = NULL;
120 static struct link *bcl = NULL;
121 static DEFINE_SPINLOCK(bc_lock);
122
123 const char tipc_bclink_name[] = "broadcast-link";
124
125 static void tipc_nmap_diff(struct tipc_node_map *nm_a,
126                            struct tipc_node_map *nm_b,
127                            struct tipc_node_map *nm_diff);
128
129 static u32 buf_seqno(struct sk_buff *buf)
130 {
131         return msg_seqno(buf_msg(buf));
132 }
133
134 static u32 bcbuf_acks(struct sk_buff *buf)
135 {
136         return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
137 }
138
139 static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
140 {
141         TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
142 }
143
144 static void bcbuf_decr_acks(struct sk_buff *buf)
145 {
146         bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
147 }
148
149
150 static void bclink_set_last_sent(void)
151 {
152         if (bcl->next_out)
153                 bcl->fsm_msg_cnt = mod(buf_seqno(bcl->next_out) - 1);
154         else
155                 bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
156 }
157
158 u32 tipc_bclink_get_last_sent(void)
159 {
160         return bcl->fsm_msg_cnt;
161 }
162
163 /**
164  * bclink_set_gap - set gap according to contents of current deferred pkt queue
165  *
166  * Called with 'node' locked, bc_lock unlocked
167  */
168
169 static void bclink_set_gap(struct tipc_node *n_ptr)
170 {
171         struct sk_buff *buf = n_ptr->bclink.deferred_head;
172
173         n_ptr->bclink.gap_after = n_ptr->bclink.gap_to =
174                 mod(n_ptr->bclink.last_in);
175         if (unlikely(buf != NULL))
176                 n_ptr->bclink.gap_to = mod(buf_seqno(buf) - 1);
177 }
178
179 /**
180  * bclink_ack_allowed - test if ACK or NACK message can be sent at this moment
181  *
182  * This mechanism endeavours to prevent all nodes in network from trying
183  * to ACK or NACK at the same time.
184  *
185  * Note: TIPC uses a different trigger to distribute ACKs than it does to
186  *       distribute NACKs, but tries to use the same spacing (divide by 16).
187  */
188
189 static int bclink_ack_allowed(u32 n)
190 {
191         return (n % TIPC_MIN_LINK_WIN) == tipc_own_tag;
192 }
193
194
195 /**
196  * bclink_retransmit_pkt - retransmit broadcast packets
197  * @after: sequence number of last packet to *not* retransmit
198  * @to: sequence number of last packet to retransmit
199  *
200  * Called with bc_lock locked
201  */
202
203 static void bclink_retransmit_pkt(u32 after, u32 to)
204 {
205         struct sk_buff *buf;
206
207         buf = bcl->first_out;
208         while (buf && less_eq(buf_seqno(buf), after)) {
209                 buf = buf->next;
210         }
211         tipc_link_retransmit(bcl, buf, mod(to - after));
212 }
213
214 /**
215  * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
216  * @n_ptr: node that sent acknowledgement info
217  * @acked: broadcast sequence # that has been acknowledged
218  *
219  * Node is locked, bc_lock unlocked.
220  */
221
222 void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
223 {
224         struct sk_buff *crs;
225         struct sk_buff *next;
226         unsigned int released = 0;
227
228         if (less_eq(acked, n_ptr->bclink.acked))
229                 return;
230
231         spin_lock_bh(&bc_lock);
232
233         /* Skip over packets that node has previously acknowledged */
234
235         crs = bcl->first_out;
236         while (crs && less_eq(buf_seqno(crs), n_ptr->bclink.acked)) {
237                 crs = crs->next;
238         }
239
240         /* Update packets that node is now acknowledging */
241
242         while (crs && less_eq(buf_seqno(crs), acked)) {
243                 next = crs->next;
244                 bcbuf_decr_acks(crs);
245                 if (bcbuf_acks(crs) == 0) {
246                         bcl->first_out = next;
247                         bcl->out_queue_size--;
248                         buf_discard(crs);
249                         released = 1;
250                 }
251                 crs = next;
252         }
253         n_ptr->bclink.acked = acked;
254
255         /* Try resolving broadcast link congestion, if necessary */
256
257         if (unlikely(bcl->next_out)) {
258                 tipc_link_push_queue(bcl);
259                 bclink_set_last_sent();
260         }
261         if (unlikely(released && !list_empty(&bcl->waiting_ports)))
262                 tipc_link_wakeup_ports(bcl, 0);
263         spin_unlock_bh(&bc_lock);
264 }
265
266 /**
267  * bclink_send_ack - unicast an ACK msg
268  *
269  * tipc_net_lock and node lock set
270  */
271
272 static void bclink_send_ack(struct tipc_node *n_ptr)
273 {
274         struct link *l_ptr = n_ptr->active_links[n_ptr->addr & 1];
275
276         if (l_ptr != NULL)
277                 tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
278 }
279
280 /**
281  * bclink_send_nack- broadcast a NACK msg
282  *
283  * tipc_net_lock and node lock set
284  */
285
286 static void bclink_send_nack(struct tipc_node *n_ptr)
287 {
288         struct sk_buff *buf;
289         struct tipc_msg *msg;
290
291         if (!less(n_ptr->bclink.gap_after, n_ptr->bclink.gap_to))
292                 return;
293
294         buf = tipc_buf_acquire(INT_H_SIZE);
295         if (buf) {
296                 msg = buf_msg(buf);
297                 tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG,
298                          INT_H_SIZE, n_ptr->addr);
299                 msg_set_mc_netid(msg, tipc_net_id);
300                 msg_set_bcast_ack(msg, mod(n_ptr->bclink.last_in));
301                 msg_set_bcgap_after(msg, n_ptr->bclink.gap_after);
302                 msg_set_bcgap_to(msg, n_ptr->bclink.gap_to);
303                 msg_set_bcast_tag(msg, tipc_own_tag);
304
305                 if (tipc_bearer_send(&bcbearer->bearer, buf, NULL)) {
306                         bcl->stats.sent_nacks++;
307                         buf_discard(buf);
308                 } else {
309                         tipc_bearer_schedule(bcl->b_ptr, bcl);
310                         bcl->proto_msg_queue = buf;
311                         bcl->stats.bearer_congs++;
312                 }
313
314                 /*
315                  * Ensure we doesn't send another NACK msg to the node
316                  * until 16 more deferred messages arrive from it
317                  * (i.e. helps prevent all nodes from NACK'ing at same time)
318                  */
319
320                 n_ptr->bclink.nack_sync = tipc_own_tag;
321         }
322 }
323
324 /**
325  * tipc_bclink_check_gap - send a NACK if a sequence gap exists
326  *
327  * tipc_net_lock and node lock set
328  */
329
330 void tipc_bclink_check_gap(struct tipc_node *n_ptr, u32 last_sent)
331 {
332         if (!n_ptr->bclink.supported ||
333             less_eq(last_sent, mod(n_ptr->bclink.last_in)))
334                 return;
335
336         bclink_set_gap(n_ptr);
337         if (n_ptr->bclink.gap_after == n_ptr->bclink.gap_to)
338                 n_ptr->bclink.gap_to = last_sent;
339         bclink_send_nack(n_ptr);
340 }
341
342 /**
343  * tipc_bclink_peek_nack - process a NACK msg meant for another node
344  *
345  * Only tipc_net_lock set.
346  */
347
348 static void tipc_bclink_peek_nack(u32 dest, u32 sender_tag, u32 gap_after, u32 gap_to)
349 {
350         struct tipc_node *n_ptr = tipc_node_find(dest);
351         u32 my_after, my_to;
352
353         if (unlikely(!n_ptr || !tipc_node_is_up(n_ptr)))
354                 return;
355         tipc_node_lock(n_ptr);
356         /*
357          * Modify gap to suppress unnecessary NACKs from this node
358          */
359         my_after = n_ptr->bclink.gap_after;
360         my_to = n_ptr->bclink.gap_to;
361
362         if (less_eq(gap_after, my_after)) {
363                 if (less(my_after, gap_to) && less(gap_to, my_to))
364                         n_ptr->bclink.gap_after = gap_to;
365                 else if (less_eq(my_to, gap_to))
366                         n_ptr->bclink.gap_to = n_ptr->bclink.gap_after;
367         } else if (less_eq(gap_after, my_to)) {
368                 if (less_eq(my_to, gap_to))
369                         n_ptr->bclink.gap_to = gap_after;
370         } else {
371                 /*
372                  * Expand gap if missing bufs not in deferred queue:
373                  */
374                 struct sk_buff *buf = n_ptr->bclink.deferred_head;
375                 u32 prev = n_ptr->bclink.gap_to;
376
377                 for (; buf; buf = buf->next) {
378                         u32 seqno = buf_seqno(buf);
379
380                         if (mod(seqno - prev) != 1) {
381                                 buf = NULL;
382                                 break;
383                         }
384                         if (seqno == gap_after)
385                                 break;
386                         prev = seqno;
387                 }
388                 if (buf == NULL)
389                         n_ptr->bclink.gap_to = gap_after;
390         }
391         /*
392          * Some nodes may send a complementary NACK now:
393          */
394         if (bclink_ack_allowed(sender_tag + 1)) {
395                 if (n_ptr->bclink.gap_to != n_ptr->bclink.gap_after) {
396                         bclink_send_nack(n_ptr);
397                         bclink_set_gap(n_ptr);
398                 }
399         }
400         tipc_node_unlock(n_ptr);
401 }
402
403 /**
404  * tipc_bclink_send_msg - broadcast a packet to all nodes in cluster
405  */
406
407 int tipc_bclink_send_msg(struct sk_buff *buf)
408 {
409         int res;
410
411         spin_lock_bh(&bc_lock);
412
413         res = tipc_link_send_buf(bcl, buf);
414         if (unlikely(res == -ELINKCONG))
415                 buf_discard(buf);
416         else
417                 bclink_set_last_sent();
418
419         if (bcl->out_queue_size > bcl->stats.max_queue_sz)
420                 bcl->stats.max_queue_sz = bcl->out_queue_size;
421         bcl->stats.queue_sz_counts++;
422         bcl->stats.accu_queue_sz += bcl->out_queue_size;
423
424         spin_unlock_bh(&bc_lock);
425         return res;
426 }
427
428 /**
429  * tipc_bclink_recv_pkt - receive a broadcast packet, and deliver upwards
430  *
431  * tipc_net_lock is read_locked, no other locks set
432  */
433
434 void tipc_bclink_recv_pkt(struct sk_buff *buf)
435 {
436 #if (TIPC_BCAST_LOSS_RATE)
437         static int rx_count = 0;
438 #endif
439         struct tipc_msg *msg = buf_msg(buf);
440         struct tipc_node* node = tipc_node_find(msg_prevnode(msg));
441         u32 next_in;
442         u32 seqno;
443         struct sk_buff *deferred;
444
445         msg_dbg(msg, "<BC<<<");
446
447         if (unlikely(!node || !tipc_node_is_up(node) || !node->bclink.supported ||
448                      (msg_mc_netid(msg) != tipc_net_id))) {
449                 buf_discard(buf);
450                 return;
451         }
452
453         if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
454                 msg_dbg(msg, "<BCNACK<<<");
455                 if (msg_destnode(msg) == tipc_own_addr) {
456                         tipc_node_lock(node);
457                         tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
458                         tipc_node_unlock(node);
459                         spin_lock_bh(&bc_lock);
460                         bcl->stats.recv_nacks++;
461                         bcl->owner->next = node;   /* remember requestor */
462                         bclink_retransmit_pkt(msg_bcgap_after(msg),
463                                               msg_bcgap_to(msg));
464                         bcl->owner->next = NULL;
465                         spin_unlock_bh(&bc_lock);
466                 } else {
467                         tipc_bclink_peek_nack(msg_destnode(msg),
468                                               msg_bcast_tag(msg),
469                                               msg_bcgap_after(msg),
470                                               msg_bcgap_to(msg));
471                 }
472                 buf_discard(buf);
473                 return;
474         }
475
476 #if (TIPC_BCAST_LOSS_RATE)
477         if (++rx_count == TIPC_BCAST_LOSS_RATE) {
478                 rx_count = 0;
479                 buf_discard(buf);
480                 return;
481         }
482 #endif
483
484         tipc_node_lock(node);
485 receive:
486         deferred = node->bclink.deferred_head;
487         next_in = mod(node->bclink.last_in + 1);
488         seqno = msg_seqno(msg);
489
490         if (likely(seqno == next_in)) {
491                 bcl->stats.recv_info++;
492                 node->bclink.last_in++;
493                 bclink_set_gap(node);
494                 if (unlikely(bclink_ack_allowed(seqno))) {
495                         bclink_send_ack(node);
496                         bcl->stats.sent_acks++;
497                 }
498                 if (likely(msg_isdata(msg))) {
499                         tipc_node_unlock(node);
500                         tipc_port_recv_mcast(buf, NULL);
501                 } else if (msg_user(msg) == MSG_BUNDLER) {
502                         bcl->stats.recv_bundles++;
503                         bcl->stats.recv_bundled += msg_msgcnt(msg);
504                         tipc_node_unlock(node);
505                         tipc_link_recv_bundle(buf);
506                 } else if (msg_user(msg) == MSG_FRAGMENTER) {
507                         bcl->stats.recv_fragments++;
508                         if (tipc_link_recv_fragment(&node->bclink.defragm,
509                                                     &buf, &msg))
510                                 bcl->stats.recv_fragmented++;
511                         tipc_node_unlock(node);
512                         tipc_net_route_msg(buf);
513                 } else {
514                         tipc_node_unlock(node);
515                         tipc_net_route_msg(buf);
516                 }
517                 if (deferred && (buf_seqno(deferred) == mod(next_in + 1))) {
518                         tipc_node_lock(node);
519                         buf = deferred;
520                         msg = buf_msg(buf);
521                         node->bclink.deferred_head = deferred->next;
522                         goto receive;
523                 }
524                 return;
525         } else if (less(next_in, seqno)) {
526                 u32 gap_after = node->bclink.gap_after;
527                 u32 gap_to = node->bclink.gap_to;
528
529                 if (tipc_link_defer_pkt(&node->bclink.deferred_head,
530                                         &node->bclink.deferred_tail,
531                                         buf)) {
532                         node->bclink.nack_sync++;
533                         bcl->stats.deferred_recv++;
534                         if (seqno == mod(gap_after + 1))
535                                 node->bclink.gap_after = seqno;
536                         else if (less(gap_after, seqno) && less(seqno, gap_to))
537                                 node->bclink.gap_to = seqno;
538                 }
539                 if (bclink_ack_allowed(node->bclink.nack_sync)) {
540                         if (gap_to != gap_after)
541                                 bclink_send_nack(node);
542                         bclink_set_gap(node);
543                 }
544         } else {
545                 bcl->stats.duplicates++;
546                 buf_discard(buf);
547         }
548         tipc_node_unlock(node);
549 }
550
551 u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
552 {
553         return (n_ptr->bclink.supported &&
554                 (tipc_bclink_get_last_sent() != n_ptr->bclink.acked));
555 }
556
557
558 /**
559  * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
560  *
561  * Send through as many bearers as necessary to reach all nodes
562  * that support TIPC multicasting.
563  *
564  * Returns 0 if packet sent successfully, non-zero if not
565  */
566
567 static int tipc_bcbearer_send(struct sk_buff *buf,
568                               struct tipc_bearer *unused1,
569                               struct tipc_media_addr *unused2)
570 {
571         int bp_index;
572
573         /* Prepare buffer for broadcasting (if first time trying to send it) */
574
575         if (likely(!msg_non_seq(buf_msg(buf)))) {
576                 struct tipc_msg *msg;
577
578                 assert(tipc_cltr_bcast_nodes.count != 0);
579                 bcbuf_set_acks(buf, tipc_cltr_bcast_nodes.count);
580                 msg = buf_msg(buf);
581                 msg_set_non_seq(msg, 1);
582                 msg_set_mc_netid(msg, tipc_net_id);
583                 bcl->stats.sent_info++;
584         }
585
586         /* Send buffer over bearers until all targets reached */
587
588         bcbearer->remains = tipc_cltr_bcast_nodes;
589
590         for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
591                 struct bearer *p = bcbearer->bpairs[bp_index].primary;
592                 struct bearer *s = bcbearer->bpairs[bp_index].secondary;
593
594                 if (!p)
595                         break;  /* no more bearers to try */
596
597                 tipc_nmap_diff(&bcbearer->remains, &p->nodes, &bcbearer->remains_new);
598                 if (bcbearer->remains_new.count == bcbearer->remains.count)
599                         continue;       /* bearer pair doesn't add anything */
600
601                 if (p->publ.blocked ||
602                     p->media->send_msg(buf, &p->publ, &p->media->bcast_addr)) {
603                         /* unable to send on primary bearer */
604                         if (!s || s->publ.blocked ||
605                             s->media->send_msg(buf, &s->publ,
606                                                &s->media->bcast_addr)) {
607                                 /* unable to send on either bearer */
608                                 continue;
609                         }
610                 }
611
612                 if (s) {
613                         bcbearer->bpairs[bp_index].primary = s;
614                         bcbearer->bpairs[bp_index].secondary = p;
615                 }
616
617                 if (bcbearer->remains_new.count == 0)
618                         return 0;
619
620                 bcbearer->remains = bcbearer->remains_new;
621         }
622
623         /*
624          * Unable to reach all targets (indicate success, since currently
625          * there isn't code in place to properly block & unblock the
626          * pseudo-bearer used by the broadcast link)
627          */
628
629         return TIPC_OK;
630 }
631
632 /**
633  * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
634  */
635
636 void tipc_bcbearer_sort(void)
637 {
638         struct bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
639         struct bcbearer_pair *bp_curr;
640         int b_index;
641         int pri;
642
643         spin_lock_bh(&bc_lock);
644
645         /* Group bearers by priority (can assume max of two per priority) */
646
647         memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
648
649         for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
650                 struct bearer *b = &tipc_bearers[b_index];
651
652                 if (!b->active || !b->nodes.count)
653                         continue;
654
655                 if (!bp_temp[b->priority].primary)
656                         bp_temp[b->priority].primary = b;
657                 else
658                         bp_temp[b->priority].secondary = b;
659         }
660
661         /* Create array of bearer pairs for broadcasting */
662
663         bp_curr = bcbearer->bpairs;
664         memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
665
666         for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
667
668                 if (!bp_temp[pri].primary)
669                         continue;
670
671                 bp_curr->primary = bp_temp[pri].primary;
672
673                 if (bp_temp[pri].secondary) {
674                         if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
675                                             &bp_temp[pri].secondary->nodes)) {
676                                 bp_curr->secondary = bp_temp[pri].secondary;
677                         } else {
678                                 bp_curr++;
679                                 bp_curr->primary = bp_temp[pri].secondary;
680                         }
681                 }
682
683                 bp_curr++;
684         }
685
686         spin_unlock_bh(&bc_lock);
687 }
688
689 /**
690  * tipc_bcbearer_push - resolve bearer congestion
691  *
692  * Forces bclink to push out any unsent packets, until all packets are gone
693  * or congestion reoccurs.
694  * No locks set when function called
695  */
696
697 void tipc_bcbearer_push(void)
698 {
699         struct bearer *b_ptr;
700
701         spin_lock_bh(&bc_lock);
702         b_ptr = &bcbearer->bearer;
703         if (b_ptr->publ.blocked) {
704                 b_ptr->publ.blocked = 0;
705                 tipc_bearer_lock_push(b_ptr);
706         }
707         spin_unlock_bh(&bc_lock);
708 }
709
710
711 int tipc_bclink_stats(char *buf, const u32 buf_size)
712 {
713         struct print_buf pb;
714
715         if (!bcl)
716                 return 0;
717
718         tipc_printbuf_init(&pb, buf, buf_size);
719
720         spin_lock_bh(&bc_lock);
721
722         tipc_printf(&pb, "Link <%s>\n"
723                          "  Window:%u packets\n",
724                     bcl->name, bcl->queue_limit[0]);
725         tipc_printf(&pb, "  RX packets:%u fragments:%u/%u bundles:%u/%u\n",
726                     bcl->stats.recv_info,
727                     bcl->stats.recv_fragments,
728                     bcl->stats.recv_fragmented,
729                     bcl->stats.recv_bundles,
730                     bcl->stats.recv_bundled);
731         tipc_printf(&pb, "  TX packets:%u fragments:%u/%u bundles:%u/%u\n",
732                     bcl->stats.sent_info,
733                     bcl->stats.sent_fragments,
734                     bcl->stats.sent_fragmented,
735                     bcl->stats.sent_bundles,
736                     bcl->stats.sent_bundled);
737         tipc_printf(&pb, "  RX naks:%u defs:%u dups:%u\n",
738                     bcl->stats.recv_nacks,
739                     bcl->stats.deferred_recv,
740                     bcl->stats.duplicates);
741         tipc_printf(&pb, "  TX naks:%u acks:%u dups:%u\n",
742                     bcl->stats.sent_nacks,
743                     bcl->stats.sent_acks,
744                     bcl->stats.retransmitted);
745         tipc_printf(&pb, "  Congestion bearer:%u link:%u  Send queue max:%u avg:%u\n",
746                     bcl->stats.bearer_congs,
747                     bcl->stats.link_congs,
748                     bcl->stats.max_queue_sz,
749                     bcl->stats.queue_sz_counts
750                     ? (bcl->stats.accu_queue_sz / bcl->stats.queue_sz_counts)
751                     : 0);
752
753         spin_unlock_bh(&bc_lock);
754         return tipc_printbuf_validate(&pb);
755 }
756
757 int tipc_bclink_reset_stats(void)
758 {
759         if (!bcl)
760                 return -ENOPROTOOPT;
761
762         spin_lock_bh(&bc_lock);
763         memset(&bcl->stats, 0, sizeof(bcl->stats));
764         spin_unlock_bh(&bc_lock);
765         return 0;
766 }
767
768 int tipc_bclink_set_queue_limits(u32 limit)
769 {
770         if (!bcl)
771                 return -ENOPROTOOPT;
772         if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
773                 return -EINVAL;
774
775         spin_lock_bh(&bc_lock);
776         tipc_link_set_queue_limits(bcl, limit);
777         spin_unlock_bh(&bc_lock);
778         return 0;
779 }
780
781 int tipc_bclink_init(void)
782 {
783         bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
784         bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
785         if (!bcbearer || !bclink) {
786  nomem:
787                 warn("Multicast link creation failed, no memory\n");
788                 kfree(bcbearer);
789                 bcbearer = NULL;
790                 kfree(bclink);
791                 bclink = NULL;
792                 return -ENOMEM;
793         }
794
795         INIT_LIST_HEAD(&bcbearer->bearer.cong_links);
796         bcbearer->bearer.media = &bcbearer->media;
797         bcbearer->media.send_msg = tipc_bcbearer_send;
798         sprintf(bcbearer->media.name, "tipc-multicast");
799
800         bcl = &bclink->link;
801         INIT_LIST_HEAD(&bcl->waiting_ports);
802         bcl->next_out_no = 1;
803         spin_lock_init(&bclink->node.lock);
804         bcl->owner = &bclink->node;
805         bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
806         tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
807         bcl->b_ptr = &bcbearer->bearer;
808         bcl->state = WORKING_WORKING;
809         strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
810
811         if (BCLINK_LOG_BUF_SIZE) {
812                 char *pb = kmalloc(BCLINK_LOG_BUF_SIZE, GFP_ATOMIC);
813
814                 if (!pb)
815                         goto nomem;
816                 tipc_printbuf_init(&bcl->print_buf, pb, BCLINK_LOG_BUF_SIZE);
817         }
818
819         return 0;
820 }
821
822 void tipc_bclink_stop(void)
823 {
824         spin_lock_bh(&bc_lock);
825         if (bcbearer) {
826                 tipc_link_stop(bcl);
827                 if (BCLINK_LOG_BUF_SIZE)
828                         kfree(bcl->print_buf.buf);
829                 bcl = NULL;
830                 kfree(bclink);
831                 bclink = NULL;
832                 kfree(bcbearer);
833                 bcbearer = NULL;
834         }
835         spin_unlock_bh(&bc_lock);
836 }
837
838
839 /**
840  * tipc_nmap_add - add a node to a node map
841  */
842
843 void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
844 {
845         int n = tipc_node(node);
846         int w = n / WSIZE;
847         u32 mask = (1 << (n % WSIZE));
848
849         if ((nm_ptr->map[w] & mask) == 0) {
850                 nm_ptr->count++;
851                 nm_ptr->map[w] |= mask;
852         }
853 }
854
855 /**
856  * tipc_nmap_remove - remove a node from a node map
857  */
858
859 void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
860 {
861         int n = tipc_node(node);
862         int w = n / WSIZE;
863         u32 mask = (1 << (n % WSIZE));
864
865         if ((nm_ptr->map[w] & mask) != 0) {
866                 nm_ptr->map[w] &= ~mask;
867                 nm_ptr->count--;
868         }
869 }
870
871 /**
872  * tipc_nmap_diff - find differences between node maps
873  * @nm_a: input node map A
874  * @nm_b: input node map B
875  * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
876  */
877
878 static void tipc_nmap_diff(struct tipc_node_map *nm_a,
879                            struct tipc_node_map *nm_b,
880                            struct tipc_node_map *nm_diff)
881 {
882         int stop = ARRAY_SIZE(nm_a->map);
883         int w;
884         int b;
885         u32 map;
886
887         memset(nm_diff, 0, sizeof(*nm_diff));
888         for (w = 0; w < stop; w++) {
889                 map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
890                 nm_diff->map[w] = map;
891                 if (map != 0) {
892                         for (b = 0 ; b < WSIZE; b++) {
893                                 if (map & (1 << b))
894                                         nm_diff->count++;
895                         }
896                 }
897         }
898 }
899
900 /**
901  * tipc_port_list_add - add a port to a port list, ensuring no duplicates
902  */
903
904 void tipc_port_list_add(struct port_list *pl_ptr, u32 port)
905 {
906         struct port_list *item = pl_ptr;
907         int i;
908         int item_sz = PLSIZE;
909         int cnt = pl_ptr->count;
910
911         for (; ; cnt -= item_sz, item = item->next) {
912                 if (cnt < PLSIZE)
913                         item_sz = cnt;
914                 for (i = 0; i < item_sz; i++)
915                         if (item->ports[i] == port)
916                                 return;
917                 if (i < PLSIZE) {
918                         item->ports[i] = port;
919                         pl_ptr->count++;
920                         return;
921                 }
922                 if (!item->next) {
923                         item->next = kmalloc(sizeof(*item), GFP_ATOMIC);
924                         if (!item->next) {
925                                 warn("Incomplete multicast delivery, no memory\n");
926                                 return;
927                         }
928                         item->next->next = NULL;
929                 }
930         }
931 }
932
933 /**
934  * tipc_port_list_free - free dynamically created entries in port_list chain
935  *
936  */
937
938 void tipc_port_list_free(struct port_list *pl_ptr)
939 {
940         struct port_list *item;
941         struct port_list *next;
942
943         for (item = pl_ptr->next; item; item = next) {
944                 next = item->next;
945                 kfree(item);
946         }
947 }
948