bcast.c 28 KB

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