group.c 22 KB

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