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