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