bcast.c 28 KB

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