bcast.c 28 KB

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