bcast.c 26 KB

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