bcast.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * net/tipc/bcast.c: TIPC broadcast code
  3. *
  4. * Copyright (c) 2004-2006, 2014-2015, 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 "socket.h"
  38. #include "msg.h"
  39. #include "bcast.h"
  40. #include "name_distr.h"
  41. #include "core.h"
  42. #define MAX_PKT_DEFAULT_MCAST 1500 /* bcast link max packet size (fixed) */
  43. #define BCLINK_WIN_DEFAULT 20 /* bcast link window size (default) */
  44. const char tipc_bclink_name[] = "broadcast-link";
  45. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  46. struct tipc_node_map *nm_b,
  47. struct tipc_node_map *nm_diff);
  48. static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node);
  49. static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node);
  50. static void tipc_bclink_lock(struct net *net)
  51. {
  52. struct tipc_net *tn = net_generic(net, tipc_net_id);
  53. spin_lock_bh(&tn->bclink->lock);
  54. }
  55. static void tipc_bclink_unlock(struct net *net)
  56. {
  57. struct tipc_net *tn = net_generic(net, tipc_net_id);
  58. struct tipc_node *node = NULL;
  59. if (likely(!tn->bclink->flags)) {
  60. spin_unlock_bh(&tn->bclink->lock);
  61. return;
  62. }
  63. if (tn->bclink->flags & TIPC_BCLINK_RESET) {
  64. tn->bclink->flags &= ~TIPC_BCLINK_RESET;
  65. node = tipc_bclink_retransmit_to(net);
  66. }
  67. spin_unlock_bh(&tn->bclink->lock);
  68. if (node)
  69. tipc_link_reset_all(node);
  70. }
  71. void tipc_bclink_input(struct net *net)
  72. {
  73. struct tipc_net *tn = net_generic(net, tipc_net_id);
  74. tipc_sk_mcast_rcv(net, &tn->bclink->arrvq, &tn->bclink->inputq);
  75. }
  76. uint tipc_bclink_get_mtu(void)
  77. {
  78. return MAX_PKT_DEFAULT_MCAST;
  79. }
  80. void tipc_bclink_set_flags(struct net *net, unsigned int flags)
  81. {
  82. struct tipc_net *tn = net_generic(net, tipc_net_id);
  83. tn->bclink->flags |= flags;
  84. }
  85. static u32 bcbuf_acks(struct sk_buff *buf)
  86. {
  87. return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
  88. }
  89. static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
  90. {
  91. TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
  92. }
  93. static void bcbuf_decr_acks(struct sk_buff *buf)
  94. {
  95. bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
  96. }
  97. void tipc_bclink_add_node(struct net *net, u32 addr)
  98. {
  99. struct tipc_net *tn = net_generic(net, tipc_net_id);
  100. tipc_bclink_lock(net);
  101. tipc_nmap_add(&tn->bclink->bcast_nodes, addr);
  102. tipc_bclink_unlock(net);
  103. }
  104. void tipc_bclink_remove_node(struct net *net, u32 addr)
  105. {
  106. struct tipc_net *tn = net_generic(net, tipc_net_id);
  107. tipc_bclink_lock(net);
  108. tipc_nmap_remove(&tn->bclink->bcast_nodes, addr);
  109. tipc_bclink_unlock(net);
  110. }
  111. static void bclink_set_last_sent(struct net *net)
  112. {
  113. struct tipc_net *tn = net_generic(net, tipc_net_id);
  114. struct tipc_link *bcl = tn->bcl;
  115. struct sk_buff *skb = skb_peek(&bcl->backlogq);
  116. if (skb)
  117. bcl->fsm_msg_cnt = mod(buf_seqno(skb) - 1);
  118. else
  119. bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
  120. }
  121. u32 tipc_bclink_get_last_sent(struct net *net)
  122. {
  123. struct tipc_net *tn = net_generic(net, tipc_net_id);
  124. return tn->bcl->fsm_msg_cnt;
  125. }
  126. static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
  127. {
  128. node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
  129. seqno : node->bclink.last_sent;
  130. }
  131. /**
  132. * tipc_bclink_retransmit_to - get most recent node to request retransmission
  133. *
  134. * Called with bclink_lock locked
  135. */
  136. struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
  137. {
  138. struct tipc_net *tn = net_generic(net, tipc_net_id);
  139. return tn->bclink->retransmit_to;
  140. }
  141. /**
  142. * bclink_retransmit_pkt - retransmit broadcast packets
  143. * @after: sequence number of last packet to *not* retransmit
  144. * @to: sequence number of last packet to retransmit
  145. *
  146. * Called with bclink_lock locked
  147. */
  148. static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
  149. {
  150. struct sk_buff *skb;
  151. struct tipc_link *bcl = tn->bcl;
  152. skb_queue_walk(&bcl->transmq, skb) {
  153. if (more(buf_seqno(skb), after)) {
  154. tipc_link_retransmit(bcl, skb, mod(to - after));
  155. break;
  156. }
  157. }
  158. }
  159. /**
  160. * tipc_bclink_wakeup_users - wake up pending users
  161. *
  162. * Called with no locks taken
  163. */
  164. void tipc_bclink_wakeup_users(struct net *net)
  165. {
  166. struct tipc_net *tn = net_generic(net, tipc_net_id);
  167. tipc_sk_rcv(net, &tn->bclink->link.wakeupq);
  168. }
  169. /**
  170. * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
  171. * @n_ptr: node that sent acknowledgement info
  172. * @acked: broadcast sequence # that has been acknowledged
  173. *
  174. * Node is locked, bclink_lock unlocked.
  175. */
  176. void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
  177. {
  178. struct sk_buff *skb, *tmp;
  179. unsigned int released = 0;
  180. struct net *net = n_ptr->net;
  181. struct tipc_net *tn = net_generic(net, tipc_net_id);
  182. if (unlikely(!n_ptr->bclink.recv_permitted))
  183. return;
  184. tipc_bclink_lock(net);
  185. /* Bail out if tx queue is empty (no clean up is required) */
  186. skb = skb_peek(&tn->bcl->transmq);
  187. if (!skb)
  188. goto exit;
  189. /* Determine which messages need to be acknowledged */
  190. if (acked == INVALID_LINK_SEQ) {
  191. /*
  192. * Contact with specified node has been lost, so need to
  193. * acknowledge sent messages only (if other nodes still exist)
  194. * or both sent and unsent messages (otherwise)
  195. */
  196. if (tn->bclink->bcast_nodes.count)
  197. acked = tn->bcl->fsm_msg_cnt;
  198. else
  199. acked = tn->bcl->next_out_no;
  200. } else {
  201. /*
  202. * Bail out if specified sequence number does not correspond
  203. * to a message that has been sent and not yet acknowledged
  204. */
  205. if (less(acked, buf_seqno(skb)) ||
  206. less(tn->bcl->fsm_msg_cnt, acked) ||
  207. less_eq(acked, n_ptr->bclink.acked))
  208. goto exit;
  209. }
  210. /* Skip over packets that node has previously acknowledged */
  211. skb_queue_walk(&tn->bcl->transmq, skb) {
  212. if (more(buf_seqno(skb), n_ptr->bclink.acked))
  213. break;
  214. }
  215. /* Update packets that node is now acknowledging */
  216. skb_queue_walk_from_safe(&tn->bcl->transmq, skb, tmp) {
  217. if (more(buf_seqno(skb), acked))
  218. break;
  219. bcbuf_decr_acks(skb);
  220. bclink_set_last_sent(net);
  221. if (bcbuf_acks(skb) == 0) {
  222. __skb_unlink(skb, &tn->bcl->transmq);
  223. kfree_skb(skb);
  224. released = 1;
  225. }
  226. }
  227. n_ptr->bclink.acked = acked;
  228. /* Try resolving broadcast link congestion, if necessary */
  229. if (unlikely(skb_peek(&tn->bcl->backlogq))) {
  230. tipc_link_push_packets(tn->bcl);
  231. bclink_set_last_sent(net);
  232. }
  233. if (unlikely(released && !skb_queue_empty(&tn->bcl->wakeupq)))
  234. n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
  235. exit:
  236. tipc_bclink_unlock(net);
  237. }
  238. /**
  239. * tipc_bclink_update_link_state - update broadcast link state
  240. *
  241. * RCU and node lock set
  242. */
  243. void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
  244. u32 last_sent)
  245. {
  246. struct sk_buff *buf;
  247. struct net *net = n_ptr->net;
  248. struct tipc_net *tn = net_generic(net, tipc_net_id);
  249. /* Ignore "stale" link state info */
  250. if (less_eq(last_sent, n_ptr->bclink.last_in))
  251. return;
  252. /* Update link synchronization state; quit if in sync */
  253. bclink_update_last_sent(n_ptr, last_sent);
  254. if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
  255. return;
  256. /* Update out-of-sync state; quit if loss is still unconfirmed */
  257. if ((++n_ptr->bclink.oos_state) == 1) {
  258. if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
  259. return;
  260. n_ptr->bclink.oos_state++;
  261. }
  262. /* Don't NACK if one has been recently sent (or seen) */
  263. if (n_ptr->bclink.oos_state & 0x1)
  264. return;
  265. /* Send NACK */
  266. buf = tipc_buf_acquire(INT_H_SIZE);
  267. if (buf) {
  268. struct tipc_msg *msg = buf_msg(buf);
  269. struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferdq);
  270. u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
  271. tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
  272. INT_H_SIZE, n_ptr->addr);
  273. msg_set_non_seq(msg, 1);
  274. msg_set_mc_netid(msg, tn->net_id);
  275. msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
  276. msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
  277. msg_set_bcgap_to(msg, to);
  278. tipc_bclink_lock(net);
  279. tipc_bearer_send(net, MAX_BEARERS, buf, NULL);
  280. tn->bcl->stats.sent_nacks++;
  281. tipc_bclink_unlock(net);
  282. kfree_skb(buf);
  283. n_ptr->bclink.oos_state++;
  284. }
  285. }
  286. /**
  287. * bclink_peek_nack - monitor retransmission requests sent by other nodes
  288. *
  289. * Delay any upcoming NACK by this node if another node has already
  290. * requested the first message this node is going to ask for.
  291. */
  292. static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
  293. {
  294. struct tipc_node *n_ptr = tipc_node_find(net, msg_destnode(msg));
  295. if (unlikely(!n_ptr))
  296. return;
  297. tipc_node_lock(n_ptr);
  298. if (n_ptr->bclink.recv_permitted &&
  299. (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
  300. (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
  301. n_ptr->bclink.oos_state = 2;
  302. tipc_node_unlock(n_ptr);
  303. }
  304. /* tipc_bclink_xmit - deliver buffer chain to all nodes in cluster
  305. * and to identified node local sockets
  306. * @net: the applicable net namespace
  307. * @list: chain of buffers containing message
  308. * Consumes the buffer chain, except when returning -ELINKCONG
  309. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  310. */
  311. int tipc_bclink_xmit(struct net *net, struct sk_buff_head *list)
  312. {
  313. struct tipc_net *tn = net_generic(net, tipc_net_id);
  314. struct tipc_link *bcl = tn->bcl;
  315. struct tipc_bclink *bclink = tn->bclink;
  316. int rc = 0;
  317. int bc = 0;
  318. struct sk_buff *skb;
  319. struct sk_buff_head arrvq;
  320. struct sk_buff_head inputq;
  321. /* Prepare clone of message for local node */
  322. skb = tipc_msg_reassemble(list);
  323. if (unlikely(!skb)) {
  324. __skb_queue_purge(list);
  325. return -EHOSTUNREACH;
  326. }
  327. /* Broadcast to all nodes */
  328. if (likely(bclink)) {
  329. tipc_bclink_lock(net);
  330. if (likely(bclink->bcast_nodes.count)) {
  331. rc = __tipc_link_xmit(net, bcl, list);
  332. if (likely(!rc)) {
  333. u32 len = skb_queue_len(&bcl->transmq);
  334. bclink_set_last_sent(net);
  335. bcl->stats.queue_sz_counts++;
  336. bcl->stats.accu_queue_sz += len;
  337. }
  338. bc = 1;
  339. }
  340. tipc_bclink_unlock(net);
  341. }
  342. if (unlikely(!bc))
  343. __skb_queue_purge(list);
  344. if (unlikely(rc)) {
  345. kfree_skb(skb);
  346. return rc;
  347. }
  348. /* Deliver message clone */
  349. __skb_queue_head_init(&arrvq);
  350. skb_queue_head_init(&inputq);
  351. __skb_queue_tail(&arrvq, skb);
  352. tipc_sk_mcast_rcv(net, &arrvq, &inputq);
  353. return rc;
  354. }
  355. /**
  356. * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
  357. *
  358. * Called with both sending node's lock and bclink_lock taken.
  359. */
  360. static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
  361. {
  362. struct tipc_net *tn = net_generic(node->net, tipc_net_id);
  363. bclink_update_last_sent(node, seqno);
  364. node->bclink.last_in = seqno;
  365. node->bclink.oos_state = 0;
  366. tn->bcl->stats.recv_info++;
  367. /*
  368. * Unicast an ACK periodically, ensuring that
  369. * all nodes in the cluster don't ACK at the same time
  370. */
  371. if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
  372. tipc_link_proto_xmit(node->active_links[node->addr & 1],
  373. STATE_MSG, 0, 0, 0, 0, 0);
  374. tn->bcl->stats.sent_acks++;
  375. }
  376. }
  377. /**
  378. * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
  379. *
  380. * RCU is locked, no other locks set
  381. */
  382. void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
  383. {
  384. struct tipc_net *tn = net_generic(net, tipc_net_id);
  385. struct tipc_link *bcl = tn->bcl;
  386. struct tipc_msg *msg = buf_msg(buf);
  387. struct tipc_node *node;
  388. u32 next_in;
  389. u32 seqno;
  390. int deferred = 0;
  391. int pos = 0;
  392. struct sk_buff *iskb;
  393. struct sk_buff_head *arrvq, *inputq;
  394. /* Screen out unwanted broadcast messages */
  395. if (msg_mc_netid(msg) != tn->net_id)
  396. goto exit;
  397. node = tipc_node_find(net, msg_prevnode(msg));
  398. if (unlikely(!node))
  399. goto exit;
  400. tipc_node_lock(node);
  401. if (unlikely(!node->bclink.recv_permitted))
  402. goto unlock;
  403. /* Handle broadcast protocol message */
  404. if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
  405. if (msg_type(msg) != STATE_MSG)
  406. goto unlock;
  407. if (msg_destnode(msg) == tn->own_addr) {
  408. tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
  409. tipc_node_unlock(node);
  410. tipc_bclink_lock(net);
  411. bcl->stats.recv_nacks++;
  412. tn->bclink->retransmit_to = node;
  413. bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
  414. msg_bcgap_to(msg));
  415. tipc_bclink_unlock(net);
  416. } else {
  417. tipc_node_unlock(node);
  418. bclink_peek_nack(net, msg);
  419. }
  420. goto exit;
  421. }
  422. /* Handle in-sequence broadcast message */
  423. seqno = msg_seqno(msg);
  424. next_in = mod(node->bclink.last_in + 1);
  425. arrvq = &tn->bclink->arrvq;
  426. inputq = &tn->bclink->inputq;
  427. if (likely(seqno == next_in)) {
  428. receive:
  429. /* Deliver message to destination */
  430. if (likely(msg_isdata(msg))) {
  431. tipc_bclink_lock(net);
  432. bclink_accept_pkt(node, seqno);
  433. spin_lock_bh(&inputq->lock);
  434. __skb_queue_tail(arrvq, buf);
  435. spin_unlock_bh(&inputq->lock);
  436. node->action_flags |= TIPC_BCAST_MSG_EVT;
  437. tipc_bclink_unlock(net);
  438. tipc_node_unlock(node);
  439. } else if (msg_user(msg) == MSG_BUNDLER) {
  440. tipc_bclink_lock(net);
  441. bclink_accept_pkt(node, seqno);
  442. bcl->stats.recv_bundles++;
  443. bcl->stats.recv_bundled += msg_msgcnt(msg);
  444. pos = 0;
  445. while (tipc_msg_extract(buf, &iskb, &pos)) {
  446. spin_lock_bh(&inputq->lock);
  447. __skb_queue_tail(arrvq, iskb);
  448. spin_unlock_bh(&inputq->lock);
  449. }
  450. node->action_flags |= TIPC_BCAST_MSG_EVT;
  451. tipc_bclink_unlock(net);
  452. tipc_node_unlock(node);
  453. } else if (msg_user(msg) == MSG_FRAGMENTER) {
  454. tipc_buf_append(&node->bclink.reasm_buf, &buf);
  455. if (unlikely(!buf && !node->bclink.reasm_buf))
  456. goto unlock;
  457. tipc_bclink_lock(net);
  458. bclink_accept_pkt(node, seqno);
  459. bcl->stats.recv_fragments++;
  460. if (buf) {
  461. bcl->stats.recv_fragmented++;
  462. msg = buf_msg(buf);
  463. tipc_bclink_unlock(net);
  464. goto receive;
  465. }
  466. tipc_bclink_unlock(net);
  467. tipc_node_unlock(node);
  468. } else {
  469. tipc_bclink_lock(net);
  470. bclink_accept_pkt(node, seqno);
  471. tipc_bclink_unlock(net);
  472. tipc_node_unlock(node);
  473. kfree_skb(buf);
  474. }
  475. buf = NULL;
  476. /* Determine new synchronization state */
  477. tipc_node_lock(node);
  478. if (unlikely(!tipc_node_is_up(node)))
  479. goto unlock;
  480. if (node->bclink.last_in == node->bclink.last_sent)
  481. goto unlock;
  482. if (skb_queue_empty(&node->bclink.deferdq)) {
  483. node->bclink.oos_state = 1;
  484. goto unlock;
  485. }
  486. msg = buf_msg(skb_peek(&node->bclink.deferdq));
  487. seqno = msg_seqno(msg);
  488. next_in = mod(next_in + 1);
  489. if (seqno != next_in)
  490. goto unlock;
  491. /* Take in-sequence message from deferred queue & deliver it */
  492. buf = __skb_dequeue(&node->bclink.deferdq);
  493. goto receive;
  494. }
  495. /* Handle out-of-sequence broadcast message */
  496. if (less(next_in, seqno)) {
  497. deferred = tipc_link_defer_pkt(&node->bclink.deferdq,
  498. buf);
  499. bclink_update_last_sent(node, seqno);
  500. buf = NULL;
  501. }
  502. tipc_bclink_lock(net);
  503. if (deferred)
  504. bcl->stats.deferred_recv++;
  505. else
  506. bcl->stats.duplicates++;
  507. tipc_bclink_unlock(net);
  508. unlock:
  509. tipc_node_unlock(node);
  510. exit:
  511. kfree_skb(buf);
  512. }
  513. u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
  514. {
  515. return (n_ptr->bclink.recv_permitted &&
  516. (tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
  517. }
  518. /**
  519. * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
  520. *
  521. * Send packet over as many bearers as necessary to reach all nodes
  522. * that have joined the broadcast link.
  523. *
  524. * Returns 0 (packet sent successfully) under all circumstances,
  525. * since the broadcast link's pseudo-bearer never blocks
  526. */
  527. static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
  528. struct tipc_bearer *unused1,
  529. struct tipc_media_addr *unused2)
  530. {
  531. int bp_index;
  532. struct tipc_msg *msg = buf_msg(buf);
  533. struct tipc_net *tn = net_generic(net, tipc_net_id);
  534. struct tipc_bcbearer *bcbearer = tn->bcbearer;
  535. struct tipc_bclink *bclink = tn->bclink;
  536. /* Prepare broadcast link message for reliable transmission,
  537. * if first time trying to send it;
  538. * preparation is skipped for broadcast link protocol messages
  539. * since they are sent in an unreliable manner and don't need it
  540. */
  541. if (likely(!msg_non_seq(buf_msg(buf)))) {
  542. bcbuf_set_acks(buf, bclink->bcast_nodes.count);
  543. msg_set_non_seq(msg, 1);
  544. msg_set_mc_netid(msg, tn->net_id);
  545. tn->bcl->stats.sent_info++;
  546. if (WARN_ON(!bclink->bcast_nodes.count)) {
  547. dump_stack();
  548. return 0;
  549. }
  550. }
  551. /* Send buffer over bearers until all targets reached */
  552. bcbearer->remains = bclink->bcast_nodes;
  553. for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
  554. struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
  555. struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
  556. struct tipc_bearer *bp[2] = {p, s};
  557. struct tipc_bearer *b = bp[msg_link_selector(msg)];
  558. struct sk_buff *tbuf;
  559. if (!p)
  560. break; /* No more bearers to try */
  561. if (!b)
  562. b = p;
  563. tipc_nmap_diff(&bcbearer->remains, &b->nodes,
  564. &bcbearer->remains_new);
  565. if (bcbearer->remains_new.count == bcbearer->remains.count)
  566. continue; /* Nothing added by bearer pair */
  567. if (bp_index == 0) {
  568. /* Use original buffer for first bearer */
  569. tipc_bearer_send(net, b->identity, buf, &b->bcast_addr);
  570. } else {
  571. /* Avoid concurrent buffer access */
  572. tbuf = pskb_copy_for_clone(buf, GFP_ATOMIC);
  573. if (!tbuf)
  574. break;
  575. tipc_bearer_send(net, b->identity, tbuf,
  576. &b->bcast_addr);
  577. kfree_skb(tbuf); /* Bearer keeps a clone */
  578. }
  579. if (bcbearer->remains_new.count == 0)
  580. break; /* All targets reached */
  581. bcbearer->remains = bcbearer->remains_new;
  582. }
  583. return 0;
  584. }
  585. /**
  586. * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
  587. */
  588. void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
  589. u32 node, bool action)
  590. {
  591. struct tipc_net *tn = net_generic(net, tipc_net_id);
  592. struct tipc_bcbearer *bcbearer = tn->bcbearer;
  593. struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
  594. struct tipc_bcbearer_pair *bp_curr;
  595. struct tipc_bearer *b;
  596. int b_index;
  597. int pri;
  598. tipc_bclink_lock(net);
  599. if (action)
  600. tipc_nmap_add(nm_ptr, node);
  601. else
  602. tipc_nmap_remove(nm_ptr, node);
  603. /* Group bearers by priority (can assume max of two per priority) */
  604. memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
  605. rcu_read_lock();
  606. for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
  607. b = rcu_dereference_rtnl(tn->bearer_list[b_index]);
  608. if (!b || !b->nodes.count)
  609. continue;
  610. if (!bp_temp[b->priority].primary)
  611. bp_temp[b->priority].primary = b;
  612. else
  613. bp_temp[b->priority].secondary = b;
  614. }
  615. rcu_read_unlock();
  616. /* Create array of bearer pairs for broadcasting */
  617. bp_curr = bcbearer->bpairs;
  618. memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
  619. for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
  620. if (!bp_temp[pri].primary)
  621. continue;
  622. bp_curr->primary = bp_temp[pri].primary;
  623. if (bp_temp[pri].secondary) {
  624. if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
  625. &bp_temp[pri].secondary->nodes)) {
  626. bp_curr->secondary = bp_temp[pri].secondary;
  627. } else {
  628. bp_curr++;
  629. bp_curr->primary = bp_temp[pri].secondary;
  630. }
  631. }
  632. bp_curr++;
  633. }
  634. tipc_bclink_unlock(net);
  635. }
  636. static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
  637. struct tipc_stats *stats)
  638. {
  639. int i;
  640. struct nlattr *nest;
  641. struct nla_map {
  642. __u32 key;
  643. __u32 val;
  644. };
  645. struct nla_map map[] = {
  646. {TIPC_NLA_STATS_RX_INFO, stats->recv_info},
  647. {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
  648. {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
  649. {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
  650. {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
  651. {TIPC_NLA_STATS_TX_INFO, stats->sent_info},
  652. {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
  653. {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
  654. {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
  655. {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
  656. {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
  657. {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
  658. {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
  659. {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
  660. {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
  661. {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
  662. {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
  663. {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
  664. {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
  665. (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
  666. };
  667. nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  668. if (!nest)
  669. return -EMSGSIZE;
  670. for (i = 0; i < ARRAY_SIZE(map); i++)
  671. if (nla_put_u32(skb, map[i].key, map[i].val))
  672. goto msg_full;
  673. nla_nest_end(skb, nest);
  674. return 0;
  675. msg_full:
  676. nla_nest_cancel(skb, nest);
  677. return -EMSGSIZE;
  678. }
  679. int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
  680. {
  681. int err;
  682. void *hdr;
  683. struct nlattr *attrs;
  684. struct nlattr *prop;
  685. struct tipc_net *tn = net_generic(net, tipc_net_id);
  686. struct tipc_link *bcl = tn->bcl;
  687. if (!bcl)
  688. return 0;
  689. tipc_bclink_lock(net);
  690. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  691. NLM_F_MULTI, TIPC_NL_LINK_GET);
  692. if (!hdr)
  693. return -EMSGSIZE;
  694. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  695. if (!attrs)
  696. goto msg_full;
  697. /* The broadcast link is always up */
  698. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  699. goto attr_msg_full;
  700. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
  701. goto attr_msg_full;
  702. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
  703. goto attr_msg_full;
  704. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->next_in_no))
  705. goto attr_msg_full;
  706. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->next_out_no))
  707. goto attr_msg_full;
  708. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  709. if (!prop)
  710. goto attr_msg_full;
  711. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->queue_limit[0]))
  712. goto prop_msg_full;
  713. nla_nest_end(msg->skb, prop);
  714. err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
  715. if (err)
  716. goto attr_msg_full;
  717. tipc_bclink_unlock(net);
  718. nla_nest_end(msg->skb, attrs);
  719. genlmsg_end(msg->skb, hdr);
  720. return 0;
  721. prop_msg_full:
  722. nla_nest_cancel(msg->skb, prop);
  723. attr_msg_full:
  724. nla_nest_cancel(msg->skb, attrs);
  725. msg_full:
  726. tipc_bclink_unlock(net);
  727. genlmsg_cancel(msg->skb, hdr);
  728. return -EMSGSIZE;
  729. }
  730. int tipc_bclink_reset_stats(struct net *net)
  731. {
  732. struct tipc_net *tn = net_generic(net, tipc_net_id);
  733. struct tipc_link *bcl = tn->bcl;
  734. if (!bcl)
  735. return -ENOPROTOOPT;
  736. tipc_bclink_lock(net);
  737. memset(&bcl->stats, 0, sizeof(bcl->stats));
  738. tipc_bclink_unlock(net);
  739. return 0;
  740. }
  741. int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
  742. {
  743. struct tipc_net *tn = net_generic(net, tipc_net_id);
  744. struct tipc_link *bcl = tn->bcl;
  745. if (!bcl)
  746. return -ENOPROTOOPT;
  747. if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
  748. return -EINVAL;
  749. tipc_bclink_lock(net);
  750. tipc_link_set_queue_limits(bcl, limit);
  751. tipc_bclink_unlock(net);
  752. return 0;
  753. }
  754. int tipc_bclink_init(struct net *net)
  755. {
  756. struct tipc_net *tn = net_generic(net, tipc_net_id);
  757. struct tipc_bcbearer *bcbearer;
  758. struct tipc_bclink *bclink;
  759. struct tipc_link *bcl;
  760. bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
  761. if (!bcbearer)
  762. return -ENOMEM;
  763. bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
  764. if (!bclink) {
  765. kfree(bcbearer);
  766. return -ENOMEM;
  767. }
  768. bcl = &bclink->link;
  769. bcbearer->bearer.media = &bcbearer->media;
  770. bcbearer->media.send_msg = tipc_bcbearer_send;
  771. sprintf(bcbearer->media.name, "tipc-broadcast");
  772. spin_lock_init(&bclink->lock);
  773. __skb_queue_head_init(&bcl->transmq);
  774. __skb_queue_head_init(&bcl->backlogq);
  775. __skb_queue_head_init(&bcl->deferdq);
  776. skb_queue_head_init(&bcl->wakeupq);
  777. bcl->next_out_no = 1;
  778. spin_lock_init(&bclink->node.lock);
  779. __skb_queue_head_init(&bclink->arrvq);
  780. skb_queue_head_init(&bclink->inputq);
  781. bcl->owner = &bclink->node;
  782. bcl->owner->net = net;
  783. bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
  784. tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
  785. bcl->bearer_id = MAX_BEARERS;
  786. rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
  787. bcl->state = WORKING_WORKING;
  788. bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
  789. msg_set_prevnode(bcl->pmsg, tn->own_addr);
  790. strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  791. tn->bcbearer = bcbearer;
  792. tn->bclink = bclink;
  793. tn->bcl = bcl;
  794. return 0;
  795. }
  796. void tipc_bclink_stop(struct net *net)
  797. {
  798. struct tipc_net *tn = net_generic(net, tipc_net_id);
  799. tipc_bclink_lock(net);
  800. tipc_link_purge_queues(tn->bcl);
  801. tipc_bclink_unlock(net);
  802. RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
  803. synchronize_net();
  804. kfree(tn->bcbearer);
  805. kfree(tn->bclink);
  806. }
  807. /**
  808. * tipc_nmap_add - add a node to a node map
  809. */
  810. static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
  811. {
  812. int n = tipc_node(node);
  813. int w = n / WSIZE;
  814. u32 mask = (1 << (n % WSIZE));
  815. if ((nm_ptr->map[w] & mask) == 0) {
  816. nm_ptr->count++;
  817. nm_ptr->map[w] |= mask;
  818. }
  819. }
  820. /**
  821. * tipc_nmap_remove - remove a node from a node map
  822. */
  823. static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
  824. {
  825. int n = tipc_node(node);
  826. int w = n / WSIZE;
  827. u32 mask = (1 << (n % WSIZE));
  828. if ((nm_ptr->map[w] & mask) != 0) {
  829. nm_ptr->map[w] &= ~mask;
  830. nm_ptr->count--;
  831. }
  832. }
  833. /**
  834. * tipc_nmap_diff - find differences between node maps
  835. * @nm_a: input node map A
  836. * @nm_b: input node map B
  837. * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
  838. */
  839. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  840. struct tipc_node_map *nm_b,
  841. struct tipc_node_map *nm_diff)
  842. {
  843. int stop = ARRAY_SIZE(nm_a->map);
  844. int w;
  845. int b;
  846. u32 map;
  847. memset(nm_diff, 0, sizeof(*nm_diff));
  848. for (w = 0; w < stop; w++) {
  849. map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
  850. nm_diff->map[w] = map;
  851. if (map != 0) {
  852. for (b = 0 ; b < WSIZE; b++) {
  853. if (map & (1 << b))
  854. nm_diff->count++;
  855. }
  856. }
  857. }
  858. }