group.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*
  2. * net/tipc/group.c: TIPC group messaging code
  3. *
  4. * Copyright (c) 2017, Ericsson AB
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright holders nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * Alternatively, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") version 2 as published by the Free
  21. * Software Foundation.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include "core.h"
  36. #include "addr.h"
  37. #include "group.h"
  38. #include "bcast.h"
  39. #include "topsrv.h"
  40. #include "msg.h"
  41. #include "socket.h"
  42. #include "node.h"
  43. #include "name_table.h"
  44. #include "subscr.h"
  45. #define ADV_UNIT (((MAX_MSG_SIZE + MAX_H_SIZE) / FLOWCTL_BLK_SZ) + 1)
  46. #define ADV_IDLE ADV_UNIT
  47. #define ADV_ACTIVE (ADV_UNIT * 12)
  48. enum mbr_state {
  49. MBR_JOINING,
  50. MBR_PUBLISHED,
  51. MBR_JOINED,
  52. MBR_PENDING,
  53. MBR_ACTIVE,
  54. MBR_RECLAIMING,
  55. MBR_REMITTED,
  56. MBR_LEAVING
  57. };
  58. struct tipc_member {
  59. struct rb_node tree_node;
  60. struct list_head list;
  61. struct list_head small_win;
  62. struct sk_buff_head deferredq;
  63. struct tipc_group *group;
  64. u32 node;
  65. u32 port;
  66. u32 instance;
  67. enum mbr_state state;
  68. u16 advertised;
  69. u16 window;
  70. u16 bc_rcv_nxt;
  71. u16 bc_syncpt;
  72. u16 bc_acked;
  73. };
  74. struct tipc_group {
  75. struct rb_root members;
  76. struct list_head small_win;
  77. struct list_head pending;
  78. struct list_head active;
  79. struct tipc_nlist dests;
  80. struct net *net;
  81. int subid;
  82. u32 type;
  83. u32 instance;
  84. u32 scope;
  85. u32 portid;
  86. u16 member_cnt;
  87. u16 active_cnt;
  88. u16 max_active;
  89. u16 bc_snd_nxt;
  90. u16 bc_ackers;
  91. bool *open;
  92. bool loopback;
  93. bool events;
  94. };
  95. static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
  96. int mtyp, struct sk_buff_head *xmitq);
  97. static void tipc_group_open(struct tipc_member *m, bool *wakeup)
  98. {
  99. *wakeup = false;
  100. if (list_empty(&m->small_win))
  101. return;
  102. list_del_init(&m->small_win);
  103. *m->group->open = true;
  104. *wakeup = true;
  105. }
  106. static void tipc_group_decr_active(struct tipc_group *grp,
  107. struct tipc_member *m)
  108. {
  109. if (m->state == MBR_ACTIVE || m->state == MBR_RECLAIMING ||
  110. m->state == MBR_REMITTED)
  111. grp->active_cnt--;
  112. }
  113. static int tipc_group_rcvbuf_limit(struct tipc_group *grp)
  114. {
  115. int max_active, active_pool, idle_pool;
  116. int mcnt = grp->member_cnt + 1;
  117. /* Limit simultaneous reception from other members */
  118. max_active = min(mcnt / 8, 64);
  119. max_active = max(max_active, 16);
  120. grp->max_active = max_active;
  121. /* Reserve blocks for active and idle members */
  122. active_pool = max_active * ADV_ACTIVE;
  123. idle_pool = (mcnt - max_active) * ADV_IDLE;
  124. /* Scale to bytes, considering worst-case truesize/msgsize ratio */
  125. return (active_pool + idle_pool) * FLOWCTL_BLK_SZ * 4;
  126. }
  127. u16 tipc_group_bc_snd_nxt(struct tipc_group *grp)
  128. {
  129. return grp->bc_snd_nxt;
  130. }
  131. static bool tipc_group_is_receiver(struct tipc_member *m)
  132. {
  133. return m && m->state != MBR_JOINING && m->state != MBR_LEAVING;
  134. }
  135. static bool tipc_group_is_sender(struct tipc_member *m)
  136. {
  137. return m && m->state != MBR_JOINING && m->state != MBR_PUBLISHED;
  138. }
  139. u32 tipc_group_exclude(struct tipc_group *grp)
  140. {
  141. if (!grp->loopback)
  142. return grp->portid;
  143. return 0;
  144. }
  145. struct tipc_group *tipc_group_create(struct net *net, u32 portid,
  146. struct tipc_group_req *mreq,
  147. bool *group_is_open)
  148. {
  149. u32 filter = TIPC_SUB_PORTS | TIPC_SUB_NO_STATUS;
  150. bool global = mreq->scope != TIPC_NODE_SCOPE;
  151. struct tipc_group *grp;
  152. u32 type = mreq->type;
  153. grp = kzalloc(sizeof(*grp), GFP_ATOMIC);
  154. if (!grp)
  155. return NULL;
  156. tipc_nlist_init(&grp->dests, tipc_own_addr(net));
  157. INIT_LIST_HEAD(&grp->small_win);
  158. INIT_LIST_HEAD(&grp->active);
  159. INIT_LIST_HEAD(&grp->pending);
  160. grp->members = RB_ROOT;
  161. grp->net = net;
  162. grp->portid = portid;
  163. grp->type = type;
  164. grp->instance = mreq->instance;
  165. grp->scope = mreq->scope;
  166. grp->loopback = mreq->flags & TIPC_GROUP_LOOPBACK;
  167. grp->events = mreq->flags & TIPC_GROUP_MEMBER_EVTS;
  168. grp->open = group_is_open;
  169. *grp->open = false;
  170. filter |= global ? TIPC_SUB_CLUSTER_SCOPE : TIPC_SUB_NODE_SCOPE;
  171. if (tipc_topsrv_kern_subscr(net, portid, type, 0, ~0,
  172. filter, &grp->subid))
  173. return grp;
  174. kfree(grp);
  175. return NULL;
  176. }
  177. void tipc_group_join(struct net *net, struct tipc_group *grp, int *sk_rcvbuf)
  178. {
  179. struct rb_root *tree = &grp->members;
  180. struct tipc_member *m, *tmp;
  181. struct sk_buff_head xmitq;
  182. skb_queue_head_init(&xmitq);
  183. rbtree_postorder_for_each_entry_safe(m, tmp, tree, tree_node) {
  184. tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, &xmitq);
  185. tipc_group_update_member(m, 0);
  186. }
  187. tipc_node_distr_xmit(net, &xmitq);
  188. *sk_rcvbuf = tipc_group_rcvbuf_limit(grp);
  189. }
  190. void tipc_group_delete(struct net *net, struct tipc_group *grp)
  191. {
  192. struct rb_root *tree = &grp->members;
  193. struct tipc_member *m, *tmp;
  194. struct sk_buff_head xmitq;
  195. __skb_queue_head_init(&xmitq);
  196. rbtree_postorder_for_each_entry_safe(m, tmp, tree, tree_node) {
  197. tipc_group_proto_xmit(grp, m, GRP_LEAVE_MSG, &xmitq);
  198. __skb_queue_purge(&m->deferredq);
  199. list_del(&m->list);
  200. kfree(m);
  201. }
  202. tipc_node_distr_xmit(net, &xmitq);
  203. tipc_nlist_purge(&grp->dests);
  204. tipc_topsrv_kern_unsubscr(net, grp->subid);
  205. kfree(grp);
  206. }
  207. static struct tipc_member *tipc_group_find_member(struct tipc_group *grp,
  208. u32 node, u32 port)
  209. {
  210. struct rb_node *n = grp->members.rb_node;
  211. u64 nkey, key = (u64)node << 32 | port;
  212. struct tipc_member *m;
  213. while (n) {
  214. m = container_of(n, struct tipc_member, tree_node);
  215. nkey = (u64)m->node << 32 | m->port;
  216. if (key < nkey)
  217. n = n->rb_left;
  218. else if (key > nkey)
  219. n = n->rb_right;
  220. else
  221. return m;
  222. }
  223. return NULL;
  224. }
  225. static struct tipc_member *tipc_group_find_dest(struct tipc_group *grp,
  226. u32 node, u32 port)
  227. {
  228. struct tipc_member *m;
  229. m = tipc_group_find_member(grp, node, port);
  230. if (m && tipc_group_is_receiver(m))
  231. return m;
  232. return NULL;
  233. }
  234. static struct tipc_member *tipc_group_find_node(struct tipc_group *grp,
  235. u32 node)
  236. {
  237. struct tipc_member *m;
  238. struct rb_node *n;
  239. for (n = rb_first(&grp->members); n; n = rb_next(n)) {
  240. m = container_of(n, struct tipc_member, tree_node);
  241. if (m->node == node)
  242. return m;
  243. }
  244. return NULL;
  245. }
  246. static void tipc_group_add_to_tree(struct tipc_group *grp,
  247. struct tipc_member *m)
  248. {
  249. u64 nkey, key = (u64)m->node << 32 | m->port;
  250. struct rb_node **n, *parent = NULL;
  251. struct tipc_member *tmp;
  252. n = &grp->members.rb_node;
  253. while (*n) {
  254. tmp = container_of(*n, struct tipc_member, tree_node);
  255. parent = *n;
  256. tmp = container_of(parent, struct tipc_member, tree_node);
  257. nkey = (u64)tmp->node << 32 | tmp->port;
  258. if (key < nkey)
  259. n = &(*n)->rb_left;
  260. else if (key > nkey)
  261. n = &(*n)->rb_right;
  262. else
  263. return;
  264. }
  265. rb_link_node(&m->tree_node, parent, n);
  266. rb_insert_color(&m->tree_node, &grp->members);
  267. }
  268. static struct tipc_member *tipc_group_create_member(struct tipc_group *grp,
  269. u32 node, u32 port,
  270. u32 instance, int state)
  271. {
  272. struct tipc_member *m;
  273. m = kzalloc(sizeof(*m), GFP_ATOMIC);
  274. if (!m)
  275. return NULL;
  276. INIT_LIST_HEAD(&m->list);
  277. INIT_LIST_HEAD(&m->small_win);
  278. __skb_queue_head_init(&m->deferredq);
  279. m->group = grp;
  280. m->node = node;
  281. m->port = port;
  282. m->instance = instance;
  283. m->bc_acked = grp->bc_snd_nxt - 1;
  284. grp->member_cnt++;
  285. tipc_group_add_to_tree(grp, m);
  286. tipc_nlist_add(&grp->dests, m->node);
  287. m->state = state;
  288. return m;
  289. }
  290. void tipc_group_add_member(struct tipc_group *grp, u32 node,
  291. u32 port, u32 instance)
  292. {
  293. tipc_group_create_member(grp, node, port, instance, MBR_PUBLISHED);
  294. }
  295. static void tipc_group_delete_member(struct tipc_group *grp,
  296. struct tipc_member *m)
  297. {
  298. rb_erase(&m->tree_node, &grp->members);
  299. grp->member_cnt--;
  300. /* Check if we were waiting for replicast ack from this member */
  301. if (grp->bc_ackers && less(m->bc_acked, grp->bc_snd_nxt - 1))
  302. grp->bc_ackers--;
  303. list_del_init(&m->list);
  304. list_del_init(&m->small_win);
  305. tipc_group_decr_active(grp, m);
  306. /* If last member on a node, remove node from dest list */
  307. if (!tipc_group_find_node(grp, m->node))
  308. tipc_nlist_del(&grp->dests, m->node);
  309. kfree(m);
  310. }
  311. struct tipc_nlist *tipc_group_dests(struct tipc_group *grp)
  312. {
  313. return &grp->dests;
  314. }
  315. void tipc_group_self(struct tipc_group *grp, struct tipc_name_seq *seq,
  316. int *scope)
  317. {
  318. seq->type = grp->type;
  319. seq->lower = grp->instance;
  320. seq->upper = grp->instance;
  321. *scope = grp->scope;
  322. }
  323. void tipc_group_update_member(struct tipc_member *m, int len)
  324. {
  325. struct tipc_group *grp = m->group;
  326. struct tipc_member *_m, *tmp;
  327. if (!tipc_group_is_receiver(m))
  328. return;
  329. m->window -= len;
  330. if (m->window >= ADV_IDLE)
  331. return;
  332. list_del_init(&m->small_win);
  333. /* Sort member into small_window members' list */
  334. list_for_each_entry_safe(_m, tmp, &grp->small_win, small_win) {
  335. if (_m->window > m->window)
  336. break;
  337. }
  338. list_add_tail(&m->small_win, &_m->small_win);
  339. }
  340. void tipc_group_update_bc_members(struct tipc_group *grp, int len, bool ack)
  341. {
  342. u16 prev = grp->bc_snd_nxt - 1;
  343. struct tipc_member *m;
  344. struct rb_node *n;
  345. u16 ackers = 0;
  346. for (n = rb_first(&grp->members); n; n = rb_next(n)) {
  347. m = container_of(n, struct tipc_member, tree_node);
  348. if (tipc_group_is_receiver(m)) {
  349. tipc_group_update_member(m, len);
  350. m->bc_acked = prev;
  351. ackers++;
  352. }
  353. }
  354. /* Mark number of acknowledges to expect, if any */
  355. if (ack)
  356. grp->bc_ackers = ackers;
  357. grp->bc_snd_nxt++;
  358. }
  359. bool tipc_group_cong(struct tipc_group *grp, u32 dnode, u32 dport,
  360. int len, struct tipc_member **mbr)
  361. {
  362. struct sk_buff_head xmitq;
  363. struct tipc_member *m;
  364. int adv, state;
  365. m = tipc_group_find_dest(grp, dnode, dport);
  366. if (!tipc_group_is_receiver(m)) {
  367. *mbr = NULL;
  368. return false;
  369. }
  370. *mbr = m;
  371. if (m->window >= len)
  372. return false;
  373. *grp->open = false;
  374. /* If not fully advertised, do it now to prevent mutual blocking */
  375. adv = m->advertised;
  376. state = m->state;
  377. if (state == MBR_JOINED && adv == ADV_IDLE)
  378. return true;
  379. if (state == MBR_ACTIVE && adv == ADV_ACTIVE)
  380. return true;
  381. if (state == MBR_PENDING && adv == ADV_IDLE)
  382. return true;
  383. skb_queue_head_init(&xmitq);
  384. tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, &xmitq);
  385. tipc_node_distr_xmit(grp->net, &xmitq);
  386. return true;
  387. }
  388. bool tipc_group_bc_cong(struct tipc_group *grp, int len)
  389. {
  390. struct tipc_member *m = NULL;
  391. /* If prev bcast was replicast, reject until all receivers have acked */
  392. if (grp->bc_ackers) {
  393. *grp->open = false;
  394. return true;
  395. }
  396. if (list_empty(&grp->small_win))
  397. return false;
  398. m = list_first_entry(&grp->small_win, struct tipc_member, small_win);
  399. if (m->window >= len)
  400. return false;
  401. return tipc_group_cong(grp, m->node, m->port, len, &m);
  402. }
  403. /* tipc_group_sort_msg() - sort msg into queue by bcast sequence number
  404. */
  405. static void tipc_group_sort_msg(struct sk_buff *skb, struct sk_buff_head *defq)
  406. {
  407. struct tipc_msg *_hdr, *hdr = buf_msg(skb);
  408. u16 bc_seqno = msg_grp_bc_seqno(hdr);
  409. struct sk_buff *_skb, *tmp;
  410. int mtyp = msg_type(hdr);
  411. /* Bcast/mcast may be bypassed by ucast or other bcast, - sort it in */
  412. if (mtyp == TIPC_GRP_BCAST_MSG || mtyp == TIPC_GRP_MCAST_MSG) {
  413. skb_queue_walk_safe(defq, _skb, tmp) {
  414. _hdr = buf_msg(_skb);
  415. if (!less(bc_seqno, msg_grp_bc_seqno(_hdr)))
  416. continue;
  417. __skb_queue_before(defq, _skb, skb);
  418. return;
  419. }
  420. /* Bcast was not bypassed, - add to tail */
  421. }
  422. /* Unicasts are never bypassed, - always add to tail */
  423. __skb_queue_tail(defq, skb);
  424. }
  425. /* tipc_group_filter_msg() - determine if we should accept arriving message
  426. */
  427. void tipc_group_filter_msg(struct tipc_group *grp, struct sk_buff_head *inputq,
  428. struct sk_buff_head *xmitq)
  429. {
  430. struct sk_buff *skb = __skb_dequeue(inputq);
  431. bool ack, deliver, update, leave = false;
  432. struct sk_buff_head *defq;
  433. struct tipc_member *m;
  434. struct tipc_msg *hdr;
  435. u32 node, port;
  436. int mtyp, blks;
  437. if (!skb)
  438. return;
  439. hdr = buf_msg(skb);
  440. node = msg_orignode(hdr);
  441. port = msg_origport(hdr);
  442. if (!msg_in_group(hdr))
  443. goto drop;
  444. m = tipc_group_find_member(grp, node, port);
  445. if (!tipc_group_is_sender(m))
  446. goto drop;
  447. if (less(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt))
  448. goto drop;
  449. TIPC_SKB_CB(skb)->orig_member = m->instance;
  450. defq = &m->deferredq;
  451. tipc_group_sort_msg(skb, defq);
  452. while ((skb = skb_peek(defq))) {
  453. hdr = buf_msg(skb);
  454. mtyp = msg_type(hdr);
  455. blks = msg_blocks(hdr);
  456. deliver = true;
  457. ack = false;
  458. update = false;
  459. if (more(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt))
  460. break;
  461. /* Decide what to do with message */
  462. switch (mtyp) {
  463. case TIPC_GRP_MCAST_MSG:
  464. if (msg_nameinst(hdr) != grp->instance) {
  465. update = true;
  466. deliver = false;
  467. }
  468. /* Fall thru */
  469. case TIPC_GRP_BCAST_MSG:
  470. m->bc_rcv_nxt++;
  471. ack = msg_grp_bc_ack_req(hdr);
  472. break;
  473. case TIPC_GRP_UCAST_MSG:
  474. break;
  475. case TIPC_GRP_MEMBER_EVT:
  476. if (m->state == MBR_LEAVING)
  477. leave = true;
  478. if (!grp->events)
  479. deliver = false;
  480. break;
  481. default:
  482. break;
  483. }
  484. /* Execute decisions */
  485. __skb_dequeue(defq);
  486. if (deliver)
  487. __skb_queue_tail(inputq, skb);
  488. else
  489. kfree_skb(skb);
  490. if (ack)
  491. tipc_group_proto_xmit(grp, m, GRP_ACK_MSG, xmitq);
  492. if (leave) {
  493. __skb_queue_purge(defq);
  494. tipc_group_delete_member(grp, m);
  495. break;
  496. }
  497. if (!update)
  498. continue;
  499. tipc_group_update_rcv_win(grp, blks, node, port, xmitq);
  500. }
  501. return;
  502. drop:
  503. kfree_skb(skb);
  504. }
  505. void tipc_group_update_rcv_win(struct tipc_group *grp, int blks, u32 node,
  506. u32 port, struct sk_buff_head *xmitq)
  507. {
  508. struct list_head *active = &grp->active;
  509. int max_active = grp->max_active;
  510. int reclaim_limit = max_active * 3 / 4;
  511. int active_cnt = grp->active_cnt;
  512. struct tipc_member *m, *rm, *pm;
  513. m = tipc_group_find_member(grp, node, port);
  514. if (!m)
  515. return;
  516. m->advertised -= blks;
  517. switch (m->state) {
  518. case MBR_JOINED:
  519. /* First, decide if member can go active */
  520. if (active_cnt <= max_active) {
  521. m->state = MBR_ACTIVE;
  522. list_add_tail(&m->list, active);
  523. grp->active_cnt++;
  524. tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
  525. } else {
  526. m->state = MBR_PENDING;
  527. list_add_tail(&m->list, &grp->pending);
  528. }
  529. if (active_cnt < reclaim_limit)
  530. break;
  531. /* Reclaim from oldest active member, if possible */
  532. if (!list_empty(active)) {
  533. rm = list_first_entry(active, struct tipc_member, list);
  534. rm->state = MBR_RECLAIMING;
  535. list_del_init(&rm->list);
  536. tipc_group_proto_xmit(grp, rm, GRP_RECLAIM_MSG, xmitq);
  537. break;
  538. }
  539. /* Nobody to reclaim from; - revert oldest pending to JOINED */
  540. pm = list_first_entry(&grp->pending, struct tipc_member, list);
  541. list_del_init(&pm->list);
  542. pm->state = MBR_JOINED;
  543. tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
  544. break;
  545. case MBR_ACTIVE:
  546. if (!list_is_last(&m->list, &grp->active))
  547. list_move_tail(&m->list, &grp->active);
  548. if (m->advertised > (ADV_ACTIVE * 3 / 4))
  549. break;
  550. tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
  551. break;
  552. case MBR_REMITTED:
  553. if (m->advertised > ADV_IDLE)
  554. break;
  555. m->state = MBR_JOINED;
  556. grp->active_cnt--;
  557. if (m->advertised < ADV_IDLE) {
  558. pr_warn_ratelimited("Rcv unexpected msg after REMIT\n");
  559. tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
  560. }
  561. if (list_empty(&grp->pending))
  562. return;
  563. /* Set oldest pending member to active and advertise */
  564. pm = list_first_entry(&grp->pending, struct tipc_member, list);
  565. pm->state = MBR_ACTIVE;
  566. list_move_tail(&pm->list, &grp->active);
  567. grp->active_cnt++;
  568. tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
  569. break;
  570. case MBR_RECLAIMING:
  571. case MBR_JOINING:
  572. case MBR_LEAVING:
  573. default:
  574. break;
  575. }
  576. }
  577. static void tipc_group_create_event(struct tipc_group *grp,
  578. struct tipc_member *m,
  579. u32 event, u16 seqno,
  580. struct sk_buff_head *inputq)
  581. { u32 dnode = tipc_own_addr(grp->net);
  582. struct tipc_event evt;
  583. struct sk_buff *skb;
  584. struct tipc_msg *hdr;
  585. memset(&evt, 0, sizeof(evt));
  586. evt.event = event;
  587. evt.found_lower = m->instance;
  588. evt.found_upper = m->instance;
  589. evt.port.ref = m->port;
  590. evt.port.node = m->node;
  591. evt.s.seq.type = grp->type;
  592. evt.s.seq.lower = m->instance;
  593. evt.s.seq.upper = m->instance;
  594. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_GRP_MEMBER_EVT,
  595. GROUP_H_SIZE, sizeof(evt), dnode, m->node,
  596. grp->portid, m->port, 0);
  597. if (!skb)
  598. return;
  599. hdr = buf_msg(skb);
  600. msg_set_nametype(hdr, grp->type);
  601. msg_set_grp_evt(hdr, event);
  602. msg_set_dest_droppable(hdr, true);
  603. msg_set_grp_bc_seqno(hdr, seqno);
  604. memcpy(msg_data(hdr), &evt, sizeof(evt));
  605. TIPC_SKB_CB(skb)->orig_member = m->instance;
  606. __skb_queue_tail(inputq, skb);
  607. }
  608. static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
  609. int mtyp, struct sk_buff_head *xmitq)
  610. {
  611. struct tipc_msg *hdr;
  612. struct sk_buff *skb;
  613. int adv = 0;
  614. skb = tipc_msg_create(GROUP_PROTOCOL, mtyp, INT_H_SIZE, 0,
  615. m->node, tipc_own_addr(grp->net),
  616. m->port, grp->portid, 0);
  617. if (!skb)
  618. return;
  619. if (m->state == MBR_ACTIVE)
  620. adv = ADV_ACTIVE - m->advertised;
  621. else if (m->state == MBR_JOINED || m->state == MBR_PENDING)
  622. adv = ADV_IDLE - m->advertised;
  623. hdr = buf_msg(skb);
  624. if (mtyp == GRP_JOIN_MSG) {
  625. msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
  626. msg_set_adv_win(hdr, adv);
  627. m->advertised += adv;
  628. } else if (mtyp == GRP_LEAVE_MSG) {
  629. msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
  630. } else if (mtyp == GRP_ADV_MSG) {
  631. msg_set_adv_win(hdr, adv);
  632. m->advertised += adv;
  633. } else if (mtyp == GRP_ACK_MSG) {
  634. msg_set_grp_bc_acked(hdr, m->bc_rcv_nxt);
  635. } else if (mtyp == GRP_REMIT_MSG) {
  636. msg_set_grp_remitted(hdr, m->window);
  637. }
  638. msg_set_dest_droppable(hdr, true);
  639. __skb_queue_tail(xmitq, skb);
  640. }
  641. void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup,
  642. struct tipc_msg *hdr, struct sk_buff_head *inputq,
  643. struct sk_buff_head *xmitq)
  644. {
  645. u32 node = msg_orignode(hdr);
  646. u32 port = msg_origport(hdr);
  647. struct tipc_member *m, *pm;
  648. u16 remitted, in_flight;
  649. if (!grp)
  650. return;
  651. if (grp->scope == TIPC_NODE_SCOPE && node != tipc_own_addr(grp->net))
  652. return;
  653. m = tipc_group_find_member(grp, node, port);
  654. switch (msg_type(hdr)) {
  655. case GRP_JOIN_MSG:
  656. if (!m)
  657. m = tipc_group_create_member(grp, node, port,
  658. 0, MBR_JOINING);
  659. if (!m)
  660. return;
  661. m->bc_syncpt = msg_grp_bc_syncpt(hdr);
  662. m->bc_rcv_nxt = m->bc_syncpt;
  663. m->window += msg_adv_win(hdr);
  664. /* Wait until PUBLISH event is received if necessary */
  665. if (m->state != MBR_PUBLISHED)
  666. return;
  667. /* Member can be taken into service */
  668. m->state = MBR_JOINED;
  669. tipc_group_open(m, usr_wakeup);
  670. tipc_group_update_member(m, 0);
  671. tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
  672. tipc_group_create_event(grp, m, TIPC_PUBLISHED,
  673. m->bc_syncpt, inputq);
  674. return;
  675. case GRP_LEAVE_MSG:
  676. if (!m)
  677. return;
  678. m->bc_syncpt = msg_grp_bc_syncpt(hdr);
  679. list_del_init(&m->list);
  680. tipc_group_open(m, usr_wakeup);
  681. tipc_group_decr_active(grp, m);
  682. m->state = MBR_LEAVING;
  683. tipc_group_create_event(grp, m, TIPC_WITHDRAWN,
  684. m->bc_syncpt, inputq);
  685. return;
  686. case GRP_ADV_MSG:
  687. if (!m)
  688. return;
  689. m->window += msg_adv_win(hdr);
  690. tipc_group_open(m, usr_wakeup);
  691. return;
  692. case GRP_ACK_MSG:
  693. if (!m)
  694. return;
  695. m->bc_acked = msg_grp_bc_acked(hdr);
  696. if (--grp->bc_ackers)
  697. return;
  698. list_del_init(&m->small_win);
  699. *m->group->open = true;
  700. *usr_wakeup = true;
  701. tipc_group_update_member(m, 0);
  702. return;
  703. case GRP_RECLAIM_MSG:
  704. if (!m)
  705. return;
  706. tipc_group_proto_xmit(grp, m, GRP_REMIT_MSG, xmitq);
  707. m->window = ADV_IDLE;
  708. tipc_group_open(m, usr_wakeup);
  709. return;
  710. case GRP_REMIT_MSG:
  711. if (!m || m->state != MBR_RECLAIMING)
  712. return;
  713. remitted = msg_grp_remitted(hdr);
  714. /* Messages preceding the REMIT still in receive queue */
  715. if (m->advertised > remitted) {
  716. m->state = MBR_REMITTED;
  717. in_flight = m->advertised - remitted;
  718. m->advertised = ADV_IDLE + in_flight;
  719. return;
  720. }
  721. /* This should never happen */
  722. if (m->advertised < remitted)
  723. pr_warn_ratelimited("Unexpected REMIT msg\n");
  724. /* All messages preceding the REMIT have been read */
  725. m->state = MBR_JOINED;
  726. grp->active_cnt--;
  727. m->advertised = ADV_IDLE;
  728. /* Set oldest pending member to active and advertise */
  729. if (list_empty(&grp->pending))
  730. return;
  731. pm = list_first_entry(&grp->pending, struct tipc_member, list);
  732. pm->state = MBR_ACTIVE;
  733. list_move_tail(&pm->list, &grp->active);
  734. grp->active_cnt++;
  735. if (pm->advertised <= (ADV_ACTIVE * 3 / 4))
  736. tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
  737. return;
  738. default:
  739. pr_warn("Received unknown GROUP_PROTO message\n");
  740. }
  741. }
  742. /* tipc_group_member_evt() - receive and handle a member up/down event
  743. */
  744. void tipc_group_member_evt(struct tipc_group *grp,
  745. bool *usr_wakeup,
  746. int *sk_rcvbuf,
  747. struct tipc_msg *hdr,
  748. struct sk_buff_head *inputq,
  749. struct sk_buff_head *xmitq)
  750. {
  751. struct tipc_event *evt = (void *)msg_data(hdr);
  752. u32 instance = evt->found_lower;
  753. u32 node = evt->port.node;
  754. u32 port = evt->port.ref;
  755. int event = evt->event;
  756. struct tipc_member *m;
  757. struct net *net;
  758. u32 self;
  759. if (!grp)
  760. return;
  761. net = grp->net;
  762. self = tipc_own_addr(net);
  763. if (!grp->loopback && node == self && port == grp->portid)
  764. return;
  765. m = tipc_group_find_member(grp, node, port);
  766. switch (event) {
  767. case TIPC_PUBLISHED:
  768. /* Send and wait for arrival of JOIN message if necessary */
  769. if (!m) {
  770. m = tipc_group_create_member(grp, node, port, instance,
  771. MBR_PUBLISHED);
  772. if (!m)
  773. break;
  774. tipc_group_update_member(m, 0);
  775. tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, xmitq);
  776. break;
  777. }
  778. if (m->state != MBR_JOINING)
  779. break;
  780. /* Member can be taken into service */
  781. m->instance = instance;
  782. m->state = MBR_JOINED;
  783. tipc_group_open(m, usr_wakeup);
  784. tipc_group_update_member(m, 0);
  785. tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, xmitq);
  786. tipc_group_create_event(grp, m, TIPC_PUBLISHED,
  787. m->bc_syncpt, inputq);
  788. break;
  789. case TIPC_WITHDRAWN:
  790. if (!m)
  791. break;
  792. tipc_group_decr_active(grp, m);
  793. m->state = MBR_LEAVING;
  794. list_del_init(&m->list);
  795. tipc_group_open(m, usr_wakeup);
  796. /* Only send event if no LEAVE message can be expected */
  797. if (!tipc_node_is_up(net, node))
  798. tipc_group_create_event(grp, m, TIPC_WITHDRAWN,
  799. m->bc_rcv_nxt, inputq);
  800. break;
  801. default:
  802. break;
  803. }
  804. *sk_rcvbuf = tipc_group_rcvbuf_limit(grp);
  805. }
  806. int tipc_group_fill_sock_diag(struct tipc_group *grp, struct sk_buff *skb)
  807. {
  808. struct nlattr *group = nla_nest_start(skb, TIPC_NLA_SOCK_GROUP);
  809. if (nla_put_u32(skb, TIPC_NLA_SOCK_GROUP_ID,
  810. grp->type) ||
  811. nla_put_u32(skb, TIPC_NLA_SOCK_GROUP_INSTANCE,
  812. grp->instance) ||
  813. nla_put_u32(skb, TIPC_NLA_SOCK_GROUP_BC_SEND_NEXT,
  814. grp->bc_snd_nxt))
  815. goto group_msg_cancel;
  816. if (grp->scope == TIPC_NODE_SCOPE)
  817. if (nla_put_flag(skb, TIPC_NLA_SOCK_GROUP_NODE_SCOPE))
  818. goto group_msg_cancel;
  819. if (grp->scope == TIPC_CLUSTER_SCOPE)
  820. if (nla_put_flag(skb, TIPC_NLA_SOCK_GROUP_CLUSTER_SCOPE))
  821. goto group_msg_cancel;
  822. if (*grp->open)
  823. if (nla_put_flag(skb, TIPC_NLA_SOCK_GROUP_OPEN))
  824. goto group_msg_cancel;
  825. nla_nest_end(skb, group);
  826. return 0;
  827. group_msg_cancel:
  828. nla_nest_cancel(skb, group);
  829. return -1;
  830. }