bcast.c 28 KB

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