bcast.c 27 KB

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