bcast.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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_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_reset_stats(struct net *net)
  735. {
  736. struct tipc_net *tn = net_generic(net, tipc_net_id);
  737. struct tipc_link *bcl = tn->bcl;
  738. if (!bcl)
  739. return -ENOPROTOOPT;
  740. tipc_bclink_lock(net);
  741. memset(&bcl->stats, 0, sizeof(bcl->stats));
  742. tipc_bclink_unlock(net);
  743. return 0;
  744. }
  745. int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
  746. {
  747. struct tipc_net *tn = net_generic(net, tipc_net_id);
  748. struct tipc_link *bcl = tn->bcl;
  749. if (!bcl)
  750. return -ENOPROTOOPT;
  751. if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
  752. return -EINVAL;
  753. tipc_bclink_lock(net);
  754. tipc_link_set_queue_limits(bcl, limit);
  755. tipc_bclink_unlock(net);
  756. return 0;
  757. }
  758. int tipc_bclink_init(struct net *net)
  759. {
  760. struct tipc_net *tn = net_generic(net, tipc_net_id);
  761. struct tipc_bcbearer *bcbearer;
  762. struct tipc_bclink *bclink;
  763. struct tipc_link *bcl;
  764. bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
  765. if (!bcbearer)
  766. return -ENOMEM;
  767. bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
  768. if (!bclink) {
  769. kfree(bcbearer);
  770. return -ENOMEM;
  771. }
  772. bcl = &bclink->link;
  773. bcbearer->bearer.media = &bcbearer->media;
  774. bcbearer->media.send_msg = tipc_bcbearer_send;
  775. sprintf(bcbearer->media.name, "tipc-broadcast");
  776. spin_lock_init(&bclink->lock);
  777. __skb_queue_head_init(&bcl->outqueue);
  778. __skb_queue_head_init(&bcl->deferred_queue);
  779. skb_queue_head_init(&bcl->wakeupq);
  780. bcl->next_out_no = 1;
  781. spin_lock_init(&bclink->node.lock);
  782. __skb_queue_head_init(&bclink->arrvq);
  783. skb_queue_head_init(&bclink->inputq);
  784. bcl->owner = &bclink->node;
  785. bcl->owner->net = net;
  786. bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
  787. tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
  788. bcl->bearer_id = MAX_BEARERS;
  789. rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
  790. bcl->state = WORKING_WORKING;
  791. bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
  792. msg_set_prevnode(bcl->pmsg, tn->own_addr);
  793. strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  794. tn->bcbearer = bcbearer;
  795. tn->bclink = bclink;
  796. tn->bcl = bcl;
  797. return 0;
  798. }
  799. void tipc_bclink_stop(struct net *net)
  800. {
  801. struct tipc_net *tn = net_generic(net, tipc_net_id);
  802. tipc_bclink_lock(net);
  803. tipc_link_purge_queues(tn->bcl);
  804. tipc_bclink_unlock(net);
  805. RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
  806. synchronize_net();
  807. kfree(tn->bcbearer);
  808. kfree(tn->bclink);
  809. }
  810. /**
  811. * tipc_nmap_add - add a node to a node map
  812. */
  813. static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
  814. {
  815. int n = tipc_node(node);
  816. int w = n / WSIZE;
  817. u32 mask = (1 << (n % WSIZE));
  818. if ((nm_ptr->map[w] & mask) == 0) {
  819. nm_ptr->count++;
  820. nm_ptr->map[w] |= mask;
  821. }
  822. }
  823. /**
  824. * tipc_nmap_remove - remove a node from a node map
  825. */
  826. static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
  827. {
  828. int n = tipc_node(node);
  829. int w = n / WSIZE;
  830. u32 mask = (1 << (n % WSIZE));
  831. if ((nm_ptr->map[w] & mask) != 0) {
  832. nm_ptr->map[w] &= ~mask;
  833. nm_ptr->count--;
  834. }
  835. }
  836. /**
  837. * tipc_nmap_diff - find differences between node maps
  838. * @nm_a: input node map A
  839. * @nm_b: input node map B
  840. * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
  841. */
  842. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  843. struct tipc_node_map *nm_b,
  844. struct tipc_node_map *nm_diff)
  845. {
  846. int stop = ARRAY_SIZE(nm_a->map);
  847. int w;
  848. int b;
  849. u32 map;
  850. memset(nm_diff, 0, sizeof(*nm_diff));
  851. for (w = 0; w < stop; w++) {
  852. map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
  853. nm_diff->map[w] = map;
  854. if (map != 0) {
  855. for (b = 0 ; b < WSIZE; b++) {
  856. if (map & (1 << b))
  857. nm_diff->count++;
  858. }
  859. }
  860. }
  861. }