bcast.c 24 KB

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