blob: 3e137d8c9d2ff5c368c26f78607c5755ab69f781 [file] [log] [blame]
Jon Maloy75da2162017-10-13 11:04:23 +02001/*
2 * net/tipc/group.c: TIPC group messaging code
3 *
4 * Copyright (c) 2017, Ericsson AB
Jon Maloyb6f88d92020-11-25 13:29:15 -05005 * Copyright (c) 2020, Red Hat Inc
Jon Maloy75da2162017-10-13 11:04:23 +02006 * 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"
Jon Maloy026321c2018-02-15 10:40:51 +010041#include "topsrv.h"
Jon Maloy75da2162017-10-13 11:04:23 +020042#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
Jon Maloyb7d42632017-10-13 11:04:26 +020050#define ADV_ACTIVE (ADV_UNIT * 12)
Jon Maloy75da2162017-10-13 11:04:23 +020051
52enum mbr_state {
Jon Maloy75da2162017-10-13 11:04:23 +020053 MBR_JOINING,
54 MBR_PUBLISHED,
55 MBR_JOINED,
Jon Maloy04d7b572017-10-13 11:04:34 +020056 MBR_PENDING,
57 MBR_ACTIVE,
58 MBR_RECLAIMING,
59 MBR_REMITTED,
Jon Maloy75da2162017-10-13 11:04:23 +020060 MBR_LEAVING
61};
62
63struct tipc_member {
64 struct rb_node tree_node;
65 struct list_head list;
Jon Maloy38266ca2018-01-04 15:20:44 +010066 struct list_head small_win;
Jon Maloyb87a5ea2017-10-13 11:04:30 +020067 struct sk_buff_head deferredq;
Jon Maloyb7d42632017-10-13 11:04:26 +020068 struct tipc_group *group;
Jon Maloy75da2162017-10-13 11:04:23 +020069 u32 node;
70 u32 port;
Jon Maloy31c82a22017-10-13 11:04:24 +020071 u32 instance;
Jon Maloy75da2162017-10-13 11:04:23 +020072 enum mbr_state state;
Jon Maloyb7d42632017-10-13 11:04:26 +020073 u16 advertised;
74 u16 window;
Jon Maloy75da2162017-10-13 11:04:23 +020075 u16 bc_rcv_nxt;
Jon Maloya3bada72017-10-13 11:04:33 +020076 u16 bc_syncpt;
Jon Maloy2f487712017-10-13 11:04:31 +020077 u16 bc_acked;
Jon Maloy75da2162017-10-13 11:04:23 +020078};
79
80struct tipc_group {
81 struct rb_root members;
Jon Maloy38266ca2018-01-04 15:20:44 +010082 struct list_head small_win;
Jon Maloy04d7b572017-10-13 11:04:34 +020083 struct list_head pending;
84 struct list_head active;
Jon Maloy75da2162017-10-13 11:04:23 +020085 struct tipc_nlist dests;
86 struct net *net;
87 int subid;
88 u32 type;
89 u32 instance;
Jon Maloy75da2162017-10-13 11:04:23 +020090 u32 scope;
91 u32 portid;
92 u16 member_cnt;
Jon Maloy04d7b572017-10-13 11:04:34 +020093 u16 active_cnt;
94 u16 max_active;
Jon Maloy75da2162017-10-13 11:04:23 +020095 u16 bc_snd_nxt;
Jon Maloy2f487712017-10-13 11:04:31 +020096 u16 bc_ackers;
Jon Maloy60c25302018-01-17 16:42:46 +010097 bool *open;
Jon Maloy75da2162017-10-13 11:04:23 +020098 bool loopback;
Jon Maloyae236fb2017-10-13 11:04:25 +020099 bool events;
Jon Maloy75da2162017-10-13 11:04:23 +0200100};
101
102static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
103 int mtyp, struct sk_buff_head *xmitq);
104
Jon Maloyeb929a92018-01-08 21:03:31 +0100105static 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);
Jon Maloy60c25302018-01-17 16:42:46 +0100111 *m->group->open = true;
Jon Maloyeb929a92018-01-08 21:03:31 +0100112 *wakeup = true;
113}
114
Jon Maloy04d7b572017-10-13 11:04:34 +0200115static void tipc_group_decr_active(struct tipc_group *grp,
116 struct tipc_member *m)
117{
Jon Maloyf9c935d2017-12-29 19:48:02 +0100118 if (m->state == MBR_ACTIVE || m->state == MBR_RECLAIMING ||
119 m->state == MBR_REMITTED)
Jon Maloy04d7b572017-10-13 11:04:34 +0200120 grp->active_cnt--;
121}
122
Jon Maloyb7d42632017-10-13 11:04:26 +0200123static int tipc_group_rcvbuf_limit(struct tipc_group *grp)
124{
Jon Maloy04d7b572017-10-13 11:04:34 +0200125 int max_active, active_pool, idle_pool;
Jon Maloyb7d42632017-10-13 11:04:26 +0200126 int mcnt = grp->member_cnt + 1;
127
Jon Maloy04d7b572017-10-13 11:04:34 +0200128 /* 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
Jon Maloyb7d42632017-10-13 11:04:26 +0200137 /* Scale to bytes, considering worst-case truesize/msgsize ratio */
Jon Maloy04d7b572017-10-13 11:04:34 +0200138 return (active_pool + idle_pool) * FLOWCTL_BLK_SZ * 4;
Jon Maloyb7d42632017-10-13 11:04:26 +0200139}
140
Jon Maloy75da2162017-10-13 11:04:23 +0200141u16 tipc_group_bc_snd_nxt(struct tipc_group *grp)
142{
143 return grp->bc_snd_nxt;
144}
145
Jon Maloy38266ca2018-01-04 15:20:44 +0100146static bool tipc_group_is_receiver(struct tipc_member *m)
Jon Maloyb7d42632017-10-13 11:04:26 +0200147{
Jon Maloy02334932018-01-08 21:03:25 +0100148 return m && m->state != MBR_JOINING && m->state != MBR_LEAVING;
Jon Maloyb7d42632017-10-13 11:04:26 +0200149}
150
Jon Maloy38266ca2018-01-04 15:20:44 +0100151static bool tipc_group_is_sender(struct tipc_member *m)
Jon Maloy75da2162017-10-13 11:04:23 +0200152{
Jon Maloyd12d2e12018-01-08 21:03:28 +0100153 return m && m->state != MBR_JOINING && m->state != MBR_PUBLISHED;
Jon Maloy75da2162017-10-13 11:04:23 +0200154}
155
Jon Maloyee106d72017-10-13 11:04:28 +0200156u32 tipc_group_exclude(struct tipc_group *grp)
157{
158 if (!grp->loopback)
159 return grp->portid;
160 return 0;
161}
162
Jon Maloy75da2162017-10-13 11:04:23 +0200163struct tipc_group *tipc_group_create(struct net *net, u32 portid,
Jon Maloy60c25302018-01-17 16:42:46 +0100164 struct tipc_group_req *mreq,
165 bool *group_is_open)
Jon Maloy75da2162017-10-13 11:04:23 +0200166{
Jon Maloy232d07b2018-01-08 21:03:30 +0100167 u32 filter = TIPC_SUB_PORTS | TIPC_SUB_NO_STATUS;
168 bool global = mreq->scope != TIPC_NODE_SCOPE;
Jon Maloy75da2162017-10-13 11:04:23 +0200169 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));
Jon Maloy38266ca2018-01-04 15:20:44 +0100176 INIT_LIST_HEAD(&grp->small_win);
Jon Maloy04d7b572017-10-13 11:04:34 +0200177 INIT_LIST_HEAD(&grp->active);
178 INIT_LIST_HEAD(&grp->pending);
Jon Maloy75da2162017-10-13 11:04:23 +0200179 grp->members = RB_ROOT;
180 grp->net = net;
181 grp->portid = portid;
Jon Maloy75da2162017-10-13 11:04:23 +0200182 grp->type = type;
183 grp->instance = mreq->instance;
184 grp->scope = mreq->scope;
185 grp->loopback = mreq->flags & TIPC_GROUP_LOOPBACK;
Jon Maloyae236fb2017-10-13 11:04:25 +0200186 grp->events = mreq->flags & TIPC_GROUP_MEMBER_EVTS;
Jon Maloy60c25302018-01-17 16:42:46 +0100187 grp->open = group_is_open;
Jon Maloy1b22bca2018-02-26 20:14:04 +0100188 *grp->open = false;
Jon Maloy232d07b2018-01-08 21:03:30 +0100189 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))
Jon Maloy75da2162017-10-13 11:04:23 +0200192 return grp;
193 kfree(grp);
194 return NULL;
195}
196
Jon Maloyd12d2e12018-01-08 21:03:28 +0100197void 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
Jon Maloye654f9f2019-08-15 16:42:50 +0200203 __skb_queue_head_init(&xmitq);
Jon Maloyd12d2e12018-01-08 21:03:28 +0100204 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
Jon Maloy75da2162017-10-13 11:04:23 +0200212void 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);
Xin Long5cf02612019-06-16 17:24:07 +0800222 __skb_queue_purge(&m->deferredq);
Jon Maloy75da2162017-10-13 11:04:23 +0200223 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
YueHaibinge064cce2018-07-19 17:16:59 +0800232static struct tipc_member *tipc_group_find_member(struct tipc_group *grp,
233 u32 node, u32 port)
Jon Maloy75da2162017-10-13 11:04:23 +0200234{
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
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200252static 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);
Jon Maloy38266ca2018-01-04 15:20:44 +0100258 if (m && tipc_group_is_receiver(m))
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200259 return m;
260 return NULL;
261}
262
Jon Maloy75da2162017-10-13 11:04:23 +0200263static 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
Peilin Yebb3a4202020-09-13 04:06:05 -0400277static int tipc_group_add_to_tree(struct tipc_group *grp,
278 struct tipc_member *m)
Jon Maloy75da2162017-10-13 11:04:23 +0200279{
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
Peilin Yebb3a4202020-09-13 04:06:05 -0400295 return -EEXIST;
Jon Maloy75da2162017-10-13 11:04:23 +0200296 }
297 rb_link_node(&m->tree_node, parent, n);
298 rb_insert_color(&m->tree_node, &grp->members);
Peilin Yebb3a4202020-09-13 04:06:05 -0400299 return 0;
Jon Maloy75da2162017-10-13 11:04:23 +0200300}
301
302static struct tipc_member *tipc_group_create_member(struct tipc_group *grp,
303 u32 node, u32 port,
Jon Maloyd12d2e12018-01-08 21:03:28 +0100304 u32 instance, int state)
Jon Maloy75da2162017-10-13 11:04:23 +0200305{
306 struct tipc_member *m;
Peilin Yebb3a4202020-09-13 04:06:05 -0400307 int ret;
Jon Maloy75da2162017-10-13 11:04:23 +0200308
309 m = kzalloc(sizeof(*m), GFP_ATOMIC);
310 if (!m)
311 return NULL;
312 INIT_LIST_HEAD(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100313 INIT_LIST_HEAD(&m->small_win);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200314 __skb_queue_head_init(&m->deferredq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200315 m->group = grp;
Jon Maloy75da2162017-10-13 11:04:23 +0200316 m->node = node;
317 m->port = port;
Jon Maloyd12d2e12018-01-08 21:03:28 +0100318 m->instance = instance;
Jon Maloy2f487712017-10-13 11:04:31 +0200319 m->bc_acked = grp->bc_snd_nxt - 1;
Peilin Yebb3a4202020-09-13 04:06:05 -0400320 ret = tipc_group_add_to_tree(grp, m);
321 if (ret < 0) {
322 kfree(m);
323 return NULL;
324 }
Jon Maloy75da2162017-10-13 11:04:23 +0200325 grp->member_cnt++;
Jon Maloy75da2162017-10-13 11:04:23 +0200326 tipc_nlist_add(&grp->dests, m->node);
327 m->state = state;
328 return m;
329}
330
Jon Maloyd12d2e12018-01-08 21:03:28 +0100331void tipc_group_add_member(struct tipc_group *grp, u32 node,
332 u32 port, u32 instance)
Jon Maloy75da2162017-10-13 11:04:23 +0200333{
Jon Maloyd12d2e12018-01-08 21:03:28 +0100334 tipc_group_create_member(grp, node, port, instance, MBR_PUBLISHED);
Jon Maloy75da2162017-10-13 11:04:23 +0200335}
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--;
Jon Maloy2f487712017-10-13 11:04:31 +0200342
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
Jon Maloy75da2162017-10-13 11:04:23 +0200347 list_del_init(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100348 list_del_init(&m->small_win);
Jon Maloy04d7b572017-10-13 11:04:34 +0200349 tipc_group_decr_active(grp, m);
Jon Maloy75da2162017-10-13 11:04:23 +0200350
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
Jon Maloyb6f88d92020-11-25 13:29:15 -0500363void tipc_group_self(struct tipc_group *grp, struct tipc_service_range *seq,
Jon Maloy75da2162017-10-13 11:04:23 +0200364 int *scope)
365{
366 seq->type = grp->type;
367 seq->lower = grp->instance;
368 seq->upper = grp->instance;
369 *scope = grp->scope;
370}
371
Jon Maloyb7d42632017-10-13 11:04:26 +0200372void tipc_group_update_member(struct tipc_member *m, int len)
Jon Maloy75da2162017-10-13 11:04:23 +0200373{
Jon Maloyb7d42632017-10-13 11:04:26 +0200374 struct tipc_group *grp = m->group;
375 struct tipc_member *_m, *tmp;
376
Jon Maloy38266ca2018-01-04 15:20:44 +0100377 if (!tipc_group_is_receiver(m))
Jon Maloyb7d42632017-10-13 11:04:26 +0200378 return;
379
380 m->window -= len;
381
382 if (m->window >= ADV_IDLE)
383 return;
384
Jon Maloy38266ca2018-01-04 15:20:44 +0100385 list_del_init(&m->small_win);
Jon Maloyb7d42632017-10-13 11:04:26 +0200386
Jon Maloy38266ca2018-01-04 15:20:44 +0100387 /* Sort member into small_window members' list */
388 list_for_each_entry_safe(_m, tmp, &grp->small_win, small_win) {
Jon Maloyd84d1b32018-01-04 15:20:45 +0100389 if (_m->window > m->window)
390 break;
Jon Maloyb7d42632017-10-13 11:04:26 +0200391 }
Jon Maloyd84d1b32018-01-04 15:20:45 +0100392 list_add_tail(&m->small_win, &_m->small_win);
Jon Maloyb7d42632017-10-13 11:04:26 +0200393}
394
Jon Maloy2f487712017-10-13 11:04:31 +0200395void tipc_group_update_bc_members(struct tipc_group *grp, int len, bool ack)
Jon Maloyb7d42632017-10-13 11:04:26 +0200396{
Jon Maloy2f487712017-10-13 11:04:31 +0200397 u16 prev = grp->bc_snd_nxt - 1;
Jon Maloyb7d42632017-10-13 11:04:26 +0200398 struct tipc_member *m;
399 struct rb_node *n;
Jon Maloy0a3d8052017-12-21 13:07:11 +0100400 u16 ackers = 0;
Jon Maloyb7d42632017-10-13 11:04:26 +0200401
402 for (n = rb_first(&grp->members); n; n = rb_next(n)) {
403 m = container_of(n, struct tipc_member, tree_node);
Jon Maloy38266ca2018-01-04 15:20:44 +0100404 if (tipc_group_is_receiver(m)) {
Jon Maloyb7d42632017-10-13 11:04:26 +0200405 tipc_group_update_member(m, len);
Jon Maloy2f487712017-10-13 11:04:31 +0200406 m->bc_acked = prev;
Jon Maloy0a3d8052017-12-21 13:07:11 +0100407 ackers++;
Jon Maloy2f487712017-10-13 11:04:31 +0200408 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200409 }
Jon Maloy2f487712017-10-13 11:04:31 +0200410
411 /* Mark number of acknowledges to expect, if any */
412 if (ack)
Jon Maloy0a3d8052017-12-21 13:07:11 +0100413 grp->bc_ackers = ackers;
Jon Maloy75da2162017-10-13 11:04:23 +0200414 grp->bc_snd_nxt++;
415}
416
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200417bool tipc_group_cong(struct tipc_group *grp, u32 dnode, u32 dport,
418 int len, struct tipc_member **mbr)
419{
420 struct sk_buff_head xmitq;
421 struct tipc_member *m;
422 int adv, state;
423
424 m = tipc_group_find_dest(grp, dnode, dport);
Jon Maloyeb929a92018-01-08 21:03:31 +0100425 if (!tipc_group_is_receiver(m)) {
426 *mbr = NULL;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200427 return false;
Jon Maloyeb929a92018-01-08 21:03:31 +0100428 }
429 *mbr = m;
430
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200431 if (m->window >= len)
432 return false;
Jon Maloyeb929a92018-01-08 21:03:31 +0100433
Jon Maloy60c25302018-01-17 16:42:46 +0100434 *grp->open = false;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200435
436 /* If not fully advertised, do it now to prevent mutual blocking */
437 adv = m->advertised;
438 state = m->state;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200439 if (state == MBR_JOINED && adv == ADV_IDLE)
440 return true;
Jon Maloy04d7b572017-10-13 11:04:34 +0200441 if (state == MBR_ACTIVE && adv == ADV_ACTIVE)
442 return true;
443 if (state == MBR_PENDING && adv == ADV_IDLE)
444 return true;
Jon Maloye654f9f2019-08-15 16:42:50 +0200445 __skb_queue_head_init(&xmitq);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200446 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, &xmitq);
447 tipc_node_distr_xmit(grp->net, &xmitq);
448 return true;
449}
450
Jon Maloyb7d42632017-10-13 11:04:26 +0200451bool tipc_group_bc_cong(struct tipc_group *grp, int len)
452{
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200453 struct tipc_member *m = NULL;
Jon Maloyb7d42632017-10-13 11:04:26 +0200454
Jon Maloy2f487712017-10-13 11:04:31 +0200455 /* If prev bcast was replicast, reject until all receivers have acked */
Jon Maloyeb929a92018-01-08 21:03:31 +0100456 if (grp->bc_ackers) {
Jon Maloy60c25302018-01-17 16:42:46 +0100457 *grp->open = false;
Jon Maloy2f487712017-10-13 11:04:31 +0200458 return true;
Jon Maloyeb929a92018-01-08 21:03:31 +0100459 }
Jon Maloy38266ca2018-01-04 15:20:44 +0100460 if (list_empty(&grp->small_win))
Jon Maloyb7d42632017-10-13 11:04:26 +0200461 return false;
462
Jon Maloy38266ca2018-01-04 15:20:44 +0100463 m = list_first_entry(&grp->small_win, struct tipc_member, small_win);
Jon Maloyb7d42632017-10-13 11:04:26 +0200464 if (m->window >= len)
465 return false;
466
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200467 return tipc_group_cong(grp, m->node, m->port, len, &m);
Jon Maloyb7d42632017-10-13 11:04:26 +0200468}
469
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200470/* 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
Jon Maloya3bada72017-10-13 11:04:33 +0200479 /* Bcast/mcast may be bypassed by ucast or other bcast, - sort it in */
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200480 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
Jon Maloy75da2162017-10-13 11:04:23 +0200494/* 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);
Jon Maloya3bada72017-10-13 11:04:33 +0200500 bool ack, deliver, update, leave = false;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200501 struct sk_buff_head *defq;
Jon Maloy75da2162017-10-13 11:04:23 +0200502 struct tipc_member *m;
503 struct tipc_msg *hdr;
504 u32 node, port;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200505 int mtyp, blks;
Jon Maloy75da2162017-10-13 11:04:23 +0200506
507 if (!skb)
508 return;
509
510 hdr = buf_msg(skb);
Jon Maloy75da2162017-10-13 11:04:23 +0200511 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);
Jon Maloy38266ca2018-01-04 15:20:44 +0100518 if (!tipc_group_is_sender(m))
Jon Maloy75da2162017-10-13 11:04:23 +0200519 goto drop;
520
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200521 if (less(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt))
522 goto drop;
Jon Maloy5b8dddb2017-10-13 11:04:29 +0200523
Jon Maloy31c82a22017-10-13 11:04:24 +0200524 TIPC_SKB_CB(skb)->orig_member = m->instance;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200525 defq = &m->deferredq;
526 tipc_group_sort_msg(skb, defq);
Jon Maloy75da2162017-10-13 11:04:23 +0200527
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200528 while ((skb = skb_peek(defq))) {
529 hdr = buf_msg(skb);
530 mtyp = msg_type(hdr);
Jon Maloy2e724dc2017-11-27 20:13:39 +0100531 blks = msg_blocks(hdr);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200532 deliver = true;
Jon Maloy2f487712017-10-13 11:04:31 +0200533 ack = false;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200534 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 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500546 fallthrough;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200547 case TIPC_GRP_BCAST_MSG:
548 m->bc_rcv_nxt++;
Jon Maloy2f487712017-10-13 11:04:31 +0200549 ack = msg_grp_bc_ack_req(hdr);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200550 break;
551 case TIPC_GRP_UCAST_MSG:
552 break;
Jon Maloya3bada72017-10-13 11:04:33 +0200553 case TIPC_GRP_MEMBER_EVT:
554 if (m->state == MBR_LEAVING)
555 leave = true;
556 if (!grp->events)
557 deliver = false;
558 break;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200559 default:
560 break;
561 }
562
563 /* Execute decisions */
564 __skb_dequeue(defq);
565 if (deliver)
566 __skb_queue_tail(inputq, skb);
567 else
568 kfree_skb(skb);
569
Jon Maloy2f487712017-10-13 11:04:31 +0200570 if (ack)
571 tipc_group_proto_xmit(grp, m, GRP_ACK_MSG, xmitq);
572
Jon Maloya3bada72017-10-13 11:04:33 +0200573 if (leave) {
Jon Maloya3bada72017-10-13 11:04:33 +0200574 __skb_queue_purge(defq);
Jon Maloye0e853a2017-11-20 21:43:03 +0100575 tipc_group_delete_member(grp, m);
Jon Maloya3bada72017-10-13 11:04:33 +0200576 break;
577 }
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200578 if (!update)
579 continue;
580
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200581 tipc_group_update_rcv_win(grp, blks, node, port, xmitq);
582 }
Jon Maloy75da2162017-10-13 11:04:23 +0200583 return;
584drop:
585 kfree_skb(skb);
586}
587
Jon Maloyb7d42632017-10-13 11:04:26 +0200588void tipc_group_update_rcv_win(struct tipc_group *grp, int blks, u32 node,
589 u32 port, struct sk_buff_head *xmitq)
590{
Jon Maloy04d7b572017-10-13 11:04:34 +0200591 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;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100595 struct tipc_member *m, *rm, *pm;
Jon Maloyb7d42632017-10-13 11:04:26 +0200596
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:
Jon Maloy4ea5dab2018-01-08 21:03:24 +0100605 /* 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)) {
Jon Maloy04d7b572017-10-13 11:04:34 +0200621 rm = list_first_entry(active, struct tipc_member, list);
622 rm->state = MBR_RECLAIMING;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100623 list_del_init(&rm->list);
Jon Maloy04d7b572017-10-13 11:04:34 +0200624 tipc_group_proto_xmit(grp, rm, GRP_RECLAIM_MSG, xmitq);
Jon Maloy04d7b572017-10-13 11:04:34 +0200625 break;
626 }
Jon Maloy4ea5dab2018-01-08 21:03:24 +0100627 /* 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;
Jon Maloy04d7b572017-10-13 11:04:34 +0200633 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);
Jon Maloyb7d42632017-10-13 11:04:26 +0200639 break;
Jon Maloy04d7b572017-10-13 11:04:34 +0200640 case MBR_REMITTED:
641 if (m->advertised > ADV_IDLE)
642 break;
643 m->state = MBR_JOINED;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100644 grp->active_cnt--;
Jon Maloy04d7b572017-10-13 11:04:34 +0200645 if (m->advertised < ADV_IDLE) {
646 pr_warn_ratelimited("Rcv unexpected msg after REMIT\n");
647 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
648 }
Jon Maloy8d5dee22018-01-08 21:03:23 +0100649
Jon Maloyf9c935d2017-12-29 19:48:02 +0100650 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);
Jon Maloy04d7b572017-10-13 11:04:34 +0200659 break;
660 case MBR_RECLAIMING:
Jon Maloyb7d42632017-10-13 11:04:26 +0200661 case MBR_JOINING:
662 case MBR_LEAVING:
663 default:
664 break;
665 }
666}
667
Jon Maloy7ad32bc2018-01-08 21:03:26 +0100668static 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
Jon Maloyb06f9d92018-10-18 17:38:29 +0200677 memset(&evt, 0, sizeof(evt));
Jon Maloy7ad32bc2018-01-08 21:03:26 +0100678 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
Jon Maloy75da2162017-10-13 11:04:23 +0200703static 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;
Jon Maloyb7d42632017-10-13 11:04:26 +0200708 int adv = 0;
Jon Maloy75da2162017-10-13 11:04:23 +0200709
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
Jon Maloy04d7b572017-10-13 11:04:34 +0200716 if (m->state == MBR_ACTIVE)
Jon Maloyb7d42632017-10-13 11:04:26 +0200717 adv = ADV_ACTIVE - m->advertised;
Jon Maloy04d7b572017-10-13 11:04:34 +0200718 else if (m->state == MBR_JOINED || m->state == MBR_PENDING)
719 adv = ADV_IDLE - m->advertised;
Jon Maloyb7d42632017-10-13 11:04:26 +0200720
Jon Maloy75da2162017-10-13 11:04:23 +0200721 hdr = buf_msg(skb);
Jon Maloyb7d42632017-10-13 11:04:26 +0200722
723 if (mtyp == GRP_JOIN_MSG) {
Jon Maloy75da2162017-10-13 11:04:23 +0200724 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200725 msg_set_adv_win(hdr, adv);
726 m->advertised += adv;
Jon Maloya3bada72017-10-13 11:04:33 +0200727 } else if (mtyp == GRP_LEAVE_MSG) {
728 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200729 } else if (mtyp == GRP_ADV_MSG) {
730 msg_set_adv_win(hdr, adv);
731 m->advertised += adv;
Jon Maloy2f487712017-10-13 11:04:31 +0200732 } else if (mtyp == GRP_ACK_MSG) {
733 msg_set_grp_bc_acked(hdr, m->bc_rcv_nxt);
Jon Maloy04d7b572017-10-13 11:04:34 +0200734 } else if (mtyp == GRP_REMIT_MSG) {
735 msg_set_grp_remitted(hdr, m->window);
Jon Maloyb7d42632017-10-13 11:04:26 +0200736 }
Jon Maloy23483392017-12-18 17:34:16 +0100737 msg_set_dest_droppable(hdr, true);
Jon Maloy75da2162017-10-13 11:04:23 +0200738 __skb_queue_tail(xmitq, skb);
739}
740
Jon Maloyb7d42632017-10-13 11:04:26 +0200741void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup,
742 struct tipc_msg *hdr, struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200743 struct sk_buff_head *xmitq)
744{
745 u32 node = msg_orignode(hdr);
746 u32 port = msg_origport(hdr);
Jon Maloy04d7b572017-10-13 11:04:34 +0200747 struct tipc_member *m, *pm;
Jon Maloy04d7b572017-10-13 11:04:34 +0200748 u16 remitted, in_flight;
Jon Maloy75da2162017-10-13 11:04:23 +0200749
750 if (!grp)
751 return;
752
Jon Maloy232d07b2018-01-08 21:03:30 +0100753 if (grp->scope == TIPC_NODE_SCOPE && node != tipc_own_addr(grp->net))
754 return;
755
Jon Maloy75da2162017-10-13 11:04:23 +0200756 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,
Jon Maloyd12d2e12018-01-08 21:03:28 +0100762 0, MBR_JOINING);
Jon Maloy75da2162017-10-13 11:04:23 +0200763 if (!m)
764 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200765 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
766 m->bc_rcv_nxt = m->bc_syncpt;
Jon Maloyb7d42632017-10-13 11:04:26 +0200767 m->window += msg_adv_win(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +0200768
Jon Maloyd12d2e12018-01-08 21:03:28 +0100769 /* 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;
Jon Maloyeb929a92018-01-08 21:03:31 +0100775 tipc_group_open(m, usr_wakeup);
Jon Maloybb25c382017-12-20 11:03:15 +0100776 tipc_group_update_member(m, 0);
Jon Maloyd12d2e12018-01-08 21:03:28 +0100777 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
778 tipc_group_create_event(grp, m, TIPC_PUBLISHED,
779 m->bc_syncpt, inputq);
Jon Maloy75da2162017-10-13 11:04:23 +0200780 return;
781 case GRP_LEAVE_MSG:
782 if (!m)
783 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200784 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100785 list_del_init(&m->list);
Jon Maloyeb929a92018-01-08 21:03:31 +0100786 tipc_group_open(m, usr_wakeup);
Jon Maloyc2b22bc2018-01-08 21:03:27 +0100787 tipc_group_decr_active(grp, m);
788 m->state = MBR_LEAVING;
Jon Maloy7ad32bc2018-01-08 21:03:26 +0100789 tipc_group_create_event(grp, m, TIPC_WITHDRAWN,
790 m->bc_syncpt, inputq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200791 return;
792 case GRP_ADV_MSG:
793 if (!m)
794 return;
795 m->window += msg_adv_win(hdr);
Jon Maloyeb929a92018-01-08 21:03:31 +0100796 tipc_group_open(m, usr_wakeup);
Jon Maloy75da2162017-10-13 11:04:23 +0200797 return;
Jon Maloy2f487712017-10-13 11:04:31 +0200798 case GRP_ACK_MSG:
799 if (!m)
800 return;
801 m->bc_acked = msg_grp_bc_acked(hdr);
802 if (--grp->bc_ackers)
Jon Maloyeb929a92018-01-08 21:03:31 +0100803 return;
804 list_del_init(&m->small_win);
Jon Maloy60c25302018-01-17 16:42:46 +0100805 *m->group->open = true;
Jon Maloy2f487712017-10-13 11:04:31 +0200806 *usr_wakeup = true;
Jon Maloyeb929a92018-01-08 21:03:31 +0100807 tipc_group_update_member(m, 0);
Jon Maloy2f487712017-10-13 11:04:31 +0200808 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200809 case GRP_RECLAIM_MSG:
810 if (!m)
811 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200812 tipc_group_proto_xmit(grp, m, GRP_REMIT_MSG, xmitq);
813 m->window = ADV_IDLE;
Jon Maloyeb929a92018-01-08 21:03:31 +0100814 tipc_group_open(m, usr_wakeup);
Jon Maloy04d7b572017-10-13 11:04:34 +0200815 return;
816 case GRP_REMIT_MSG:
817 if (!m || m->state != MBR_RECLAIMING)
818 return;
819
Jon Maloy04d7b572017-10-13 11:04:34 +0200820 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;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100826 m->advertised = ADV_IDLE + in_flight;
827 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200828 }
Jon Maloy8d5dee22018-01-08 21:03:23 +0100829 /* This should never happen */
Jon Maloy04d7b572017-10-13 11:04:34 +0200830 if (m->advertised < remitted)
Jon Maloy8d5dee22018-01-08 21:03:23 +0100831 pr_warn_ratelimited("Unexpected REMIT msg\n");
Jon Maloy04d7b572017-10-13 11:04:34 +0200832
Jon Maloy8d5dee22018-01-08 21:03:23 +0100833 /* All messages preceding the REMIT have been read */
834 m->state = MBR_JOINED;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100835 grp->active_cnt--;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100836 m->advertised = ADV_IDLE;
Jon Maloy04d7b572017-10-13 11:04:34 +0200837
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;
Jon Maloy75da2162017-10-13 11:04:23 +0200848 default:
849 pr_warn("Received unknown GROUP_PROTO message\n");
850 }
851}
852
Jon Maloyb7d42632017-10-13 11:04:26 +0200853/* tipc_group_member_evt() - receive and handle a member up/down event
854 */
Jon Maloy75da2162017-10-13 11:04:23 +0200855void tipc_group_member_evt(struct tipc_group *grp,
Jon Maloyb7d42632017-10-13 11:04:26 +0200856 bool *usr_wakeup,
857 int *sk_rcvbuf,
Jon Maloy7ad32bc2018-01-08 21:03:26 +0100858 struct tipc_msg *hdr,
Jon Maloyae236fb2017-10-13 11:04:25 +0200859 struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200860 struct sk_buff_head *xmitq)
861{
Jon Maloy75da2162017-10-13 11:04:23 +0200862 struct tipc_event *evt = (void *)msg_data(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +0200863 u32 instance = evt->found_lower;
Jon Maloy75da2162017-10-13 11:04:23 +0200864 u32 node = evt->port.node;
865 u32 port = evt->port.ref;
Jon Maloyae236fb2017-10-13 11:04:25 +0200866 int event = evt->event;
Jon Maloy75da2162017-10-13 11:04:23 +0200867 struct tipc_member *m;
868 struct net *net;
869 u32 self;
870
871 if (!grp)
Jon Maloy7ad32bc2018-01-08 21:03:26 +0100872 return;
Jon Maloy75da2162017-10-13 11:04:23 +0200873
874 net = grp->net;
875 self = tipc_own_addr(net);
876 if (!grp->loopback && node == self && port == grp->portid)
Jon Maloy7ad32bc2018-01-08 21:03:26 +0100877 return;
Jon Maloyae236fb2017-10-13 11:04:25 +0200878
Jon Maloy75da2162017-10-13 11:04:23 +0200879 m = tipc_group_find_member(grp, node, port);
880
Jon Maloyd12d2e12018-01-08 21:03:28 +0100881 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;
Jon Maloyae236fb2017-10-13 11:04:25 +0200892 }
Jon Maloyd12d2e12018-01-08 21:03:28 +0100893
894 if (m->state != MBR_JOINING)
895 break;
896
897 /* Member can be taken into service */
898 m->instance = instance;
899 m->state = MBR_JOINED;
Jon Maloyeb929a92018-01-08 21:03:31 +0100900 tipc_group_open(m, usr_wakeup);
Jon Maloyd84d1b32018-01-04 15:20:45 +0100901 tipc_group_update_member(m, 0);
Jon Maloyd12d2e12018-01-08 21:03:28 +0100902 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:
Jon Maloy75da2162017-10-13 11:04:23 +0200907 if (!m)
Jon Maloyd12d2e12018-01-08 21:03:28 +0100908 break;
Jon Maloyae236fb2017-10-13 11:04:25 +0200909
Jon Maloyc2b22bc2018-01-08 21:03:27 +0100910 tipc_group_decr_active(grp, m);
911 m->state = MBR_LEAVING;
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100912 list_del_init(&m->list);
Jon Maloyeb929a92018-01-08 21:03:31 +0100913 tipc_group_open(m, usr_wakeup);
Jon Maloyc2b22bc2018-01-08 21:03:27 +0100914
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);
Jon Maloyd12d2e12018-01-08 21:03:28 +0100919 break;
920 default:
921 break;
Jon Maloy75da2162017-10-13 11:04:23 +0200922 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200923 *sk_rcvbuf = tipc_group_rcvbuf_limit(grp);
Jon Maloy75da2162017-10-13 11:04:23 +0200924}
GhantaKrishnamurthy MohanKrishnaa1be5a22018-06-29 13:26:18 +0200925
926int tipc_group_fill_sock_diag(struct tipc_group *grp, struct sk_buff *skb)
927{
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200928 struct nlattr *group = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_GROUP);
GhantaKrishnamurthy MohanKrishnaa1be5a22018-06-29 13:26:18 +0200929
Kangjie Lu4589e282019-03-15 12:11:59 -0500930 if (!group)
931 return -EMSGSIZE;
932
GhantaKrishnamurthy MohanKrishnaa1be5a22018-06-29 13:26:18 +0200933 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}