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