bcast.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. #include "core.h"
  38. #include "msg.h"
  39. #include "dbg.h"
  40. #include "link.h"
  41. #include "net.h"
  42. #include "node.h"
  43. #include "port.h"
  44. #include "addr.h"
  45. #include "node_subscr.h"
  46. #include "name_distr.h"
  47. #include "bearer.h"
  48. #include "name_table.h"
  49. #include "bcast.h"
  50. #define MAX_PKT_DEFAULT_MCAST 1500 /* bcast link max packet size (fixed) */
  51. #define BCLINK_WIN_DEFAULT 20 /* bcast link window size (default) */
  52. #define BCLINK_LOG_BUF_SIZE 0
  53. /*
  54. * Loss rate for incoming broadcast frames; used to test retransmission code.
  55. * Set to N to cause every N'th frame to be discarded; 0 => don't discard any.
  56. */
  57. #define TIPC_BCAST_LOSS_RATE 0
  58. /**
  59. * struct bcbearer_pair - a pair of bearers used by broadcast link
  60. * @primary: pointer to primary bearer
  61. * @secondary: pointer to secondary bearer
  62. *
  63. * Bearers must have same priority and same set of reachable destinations
  64. * to be paired.
  65. */
  66. struct bcbearer_pair {
  67. struct bearer *primary;
  68. struct bearer *secondary;
  69. };
  70. /**
  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"
  76. */
  77. struct bcbearer {
  78. struct bearer bearer;
  79. struct media media;
  80. struct bcbearer_pair bpairs[MAX_BEARERS];
  81. struct bcbearer_pair bpairs_temp[TIPC_MAX_LINK_PRI + 1];
  82. };
  83. /**
  84. * struct bclink - link used for broadcast messages
  85. * @link: (non-standard) broadcast link structure
  86. * @node: (non-standard) node structure representing b'cast link's peer node
  87. *
  88. * Handles sequence numbering, fragmentation, bundling, etc.
  89. */
  90. struct bclink {
  91. struct link link;
  92. struct node node;
  93. };
  94. static struct bcbearer *bcbearer = NULL;
  95. static struct bclink *bclink = NULL;
  96. static struct link *bcl = NULL;
  97. static spinlock_t bc_lock = SPIN_LOCK_UNLOCKED;
  98. char tipc_bclink_name[] = "multicast-link";
  99. static u32 buf_seqno(struct sk_buff *buf)
  100. {
  101. return msg_seqno(buf_msg(buf));
  102. }
  103. static u32 bcbuf_acks(struct sk_buff *buf)
  104. {
  105. return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
  106. }
  107. static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
  108. {
  109. TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
  110. }
  111. static void bcbuf_decr_acks(struct sk_buff *buf)
  112. {
  113. bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
  114. }
  115. /**
  116. * bclink_set_gap - set gap according to contents of current deferred pkt queue
  117. *
  118. * Called with 'node' locked, bc_lock unlocked
  119. */
  120. static void bclink_set_gap(struct node *n_ptr)
  121. {
  122. struct sk_buff *buf = n_ptr->bclink.deferred_head;
  123. n_ptr->bclink.gap_after = n_ptr->bclink.gap_to =
  124. mod(n_ptr->bclink.last_in);
  125. if (unlikely(buf != NULL))
  126. n_ptr->bclink.gap_to = mod(buf_seqno(buf) - 1);
  127. }
  128. /**
  129. * bclink_ack_allowed - test if ACK or NACK message can be sent at this moment
  130. *
  131. * This mechanism endeavours to prevent all nodes in network from trying
  132. * to ACK or NACK at the same time.
  133. *
  134. * Note: TIPC uses a different trigger to distribute ACKs than it does to
  135. * distribute NACKs, but tries to use the same spacing (divide by 16).
  136. */
  137. static int bclink_ack_allowed(u32 n)
  138. {
  139. return((n % TIPC_MIN_LINK_WIN) == tipc_own_tag);
  140. }
  141. /**
  142. * bclink_retransmit_pkt - retransmit broadcast packets
  143. * @after: sequence number of last packet to *not* retransmit
  144. * @to: sequence number of last packet to retransmit
  145. *
  146. * Called with bc_lock locked
  147. */
  148. static void bclink_retransmit_pkt(u32 after, u32 to)
  149. {
  150. struct sk_buff *buf;
  151. buf = bcl->first_out;
  152. while (buf && less_eq(buf_seqno(buf), after)) {
  153. buf = buf->next;
  154. }
  155. tipc_link_retransmit(bcl, buf, mod(to - after));
  156. }
  157. /**
  158. * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
  159. * @n_ptr: node that sent acknowledgement info
  160. * @acked: broadcast sequence # that has been acknowledged
  161. *
  162. * Node is locked, bc_lock unlocked.
  163. */
  164. void tipc_bclink_acknowledge(struct node *n_ptr, u32 acked)
  165. {
  166. struct sk_buff *crs;
  167. struct sk_buff *next;
  168. unsigned int released = 0;
  169. if (less_eq(acked, n_ptr->bclink.acked))
  170. return;
  171. spin_lock_bh(&bc_lock);
  172. /* Skip over packets that node has previously acknowledged */
  173. crs = bcl->first_out;
  174. while (crs && less_eq(buf_seqno(crs), n_ptr->bclink.acked)) {
  175. crs = crs->next;
  176. }
  177. /* Update packets that node is now acknowledging */
  178. while (crs && less_eq(buf_seqno(crs), acked)) {
  179. next = crs->next;
  180. bcbuf_decr_acks(crs);
  181. if (bcbuf_acks(crs) == 0) {
  182. bcl->first_out = next;
  183. bcl->out_queue_size--;
  184. buf_discard(crs);
  185. released = 1;
  186. }
  187. crs = next;
  188. }
  189. n_ptr->bclink.acked = acked;
  190. /* Try resolving broadcast link congestion, if necessary */
  191. if (unlikely(bcl->next_out))
  192. tipc_link_push_queue(bcl);
  193. if (unlikely(released && !list_empty(&bcl->waiting_ports)))
  194. tipc_link_wakeup_ports(bcl, 0);
  195. spin_unlock_bh(&bc_lock);
  196. }
  197. /**
  198. * bclink_send_ack - unicast an ACK msg
  199. *
  200. * tipc_net_lock and node lock set
  201. */
  202. static void bclink_send_ack(struct node *n_ptr)
  203. {
  204. struct link *l_ptr = n_ptr->active_links[n_ptr->addr & 1];
  205. if (l_ptr != NULL)
  206. tipc_link_send_proto_msg(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
  207. }
  208. /**
  209. * bclink_send_nack- broadcast a NACK msg
  210. *
  211. * tipc_net_lock and node lock set
  212. */
  213. static void bclink_send_nack(struct node *n_ptr)
  214. {
  215. struct sk_buff *buf;
  216. struct tipc_msg *msg;
  217. if (!less(n_ptr->bclink.gap_after, n_ptr->bclink.gap_to))
  218. return;
  219. buf = buf_acquire(INT_H_SIZE);
  220. if (buf) {
  221. msg = buf_msg(buf);
  222. msg_init(msg, BCAST_PROTOCOL, STATE_MSG,
  223. TIPC_OK, INT_H_SIZE, n_ptr->addr);
  224. msg_set_mc_netid(msg, tipc_net_id);
  225. msg_set_bcast_ack(msg, mod(n_ptr->bclink.last_in));
  226. msg_set_bcgap_after(msg, n_ptr->bclink.gap_after);
  227. msg_set_bcgap_to(msg, n_ptr->bclink.gap_to);
  228. msg_set_bcast_tag(msg, tipc_own_tag);
  229. if (tipc_bearer_send(&bcbearer->bearer, buf, NULL)) {
  230. bcl->stats.sent_nacks++;
  231. buf_discard(buf);
  232. } else {
  233. tipc_bearer_schedule(bcl->b_ptr, bcl);
  234. bcl->proto_msg_queue = buf;
  235. bcl->stats.bearer_congs++;
  236. }
  237. /*
  238. * Ensure we doesn't send another NACK msg to the node
  239. * until 16 more deferred messages arrive from it
  240. * (i.e. helps prevent all nodes from NACK'ing at same time)
  241. */
  242. n_ptr->bclink.nack_sync = tipc_own_tag;
  243. }
  244. }
  245. /**
  246. * tipc_bclink_check_gap - send a NACK if a sequence gap exists
  247. *
  248. * tipc_net_lock and node lock set
  249. */
  250. void tipc_bclink_check_gap(struct node *n_ptr, u32 last_sent)
  251. {
  252. if (!n_ptr->bclink.supported ||
  253. less_eq(last_sent, mod(n_ptr->bclink.last_in)))
  254. return;
  255. bclink_set_gap(n_ptr);
  256. if (n_ptr->bclink.gap_after == n_ptr->bclink.gap_to)
  257. n_ptr->bclink.gap_to = last_sent;
  258. bclink_send_nack(n_ptr);
  259. }
  260. /**
  261. * tipc_bclink_peek_nack - process a NACK msg meant for another node
  262. *
  263. * Only tipc_net_lock set.
  264. */
  265. static void tipc_bclink_peek_nack(u32 dest, u32 sender_tag, u32 gap_after, u32 gap_to)
  266. {
  267. struct node *n_ptr = tipc_node_find(dest);
  268. u32 my_after, my_to;
  269. if (unlikely(!n_ptr || !tipc_node_is_up(n_ptr)))
  270. return;
  271. tipc_node_lock(n_ptr);
  272. /*
  273. * Modify gap to suppress unnecessary NACKs from this node
  274. */
  275. my_after = n_ptr->bclink.gap_after;
  276. my_to = n_ptr->bclink.gap_to;
  277. if (less_eq(gap_after, my_after)) {
  278. if (less(my_after, gap_to) && less(gap_to, my_to))
  279. n_ptr->bclink.gap_after = gap_to;
  280. else if (less_eq(my_to, gap_to))
  281. n_ptr->bclink.gap_to = n_ptr->bclink.gap_after;
  282. } else if (less_eq(gap_after, my_to)) {
  283. if (less_eq(my_to, gap_to))
  284. n_ptr->bclink.gap_to = gap_after;
  285. } else {
  286. /*
  287. * Expand gap if missing bufs not in deferred queue:
  288. */
  289. struct sk_buff *buf = n_ptr->bclink.deferred_head;
  290. u32 prev = n_ptr->bclink.gap_to;
  291. for (; buf; buf = buf->next) {
  292. u32 seqno = buf_seqno(buf);
  293. if (mod(seqno - prev) != 1) {
  294. buf = NULL;
  295. break;
  296. }
  297. if (seqno == gap_after)
  298. break;
  299. prev = seqno;
  300. }
  301. if (buf == NULL)
  302. n_ptr->bclink.gap_to = gap_after;
  303. }
  304. /*
  305. * Some nodes may send a complementary NACK now:
  306. */
  307. if (bclink_ack_allowed(sender_tag + 1)) {
  308. if (n_ptr->bclink.gap_to != n_ptr->bclink.gap_after) {
  309. bclink_send_nack(n_ptr);
  310. bclink_set_gap(n_ptr);
  311. }
  312. }
  313. tipc_node_unlock(n_ptr);
  314. }
  315. /**
  316. * tipc_bclink_send_msg - broadcast a packet to all nodes in cluster
  317. */
  318. int tipc_bclink_send_msg(struct sk_buff *buf)
  319. {
  320. int res;
  321. spin_lock_bh(&bc_lock);
  322. res = tipc_link_send_buf(bcl, buf);
  323. if (unlikely(res == -ELINKCONG))
  324. buf_discard(buf);
  325. else
  326. bcl->stats.sent_info++;
  327. if (bcl->out_queue_size > bcl->stats.max_queue_sz)
  328. bcl->stats.max_queue_sz = bcl->out_queue_size;
  329. bcl->stats.queue_sz_counts++;
  330. bcl->stats.accu_queue_sz += bcl->out_queue_size;
  331. spin_unlock_bh(&bc_lock);
  332. return res;
  333. }
  334. /**
  335. * tipc_bclink_recv_pkt - receive a broadcast packet, and deliver upwards
  336. *
  337. * tipc_net_lock is read_locked, no other locks set
  338. */
  339. void tipc_bclink_recv_pkt(struct sk_buff *buf)
  340. {
  341. #if (TIPC_BCAST_LOSS_RATE)
  342. static int rx_count = 0;
  343. #endif
  344. struct tipc_msg *msg = buf_msg(buf);
  345. struct node* node = tipc_node_find(msg_prevnode(msg));
  346. u32 next_in;
  347. u32 seqno;
  348. struct sk_buff *deferred;
  349. msg_dbg(msg, "<BC<<<");
  350. if (unlikely(!node || !tipc_node_is_up(node) || !node->bclink.supported ||
  351. (msg_mc_netid(msg) != tipc_net_id))) {
  352. buf_discard(buf);
  353. return;
  354. }
  355. if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
  356. msg_dbg(msg, "<BCNACK<<<");
  357. if (msg_destnode(msg) == tipc_own_addr) {
  358. tipc_node_lock(node);
  359. tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
  360. tipc_node_unlock(node);
  361. spin_lock_bh(&bc_lock);
  362. bcl->stats.recv_nacks++;
  363. bcl->owner->next = node; /* remember requestor */
  364. bclink_retransmit_pkt(msg_bcgap_after(msg),
  365. msg_bcgap_to(msg));
  366. bcl->owner->next = NULL;
  367. spin_unlock_bh(&bc_lock);
  368. } else {
  369. tipc_bclink_peek_nack(msg_destnode(msg),
  370. msg_bcast_tag(msg),
  371. msg_bcgap_after(msg),
  372. msg_bcgap_to(msg));
  373. }
  374. buf_discard(buf);
  375. return;
  376. }
  377. #if (TIPC_BCAST_LOSS_RATE)
  378. if (++rx_count == TIPC_BCAST_LOSS_RATE) {
  379. rx_count = 0;
  380. buf_discard(buf);
  381. return;
  382. }
  383. #endif
  384. tipc_node_lock(node);
  385. receive:
  386. deferred = node->bclink.deferred_head;
  387. next_in = mod(node->bclink.last_in + 1);
  388. seqno = msg_seqno(msg);
  389. if (likely(seqno == next_in)) {
  390. bcl->stats.recv_info++;
  391. node->bclink.last_in++;
  392. bclink_set_gap(node);
  393. if (unlikely(bclink_ack_allowed(seqno))) {
  394. bclink_send_ack(node);
  395. bcl->stats.sent_acks++;
  396. }
  397. if (likely(msg_isdata(msg))) {
  398. tipc_node_unlock(node);
  399. tipc_port_recv_mcast(buf, NULL);
  400. } else if (msg_user(msg) == MSG_BUNDLER) {
  401. bcl->stats.recv_bundles++;
  402. bcl->stats.recv_bundled += msg_msgcnt(msg);
  403. tipc_node_unlock(node);
  404. tipc_link_recv_bundle(buf);
  405. } else if (msg_user(msg) == MSG_FRAGMENTER) {
  406. bcl->stats.recv_fragments++;
  407. if (tipc_link_recv_fragment(&node->bclink.defragm,
  408. &buf, &msg))
  409. bcl->stats.recv_fragmented++;
  410. tipc_node_unlock(node);
  411. tipc_net_route_msg(buf);
  412. } else {
  413. tipc_node_unlock(node);
  414. tipc_net_route_msg(buf);
  415. }
  416. if (deferred && (buf_seqno(deferred) == mod(next_in + 1))) {
  417. tipc_node_lock(node);
  418. buf = deferred;
  419. msg = buf_msg(buf);
  420. node->bclink.deferred_head = deferred->next;
  421. goto receive;
  422. }
  423. return;
  424. } else if (less(next_in, seqno)) {
  425. u32 gap_after = node->bclink.gap_after;
  426. u32 gap_to = node->bclink.gap_to;
  427. if (tipc_link_defer_pkt(&node->bclink.deferred_head,
  428. &node->bclink.deferred_tail,
  429. buf)) {
  430. node->bclink.nack_sync++;
  431. bcl->stats.deferred_recv++;
  432. if (seqno == mod(gap_after + 1))
  433. node->bclink.gap_after = seqno;
  434. else if (less(gap_after, seqno) && less(seqno, gap_to))
  435. node->bclink.gap_to = seqno;
  436. }
  437. if (bclink_ack_allowed(node->bclink.nack_sync)) {
  438. if (gap_to != gap_after)
  439. bclink_send_nack(node);
  440. bclink_set_gap(node);
  441. }
  442. } else {
  443. bcl->stats.duplicates++;
  444. buf_discard(buf);
  445. }
  446. tipc_node_unlock(node);
  447. }
  448. u32 tipc_bclink_get_last_sent(void)
  449. {
  450. u32 last_sent = mod(bcl->next_out_no - 1);
  451. if (bcl->next_out)
  452. last_sent = mod(buf_seqno(bcl->next_out) - 1);
  453. return last_sent;
  454. }
  455. u32 tipc_bclink_acks_missing(struct node *n_ptr)
  456. {
  457. return (n_ptr->bclink.supported &&
  458. (tipc_bclink_get_last_sent() != n_ptr->bclink.acked));
  459. }
  460. /**
  461. * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
  462. *
  463. * Send through as many bearers as necessary to reach all nodes
  464. * that support TIPC multicasting.
  465. *
  466. * Returns 0 if packet sent successfully, non-zero if not
  467. */
  468. static int tipc_bcbearer_send(struct sk_buff *buf,
  469. struct tipc_bearer *unused1,
  470. struct tipc_media_addr *unused2)
  471. {
  472. static int send_count = 0;
  473. struct node_map *remains;
  474. struct node_map *remains_new;
  475. struct node_map *remains_tmp;
  476. int bp_index;
  477. int swap_time;
  478. int err;
  479. /* Prepare buffer for broadcasting (if first time trying to send it) */
  480. if (likely(!msg_non_seq(buf_msg(buf)))) {
  481. struct tipc_msg *msg;
  482. assert(tipc_cltr_bcast_nodes.count != 0);
  483. bcbuf_set_acks(buf, tipc_cltr_bcast_nodes.count);
  484. msg = buf_msg(buf);
  485. msg_set_non_seq(msg);
  486. msg_set_mc_netid(msg, tipc_net_id);
  487. }
  488. /* Determine if bearer pairs should be swapped following this attempt */
  489. if ((swap_time = (++send_count >= 10)))
  490. send_count = 0;
  491. /* Send buffer over bearers until all targets reached */
  492. remains = kmalloc(sizeof(struct node_map), GFP_ATOMIC);
  493. remains_new = kmalloc(sizeof(struct node_map), GFP_ATOMIC);
  494. *remains = tipc_cltr_bcast_nodes;
  495. for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
  496. struct bearer *p = bcbearer->bpairs[bp_index].primary;
  497. struct bearer *s = bcbearer->bpairs[bp_index].secondary;
  498. if (!p)
  499. break; /* no more bearers to try */
  500. tipc_nmap_diff(remains, &p->nodes, remains_new);
  501. if (remains_new->count == remains->count)
  502. continue; /* bearer pair doesn't add anything */
  503. if (!p->publ.blocked &&
  504. !p->media->send_msg(buf, &p->publ, &p->media->bcast_addr)) {
  505. if (swap_time && s && !s->publ.blocked)
  506. goto swap;
  507. else
  508. goto update;
  509. }
  510. if (!s || s->publ.blocked ||
  511. s->media->send_msg(buf, &s->publ, &s->media->bcast_addr))
  512. continue; /* unable to send using bearer pair */
  513. swap:
  514. bcbearer->bpairs[bp_index].primary = s;
  515. bcbearer->bpairs[bp_index].secondary = p;
  516. update:
  517. if (remains_new->count == 0) {
  518. err = TIPC_OK;
  519. goto out;
  520. }
  521. /* swap map */
  522. remains_tmp = remains;
  523. remains = remains_new;
  524. remains_new = remains_tmp;
  525. }
  526. /* Unable to reach all targets */
  527. bcbearer->bearer.publ.blocked = 1;
  528. bcl->stats.bearer_congs++;
  529. err = ~TIPC_OK;
  530. out:
  531. kfree(remains_new);
  532. kfree(remains);
  533. return err;
  534. }
  535. /**
  536. * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
  537. */
  538. void tipc_bcbearer_sort(void)
  539. {
  540. struct bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
  541. struct bcbearer_pair *bp_curr;
  542. int b_index;
  543. int pri;
  544. spin_lock_bh(&bc_lock);
  545. /* Group bearers by priority (can assume max of two per priority) */
  546. memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
  547. for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
  548. struct bearer *b = &tipc_bearers[b_index];
  549. if (!b->active || !b->nodes.count)
  550. continue;
  551. if (!bp_temp[b->priority].primary)
  552. bp_temp[b->priority].primary = b;
  553. else
  554. bp_temp[b->priority].secondary = b;
  555. }
  556. /* Create array of bearer pairs for broadcasting */
  557. bp_curr = bcbearer->bpairs;
  558. memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
  559. for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
  560. if (!bp_temp[pri].primary)
  561. continue;
  562. bp_curr->primary = bp_temp[pri].primary;
  563. if (bp_temp[pri].secondary) {
  564. if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
  565. &bp_temp[pri].secondary->nodes)) {
  566. bp_curr->secondary = bp_temp[pri].secondary;
  567. } else {
  568. bp_curr++;
  569. bp_curr->primary = bp_temp[pri].secondary;
  570. }
  571. }
  572. bp_curr++;
  573. }
  574. spin_unlock_bh(&bc_lock);
  575. }
  576. /**
  577. * tipc_bcbearer_push - resolve bearer congestion
  578. *
  579. * Forces bclink to push out any unsent packets, until all packets are gone
  580. * or congestion reoccurs.
  581. * No locks set when function called
  582. */
  583. void tipc_bcbearer_push(void)
  584. {
  585. struct bearer *b_ptr;
  586. spin_lock_bh(&bc_lock);
  587. b_ptr = &bcbearer->bearer;
  588. if (b_ptr->publ.blocked) {
  589. b_ptr->publ.blocked = 0;
  590. tipc_bearer_lock_push(b_ptr);
  591. }
  592. spin_unlock_bh(&bc_lock);
  593. }
  594. int tipc_bclink_stats(char *buf, const u32 buf_size)
  595. {
  596. struct print_buf pb;
  597. if (!bcl)
  598. return 0;
  599. tipc_printbuf_init(&pb, buf, buf_size);
  600. spin_lock_bh(&bc_lock);
  601. tipc_printf(&pb, "Link <%s>\n"
  602. " Window:%u packets\n",
  603. bcl->name, bcl->queue_limit[0]);
  604. tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  605. bcl->stats.recv_info,
  606. bcl->stats.recv_fragments,
  607. bcl->stats.recv_fragmented,
  608. bcl->stats.recv_bundles,
  609. bcl->stats.recv_bundled);
  610. tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  611. bcl->stats.sent_info,
  612. bcl->stats.sent_fragments,
  613. bcl->stats.sent_fragmented,
  614. bcl->stats.sent_bundles,
  615. bcl->stats.sent_bundled);
  616. tipc_printf(&pb, " RX naks:%u defs:%u dups:%u\n",
  617. bcl->stats.recv_nacks,
  618. bcl->stats.deferred_recv,
  619. bcl->stats.duplicates);
  620. tipc_printf(&pb, " TX naks:%u acks:%u dups:%u\n",
  621. bcl->stats.sent_nacks,
  622. bcl->stats.sent_acks,
  623. bcl->stats.retransmitted);
  624. tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
  625. bcl->stats.bearer_congs,
  626. bcl->stats.link_congs,
  627. bcl->stats.max_queue_sz,
  628. bcl->stats.queue_sz_counts
  629. ? (bcl->stats.accu_queue_sz / bcl->stats.queue_sz_counts)
  630. : 0);
  631. spin_unlock_bh(&bc_lock);
  632. return tipc_printbuf_validate(&pb);
  633. }
  634. int tipc_bclink_reset_stats(void)
  635. {
  636. if (!bcl)
  637. return -ENOPROTOOPT;
  638. spin_lock_bh(&bc_lock);
  639. memset(&bcl->stats, 0, sizeof(bcl->stats));
  640. spin_unlock_bh(&bc_lock);
  641. return TIPC_OK;
  642. }
  643. int tipc_bclink_set_queue_limits(u32 limit)
  644. {
  645. if (!bcl)
  646. return -ENOPROTOOPT;
  647. if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
  648. return -EINVAL;
  649. spin_lock_bh(&bc_lock);
  650. tipc_link_set_queue_limits(bcl, limit);
  651. spin_unlock_bh(&bc_lock);
  652. return TIPC_OK;
  653. }
  654. int tipc_bclink_init(void)
  655. {
  656. bcbearer = kmalloc(sizeof(*bcbearer), GFP_ATOMIC);
  657. bclink = kmalloc(sizeof(*bclink), GFP_ATOMIC);
  658. if (!bcbearer || !bclink) {
  659. nomem:
  660. warn("Memory squeeze; Failed to create multicast link\n");
  661. kfree(bcbearer);
  662. bcbearer = NULL;
  663. kfree(bclink);
  664. bclink = NULL;
  665. return -ENOMEM;
  666. }
  667. memset(bcbearer, 0, sizeof(struct bcbearer));
  668. INIT_LIST_HEAD(&bcbearer->bearer.cong_links);
  669. bcbearer->bearer.media = &bcbearer->media;
  670. bcbearer->media.send_msg = tipc_bcbearer_send;
  671. sprintf(bcbearer->media.name, "tipc-multicast");
  672. bcl = &bclink->link;
  673. memset(bclink, 0, sizeof(struct bclink));
  674. INIT_LIST_HEAD(&bcl->waiting_ports);
  675. bcl->next_out_no = 1;
  676. bclink->node.lock = SPIN_LOCK_UNLOCKED;
  677. bcl->owner = &bclink->node;
  678. bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
  679. tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
  680. bcl->b_ptr = &bcbearer->bearer;
  681. bcl->state = WORKING_WORKING;
  682. sprintf(bcl->name, tipc_bclink_name);
  683. if (BCLINK_LOG_BUF_SIZE) {
  684. char *pb = kmalloc(BCLINK_LOG_BUF_SIZE, GFP_ATOMIC);
  685. if (!pb)
  686. goto nomem;
  687. tipc_printbuf_init(&bcl->print_buf, pb, BCLINK_LOG_BUF_SIZE);
  688. }
  689. return TIPC_OK;
  690. }
  691. void tipc_bclink_stop(void)
  692. {
  693. spin_lock_bh(&bc_lock);
  694. if (bcbearer) {
  695. tipc_link_stop(bcl);
  696. if (BCLINK_LOG_BUF_SIZE)
  697. kfree(bcl->print_buf.buf);
  698. bcl = NULL;
  699. kfree(bclink);
  700. bclink = NULL;
  701. kfree(bcbearer);
  702. bcbearer = NULL;
  703. }
  704. spin_unlock_bh(&bc_lock);
  705. }