bcast.c 25 KB

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