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