bcast.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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, 2010-2011, 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 "link.h"
  39. #include "port.h"
  40. #include "bcast.h"
  41. #include "name_distr.h"
  42. #define MAX_PKT_DEFAULT_MCAST 1500 /* bcast link max packet size (fixed) */
  43. #define BCLINK_WIN_DEFAULT 20 /* bcast link window size (default) */
  44. #define BCBEARER MAX_BEARERS
  45. /**
  46. * struct tipc_bcbearer_pair - a pair of bearers used by broadcast link
  47. * @primary: pointer to primary bearer
  48. * @secondary: pointer to secondary bearer
  49. *
  50. * Bearers must have same priority and same set of reachable destinations
  51. * to be paired.
  52. */
  53. struct tipc_bcbearer_pair {
  54. struct tipc_bearer *primary;
  55. struct tipc_bearer *secondary;
  56. };
  57. /**
  58. * struct tipc_bcbearer - bearer used by broadcast link
  59. * @bearer: (non-standard) broadcast bearer structure
  60. * @media: (non-standard) broadcast media structure
  61. * @bpairs: array of bearer pairs
  62. * @bpairs_temp: temporary array of bearer pairs used by tipc_bcbearer_sort()
  63. * @remains: temporary node map used by tipc_bcbearer_send()
  64. * @remains_new: temporary node map used tipc_bcbearer_send()
  65. *
  66. * Note: The fields labelled "temporary" are incorporated into the bearer
  67. * to avoid consuming potentially limited stack space through the use of
  68. * large local variables within multicast routines. Concurrent access is
  69. * prevented through use of the spinlock "bc_lock".
  70. */
  71. struct tipc_bcbearer {
  72. struct tipc_bearer bearer;
  73. struct tipc_media media;
  74. struct tipc_bcbearer_pair bpairs[MAX_BEARERS];
  75. struct tipc_bcbearer_pair bpairs_temp[TIPC_MAX_LINK_PRI + 1];
  76. struct tipc_node_map remains;
  77. struct tipc_node_map remains_new;
  78. };
  79. /**
  80. * struct tipc_bclink - link used for broadcast messages
  81. * @link: (non-standard) broadcast link structure
  82. * @node: (non-standard) node structure representing b'cast link's peer node
  83. * @bcast_nodes: map of broadcast-capable nodes
  84. * @retransmit_to: node that most recently requested a retransmit
  85. *
  86. * Handles sequence numbering, fragmentation, bundling, etc.
  87. */
  88. struct tipc_bclink {
  89. struct tipc_link link;
  90. struct tipc_node node;
  91. struct tipc_node_map bcast_nodes;
  92. struct tipc_node *retransmit_to;
  93. };
  94. static struct tipc_bcbearer bcast_bearer;
  95. static struct tipc_bclink bcast_link;
  96. static struct tipc_bcbearer *bcbearer = &bcast_bearer;
  97. static struct tipc_bclink *bclink = &bcast_link;
  98. static struct tipc_link *bcl = &bcast_link.link;
  99. static DEFINE_SPINLOCK(bc_lock);
  100. const char tipc_bclink_name[] = "broadcast-link";
  101. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  102. struct tipc_node_map *nm_b,
  103. struct tipc_node_map *nm_diff);
  104. static u32 bcbuf_acks(struct sk_buff *buf)
  105. {
  106. return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
  107. }
  108. static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
  109. {
  110. TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
  111. }
  112. static void bcbuf_decr_acks(struct sk_buff *buf)
  113. {
  114. bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
  115. }
  116. void tipc_bclink_add_node(u32 addr)
  117. {
  118. spin_lock_bh(&bc_lock);
  119. tipc_nmap_add(&bclink->bcast_nodes, addr);
  120. spin_unlock_bh(&bc_lock);
  121. }
  122. void tipc_bclink_remove_node(u32 addr)
  123. {
  124. spin_lock_bh(&bc_lock);
  125. tipc_nmap_remove(&bclink->bcast_nodes, addr);
  126. spin_unlock_bh(&bc_lock);
  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. static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
  140. {
  141. node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
  142. seqno : node->bclink.last_sent;
  143. }
  144. /**
  145. * tipc_bclink_retransmit_to - get most recent node to request retransmission
  146. *
  147. * Called with bc_lock locked
  148. */
  149. struct tipc_node *tipc_bclink_retransmit_to(void)
  150. {
  151. return bclink->retransmit_to;
  152. }
  153. /**
  154. * bclink_retransmit_pkt - retransmit broadcast packets
  155. * @after: sequence number of last packet to *not* retransmit
  156. * @to: sequence number of last packet to retransmit
  157. *
  158. * Called with bc_lock locked
  159. */
  160. static void bclink_retransmit_pkt(u32 after, u32 to)
  161. {
  162. struct sk_buff *buf;
  163. buf = bcl->first_out;
  164. while (buf && less_eq(buf_seqno(buf), after))
  165. buf = buf->next;
  166. tipc_link_retransmit(bcl, buf, mod(to - after));
  167. }
  168. /**
  169. * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
  170. * @n_ptr: node that sent acknowledgement info
  171. * @acked: broadcast sequence # that has been acknowledged
  172. *
  173. * Node is locked, bc_lock unlocked.
  174. */
  175. void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
  176. {
  177. struct sk_buff *crs;
  178. struct sk_buff *next;
  179. unsigned int released = 0;
  180. spin_lock_bh(&bc_lock);
  181. /* Bail out if tx queue is empty (no clean up is required) */
  182. crs = bcl->first_out;
  183. if (!crs)
  184. goto exit;
  185. /* Determine which messages need to be acknowledged */
  186. if (acked == INVALID_LINK_SEQ) {
  187. /*
  188. * Contact with specified node has been lost, so need to
  189. * acknowledge sent messages only (if other nodes still exist)
  190. * or both sent and unsent messages (otherwise)
  191. */
  192. if (bclink->bcast_nodes.count)
  193. acked = bcl->fsm_msg_cnt;
  194. else
  195. acked = bcl->next_out_no;
  196. } else {
  197. /*
  198. * Bail out if specified sequence number does not correspond
  199. * to a message that has been sent and not yet acknowledged
  200. */
  201. if (less(acked, buf_seqno(crs)) ||
  202. less(bcl->fsm_msg_cnt, acked) ||
  203. less_eq(acked, n_ptr->bclink.acked))
  204. goto exit;
  205. }
  206. /* Skip over packets that node has previously acknowledged */
  207. while (crs && less_eq(buf_seqno(crs), n_ptr->bclink.acked))
  208. crs = crs->next;
  209. /* Update packets that node is now acknowledging */
  210. while (crs && less_eq(buf_seqno(crs), acked)) {
  211. next = crs->next;
  212. if (crs != bcl->next_out)
  213. bcbuf_decr_acks(crs);
  214. else {
  215. bcbuf_set_acks(crs, 0);
  216. bcl->next_out = next;
  217. bclink_set_last_sent();
  218. }
  219. if (bcbuf_acks(crs) == 0) {
  220. bcl->first_out = next;
  221. bcl->out_queue_size--;
  222. kfree_skb(crs);
  223. released = 1;
  224. }
  225. crs = next;
  226. }
  227. n_ptr->bclink.acked = acked;
  228. /* Try resolving broadcast link congestion, if necessary */
  229. if (unlikely(bcl->next_out)) {
  230. tipc_link_push_queue(bcl);
  231. bclink_set_last_sent();
  232. }
  233. if (unlikely(released && !list_empty(&bcl->waiting_ports)))
  234. tipc_link_wakeup_ports(bcl, 0);
  235. exit:
  236. spin_unlock_bh(&bc_lock);
  237. }
  238. /**
  239. * tipc_bclink_update_link_state - update broadcast link state
  240. *
  241. * tipc_net_lock and node lock set
  242. */
  243. void tipc_bclink_update_link_state(struct tipc_node *n_ptr, u32 last_sent)
  244. {
  245. struct sk_buff *buf;
  246. /* Ignore "stale" link state info */
  247. if (less_eq(last_sent, n_ptr->bclink.last_in))
  248. return;
  249. /* Update link synchronization state; quit if in sync */
  250. bclink_update_last_sent(n_ptr, last_sent);
  251. if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
  252. return;
  253. /* Update out-of-sync state; quit if loss is still unconfirmed */
  254. if ((++n_ptr->bclink.oos_state) == 1) {
  255. if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
  256. return;
  257. n_ptr->bclink.oos_state++;
  258. }
  259. /* Don't NACK if one has been recently sent (or seen) */
  260. if (n_ptr->bclink.oos_state & 0x1)
  261. return;
  262. /* Send NACK */
  263. buf = tipc_buf_acquire(INT_H_SIZE);
  264. if (buf) {
  265. struct tipc_msg *msg = buf_msg(buf);
  266. tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG,
  267. INT_H_SIZE, n_ptr->addr);
  268. msg_set_non_seq(msg, 1);
  269. msg_set_mc_netid(msg, tipc_net_id);
  270. msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
  271. msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
  272. msg_set_bcgap_to(msg, n_ptr->bclink.deferred_head
  273. ? buf_seqno(n_ptr->bclink.deferred_head) - 1
  274. : n_ptr->bclink.last_sent);
  275. spin_lock_bh(&bc_lock);
  276. tipc_bearer_send(&bcbearer->bearer, buf, NULL);
  277. bcl->stats.sent_nacks++;
  278. spin_unlock_bh(&bc_lock);
  279. kfree_skb(buf);
  280. n_ptr->bclink.oos_state++;
  281. }
  282. }
  283. /**
  284. * bclink_peek_nack - monitor retransmission requests sent by other nodes
  285. *
  286. * Delay any upcoming NACK by this node if another node has already
  287. * requested the first message this node is going to ask for.
  288. *
  289. * Only tipc_net_lock set.
  290. */
  291. static void bclink_peek_nack(struct tipc_msg *msg)
  292. {
  293. struct tipc_node *n_ptr = tipc_node_find(msg_destnode(msg));
  294. if (unlikely(!n_ptr))
  295. return;
  296. tipc_node_lock(n_ptr);
  297. if (n_ptr->bclink.recv_permitted &&
  298. (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
  299. (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
  300. n_ptr->bclink.oos_state = 2;
  301. tipc_node_unlock(n_ptr);
  302. }
  303. /*
  304. * tipc_bclink_xmit - broadcast a packet to all nodes in cluster
  305. */
  306. int tipc_bclink_xmit(struct sk_buff *buf)
  307. {
  308. int res;
  309. spin_lock_bh(&bc_lock);
  310. if (!bclink->bcast_nodes.count) {
  311. res = msg_data_sz(buf_msg(buf));
  312. kfree_skb(buf);
  313. goto exit;
  314. }
  315. res = __tipc_link_xmit(bcl, buf);
  316. if (likely(res >= 0)) {
  317. bclink_set_last_sent();
  318. bcl->stats.queue_sz_counts++;
  319. bcl->stats.accu_queue_sz += bcl->out_queue_size;
  320. }
  321. exit:
  322. spin_unlock_bh(&bc_lock);
  323. return res;
  324. }
  325. /**
  326. * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
  327. *
  328. * Called with both sending node's lock and bc_lock taken.
  329. */
  330. static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
  331. {
  332. bclink_update_last_sent(node, seqno);
  333. node->bclink.last_in = seqno;
  334. node->bclink.oos_state = 0;
  335. bcl->stats.recv_info++;
  336. /*
  337. * Unicast an ACK periodically, ensuring that
  338. * all nodes in the cluster don't ACK at the same time
  339. */
  340. if (((seqno - tipc_own_addr) % TIPC_MIN_LINK_WIN) == 0) {
  341. tipc_link_proto_xmit(node->active_links[node->addr & 1],
  342. STATE_MSG, 0, 0, 0, 0, 0);
  343. bcl->stats.sent_acks++;
  344. }
  345. }
  346. /**
  347. * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
  348. *
  349. * tipc_net_lock is read_locked, no other locks set
  350. */
  351. void tipc_bclink_rcv(struct sk_buff *buf)
  352. {
  353. struct tipc_msg *msg = buf_msg(buf);
  354. struct tipc_node *node;
  355. u32 next_in;
  356. u32 seqno;
  357. int deferred;
  358. /* Screen out unwanted broadcast messages */
  359. if (msg_mc_netid(msg) != tipc_net_id)
  360. goto exit;
  361. node = tipc_node_find(msg_prevnode(msg));
  362. if (unlikely(!node))
  363. goto exit;
  364. tipc_node_lock(node);
  365. if (unlikely(!node->bclink.recv_permitted))
  366. goto unlock;
  367. /* Handle broadcast protocol message */
  368. if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
  369. if (msg_type(msg) != STATE_MSG)
  370. goto unlock;
  371. if (msg_destnode(msg) == tipc_own_addr) {
  372. tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
  373. tipc_node_unlock(node);
  374. spin_lock_bh(&bc_lock);
  375. bcl->stats.recv_nacks++;
  376. bclink->retransmit_to = node;
  377. bclink_retransmit_pkt(msg_bcgap_after(msg),
  378. msg_bcgap_to(msg));
  379. spin_unlock_bh(&bc_lock);
  380. } else {
  381. tipc_node_unlock(node);
  382. bclink_peek_nack(msg);
  383. }
  384. goto exit;
  385. }
  386. /* Handle in-sequence broadcast message */
  387. seqno = msg_seqno(msg);
  388. next_in = mod(node->bclink.last_in + 1);
  389. if (likely(seqno == next_in)) {
  390. receive:
  391. /* Deliver message to destination */
  392. if (likely(msg_isdata(msg))) {
  393. spin_lock_bh(&bc_lock);
  394. bclink_accept_pkt(node, seqno);
  395. spin_unlock_bh(&bc_lock);
  396. tipc_node_unlock(node);
  397. if (likely(msg_mcast(msg)))
  398. tipc_port_mcast_rcv(buf, NULL);
  399. else
  400. kfree_skb(buf);
  401. } else if (msg_user(msg) == MSG_BUNDLER) {
  402. spin_lock_bh(&bc_lock);
  403. bclink_accept_pkt(node, seqno);
  404. bcl->stats.recv_bundles++;
  405. bcl->stats.recv_bundled += msg_msgcnt(msg);
  406. spin_unlock_bh(&bc_lock);
  407. tipc_node_unlock(node);
  408. tipc_link_bundle_rcv(buf);
  409. } else if (msg_user(msg) == MSG_FRAGMENTER) {
  410. int ret;
  411. ret = tipc_link_frag_rcv(&node->bclink.reasm_head,
  412. &node->bclink.reasm_tail,
  413. &buf);
  414. if (ret == LINK_REASM_ERROR)
  415. goto unlock;
  416. spin_lock_bh(&bc_lock);
  417. bclink_accept_pkt(node, seqno);
  418. bcl->stats.recv_fragments++;
  419. if (ret == LINK_REASM_COMPLETE) {
  420. bcl->stats.recv_fragmented++;
  421. /* Point msg to inner header */
  422. msg = buf_msg(buf);
  423. spin_unlock_bh(&bc_lock);
  424. goto receive;
  425. }
  426. spin_unlock_bh(&bc_lock);
  427. tipc_node_unlock(node);
  428. } else if (msg_user(msg) == NAME_DISTRIBUTOR) {
  429. spin_lock_bh(&bc_lock);
  430. bclink_accept_pkt(node, seqno);
  431. spin_unlock_bh(&bc_lock);
  432. tipc_node_unlock(node);
  433. tipc_named_rcv(buf);
  434. } else {
  435. spin_lock_bh(&bc_lock);
  436. bclink_accept_pkt(node, seqno);
  437. spin_unlock_bh(&bc_lock);
  438. tipc_node_unlock(node);
  439. kfree_skb(buf);
  440. }
  441. buf = NULL;
  442. /* Determine new synchronization state */
  443. tipc_node_lock(node);
  444. if (unlikely(!tipc_node_is_up(node)))
  445. goto unlock;
  446. if (node->bclink.last_in == node->bclink.last_sent)
  447. goto unlock;
  448. if (!node->bclink.deferred_head) {
  449. node->bclink.oos_state = 1;
  450. goto unlock;
  451. }
  452. msg = buf_msg(node->bclink.deferred_head);
  453. seqno = msg_seqno(msg);
  454. next_in = mod(next_in + 1);
  455. if (seqno != next_in)
  456. goto unlock;
  457. /* Take in-sequence message from deferred queue & deliver it */
  458. buf = node->bclink.deferred_head;
  459. node->bclink.deferred_head = buf->next;
  460. node->bclink.deferred_size--;
  461. goto receive;
  462. }
  463. /* Handle out-of-sequence broadcast message */
  464. if (less(next_in, seqno)) {
  465. deferred = tipc_link_defer_pkt(&node->bclink.deferred_head,
  466. &node->bclink.deferred_tail,
  467. buf);
  468. node->bclink.deferred_size += deferred;
  469. bclink_update_last_sent(node, seqno);
  470. buf = NULL;
  471. } else
  472. deferred = 0;
  473. spin_lock_bh(&bc_lock);
  474. if (deferred)
  475. bcl->stats.deferred_recv++;
  476. else
  477. bcl->stats.duplicates++;
  478. spin_unlock_bh(&bc_lock);
  479. unlock:
  480. tipc_node_unlock(node);
  481. exit:
  482. kfree_skb(buf);
  483. }
  484. u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
  485. {
  486. return (n_ptr->bclink.recv_permitted &&
  487. (tipc_bclink_get_last_sent() != n_ptr->bclink.acked));
  488. }
  489. /**
  490. * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
  491. *
  492. * Send packet over as many bearers as necessary to reach all nodes
  493. * that have joined the broadcast link.
  494. *
  495. * Returns 0 (packet sent successfully) under all circumstances,
  496. * since the broadcast link's pseudo-bearer never blocks
  497. */
  498. static int tipc_bcbearer_send(struct sk_buff *buf, struct tipc_bearer *unused1,
  499. struct tipc_media_addr *unused2)
  500. {
  501. int bp_index;
  502. /* Prepare broadcast link message for reliable transmission,
  503. * if first time trying to send it;
  504. * preparation is skipped for broadcast link protocol messages
  505. * since they are sent in an unreliable manner and don't need it
  506. */
  507. if (likely(!msg_non_seq(buf_msg(buf)))) {
  508. struct tipc_msg *msg;
  509. bcbuf_set_acks(buf, bclink->bcast_nodes.count);
  510. msg = buf_msg(buf);
  511. msg_set_non_seq(msg, 1);
  512. msg_set_mc_netid(msg, tipc_net_id);
  513. bcl->stats.sent_info++;
  514. if (WARN_ON(!bclink->bcast_nodes.count)) {
  515. dump_stack();
  516. return 0;
  517. }
  518. }
  519. /* Send buffer over bearers until all targets reached */
  520. bcbearer->remains = bclink->bcast_nodes;
  521. for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
  522. struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
  523. struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
  524. struct tipc_bearer *b = p;
  525. struct sk_buff *tbuf;
  526. if (!p)
  527. break; /* No more bearers to try */
  528. tipc_nmap_diff(&bcbearer->remains, &b->nodes,
  529. &bcbearer->remains_new);
  530. if (bcbearer->remains_new.count == bcbearer->remains.count)
  531. continue; /* Nothing added by bearer pair */
  532. if (bp_index == 0) {
  533. /* Use original buffer for first bearer */
  534. tipc_bearer_send(b, buf, &b->bcast_addr);
  535. } else {
  536. /* Avoid concurrent buffer access */
  537. tbuf = pskb_copy(buf, GFP_ATOMIC);
  538. if (!tbuf)
  539. break;
  540. tipc_bearer_send(b, tbuf, &b->bcast_addr);
  541. kfree_skb(tbuf); /* Bearer keeps a clone */
  542. }
  543. /* Swap bearers for next packet */
  544. if (s) {
  545. bcbearer->bpairs[bp_index].primary = s;
  546. bcbearer->bpairs[bp_index].secondary = p;
  547. }
  548. if (bcbearer->remains_new.count == 0)
  549. break; /* All targets reached */
  550. bcbearer->remains = bcbearer->remains_new;
  551. }
  552. return 0;
  553. }
  554. /**
  555. * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
  556. */
  557. void tipc_bcbearer_sort(void)
  558. {
  559. struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
  560. struct tipc_bcbearer_pair *bp_curr;
  561. int b_index;
  562. int pri;
  563. spin_lock_bh(&bc_lock);
  564. /* Group bearers by priority (can assume max of two per priority) */
  565. memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
  566. for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
  567. struct tipc_bearer *b = bearer_list[b_index];
  568. if (!b || !b->nodes.count)
  569. continue;
  570. if (!bp_temp[b->priority].primary)
  571. bp_temp[b->priority].primary = b;
  572. else
  573. bp_temp[b->priority].secondary = b;
  574. }
  575. /* Create array of bearer pairs for broadcasting */
  576. bp_curr = bcbearer->bpairs;
  577. memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
  578. for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
  579. if (!bp_temp[pri].primary)
  580. continue;
  581. bp_curr->primary = bp_temp[pri].primary;
  582. if (bp_temp[pri].secondary) {
  583. if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
  584. &bp_temp[pri].secondary->nodes)) {
  585. bp_curr->secondary = bp_temp[pri].secondary;
  586. } else {
  587. bp_curr++;
  588. bp_curr->primary = bp_temp[pri].secondary;
  589. }
  590. }
  591. bp_curr++;
  592. }
  593. spin_unlock_bh(&bc_lock);
  594. }
  595. int tipc_bclink_stats(char *buf, const u32 buf_size)
  596. {
  597. int ret;
  598. struct tipc_stats *s;
  599. if (!bcl)
  600. return 0;
  601. spin_lock_bh(&bc_lock);
  602. s = &bcl->stats;
  603. ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
  604. " Window:%u packets\n",
  605. bcl->name, bcl->queue_limit[0]);
  606. ret += tipc_snprintf(buf + ret, buf_size - ret,
  607. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  608. s->recv_info, s->recv_fragments,
  609. s->recv_fragmented, s->recv_bundles,
  610. s->recv_bundled);
  611. ret += tipc_snprintf(buf + ret, buf_size - ret,
  612. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  613. s->sent_info, s->sent_fragments,
  614. s->sent_fragmented, s->sent_bundles,
  615. s->sent_bundled);
  616. ret += tipc_snprintf(buf + ret, buf_size - ret,
  617. " RX naks:%u defs:%u dups:%u\n",
  618. s->recv_nacks, s->deferred_recv, s->duplicates);
  619. ret += tipc_snprintf(buf + ret, buf_size - ret,
  620. " TX naks:%u acks:%u dups:%u\n",
  621. s->sent_nacks, s->sent_acks, s->retransmitted);
  622. ret += tipc_snprintf(buf + ret, buf_size - ret,
  623. " Congestion link:%u Send queue max:%u avg:%u\n",
  624. s->link_congs, s->max_queue_sz,
  625. s->queue_sz_counts ?
  626. (s->accu_queue_sz / s->queue_sz_counts) : 0);
  627. spin_unlock_bh(&bc_lock);
  628. return ret;
  629. }
  630. int tipc_bclink_reset_stats(void)
  631. {
  632. if (!bcl)
  633. return -ENOPROTOOPT;
  634. spin_lock_bh(&bc_lock);
  635. memset(&bcl->stats, 0, sizeof(bcl->stats));
  636. spin_unlock_bh(&bc_lock);
  637. return 0;
  638. }
  639. int tipc_bclink_set_queue_limits(u32 limit)
  640. {
  641. if (!bcl)
  642. return -ENOPROTOOPT;
  643. if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
  644. return -EINVAL;
  645. spin_lock_bh(&bc_lock);
  646. tipc_link_set_queue_limits(bcl, limit);
  647. spin_unlock_bh(&bc_lock);
  648. return 0;
  649. }
  650. void tipc_bclink_init(void)
  651. {
  652. bcbearer->bearer.media = &bcbearer->media;
  653. bcbearer->media.send_msg = tipc_bcbearer_send;
  654. sprintf(bcbearer->media.name, "tipc-broadcast");
  655. INIT_LIST_HEAD(&bcl->waiting_ports);
  656. bcl->next_out_no = 1;
  657. spin_lock_init(&bclink->node.lock);
  658. bcl->owner = &bclink->node;
  659. bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
  660. tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
  661. bcl->b_ptr = &bcbearer->bearer;
  662. bearer_list[BCBEARER] = &bcbearer->bearer;
  663. bcl->state = WORKING_WORKING;
  664. strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  665. }
  666. void tipc_bclink_stop(void)
  667. {
  668. spin_lock_bh(&bc_lock);
  669. tipc_link_purge_queues(bcl);
  670. spin_unlock_bh(&bc_lock);
  671. bearer_list[BCBEARER] = NULL;
  672. memset(bclink, 0, sizeof(*bclink));
  673. memset(bcbearer, 0, sizeof(*bcbearer));
  674. }
  675. /**
  676. * tipc_nmap_add - add a node to a node map
  677. */
  678. void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
  679. {
  680. int n = tipc_node(node);
  681. int w = n / WSIZE;
  682. u32 mask = (1 << (n % WSIZE));
  683. if ((nm_ptr->map[w] & mask) == 0) {
  684. nm_ptr->count++;
  685. nm_ptr->map[w] |= mask;
  686. }
  687. }
  688. /**
  689. * tipc_nmap_remove - remove a node from a node map
  690. */
  691. void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
  692. {
  693. int n = tipc_node(node);
  694. int w = n / WSIZE;
  695. u32 mask = (1 << (n % WSIZE));
  696. if ((nm_ptr->map[w] & mask) != 0) {
  697. nm_ptr->map[w] &= ~mask;
  698. nm_ptr->count--;
  699. }
  700. }
  701. /**
  702. * tipc_nmap_diff - find differences between node maps
  703. * @nm_a: input node map A
  704. * @nm_b: input node map B
  705. * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
  706. */
  707. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  708. struct tipc_node_map *nm_b,
  709. struct tipc_node_map *nm_diff)
  710. {
  711. int stop = ARRAY_SIZE(nm_a->map);
  712. int w;
  713. int b;
  714. u32 map;
  715. memset(nm_diff, 0, sizeof(*nm_diff));
  716. for (w = 0; w < stop; w++) {
  717. map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
  718. nm_diff->map[w] = map;
  719. if (map != 0) {
  720. for (b = 0 ; b < WSIZE; b++) {
  721. if (map & (1 << b))
  722. nm_diff->count++;
  723. }
  724. }
  725. }
  726. }
  727. /**
  728. * tipc_port_list_add - add a port to a port list, ensuring no duplicates
  729. */
  730. void tipc_port_list_add(struct tipc_port_list *pl_ptr, u32 port)
  731. {
  732. struct tipc_port_list *item = pl_ptr;
  733. int i;
  734. int item_sz = PLSIZE;
  735. int cnt = pl_ptr->count;
  736. for (; ; cnt -= item_sz, item = item->next) {
  737. if (cnt < PLSIZE)
  738. item_sz = cnt;
  739. for (i = 0; i < item_sz; i++)
  740. if (item->ports[i] == port)
  741. return;
  742. if (i < PLSIZE) {
  743. item->ports[i] = port;
  744. pl_ptr->count++;
  745. return;
  746. }
  747. if (!item->next) {
  748. item->next = kmalloc(sizeof(*item), GFP_ATOMIC);
  749. if (!item->next) {
  750. pr_warn("Incomplete multicast delivery, no memory\n");
  751. return;
  752. }
  753. item->next->next = NULL;
  754. }
  755. }
  756. }
  757. /**
  758. * tipc_port_list_free - free dynamically created entries in port_list chain
  759. *
  760. */
  761. void tipc_port_list_free(struct tipc_port_list *pl_ptr)
  762. {
  763. struct tipc_port_list *item;
  764. struct tipc_port_list *next;
  765. for (item = pl_ptr->next; item; item = next) {
  766. next = item->next;
  767. kfree(item);
  768. }
  769. }