bcast.c 23 KB

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