bcast.c 28 KB

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