bcast.c 28 KB

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