bcast.c 28 KB

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