blob: 12777cac638a6918a626953647a1bf803ced89ad [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
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
36#include "core.h"
37#include "addr.h"
38#include "group.h"
39#include "bcast.h"
40#include "server.h"
41#include "msg.h"
42#include "socket.h"
43#include "node.h"
44#include "name_table.h"
45#include "subscr.h"
46
47#define ADV_UNIT (((MAX_MSG_SIZE + MAX_H_SIZE) / FLOWCTL_BLK_SZ) + 1)
48#define ADV_IDLE ADV_UNIT
Jon Maloyb7d42632017-10-13 11:04:26 +020049#define ADV_ACTIVE (ADV_UNIT * 12)
Jon Maloy75da2162017-10-13 11:04:23 +020050
51enum mbr_state {
52 MBR_QUARANTINED,
53 MBR_DISCOVERED,
54 MBR_JOINING,
55 MBR_PUBLISHED,
56 MBR_JOINED,
Jon Maloy04d7b572017-10-13 11:04:34 +020057 MBR_PENDING,
58 MBR_ACTIVE,
59 MBR_RECLAIMING,
60 MBR_REMITTED,
Jon Maloy75da2162017-10-13 11:04:23 +020061 MBR_LEAVING
62};
63
64struct tipc_member {
65 struct rb_node tree_node;
66 struct list_head list;
Jon Maloyb7d42632017-10-13 11:04:26 +020067 struct list_head congested;
Jon Maloyae236fb2017-10-13 11:04:25 +020068 struct sk_buff *event_msg;
Jon Maloyb87a5ea2017-10-13 11:04:30 +020069 struct sk_buff_head deferredq;
Jon Maloyb7d42632017-10-13 11:04:26 +020070 struct tipc_group *group;
Jon Maloy75da2162017-10-13 11:04:23 +020071 u32 node;
72 u32 port;
Jon Maloy31c82a22017-10-13 11:04:24 +020073 u32 instance;
Jon Maloy75da2162017-10-13 11:04:23 +020074 enum mbr_state state;
Jon Maloyb7d42632017-10-13 11:04:26 +020075 u16 advertised;
76 u16 window;
Jon Maloy75da2162017-10-13 11:04:23 +020077 u16 bc_rcv_nxt;
Jon Maloya3bada72017-10-13 11:04:33 +020078 u16 bc_syncpt;
Jon Maloy2f487712017-10-13 11:04:31 +020079 u16 bc_acked;
Jon Maloyb7d42632017-10-13 11:04:26 +020080 bool usr_pending;
Jon Maloy75da2162017-10-13 11:04:23 +020081};
82
83struct tipc_group {
84 struct rb_root members;
Jon Maloyb7d42632017-10-13 11:04:26 +020085 struct list_head congested;
Jon Maloy04d7b572017-10-13 11:04:34 +020086 struct list_head pending;
87 struct list_head active;
88 struct list_head reclaiming;
Jon Maloy75da2162017-10-13 11:04:23 +020089 struct tipc_nlist dests;
90 struct net *net;
91 int subid;
92 u32 type;
93 u32 instance;
94 u32 domain;
95 u32 scope;
96 u32 portid;
97 u16 member_cnt;
Jon Maloy04d7b572017-10-13 11:04:34 +020098 u16 active_cnt;
99 u16 max_active;
Jon Maloy75da2162017-10-13 11:04:23 +0200100 u16 bc_snd_nxt;
Jon Maloy2f487712017-10-13 11:04:31 +0200101 u16 bc_ackers;
Jon Maloy75da2162017-10-13 11:04:23 +0200102 bool loopback;
Jon Maloyae236fb2017-10-13 11:04:25 +0200103 bool events;
Jon Maloy75da2162017-10-13 11:04:23 +0200104};
105
106static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
107 int mtyp, struct sk_buff_head *xmitq);
108
Jon Maloy04d7b572017-10-13 11:04:34 +0200109static void tipc_group_decr_active(struct tipc_group *grp,
110 struct tipc_member *m)
111{
112 if (m->state == MBR_ACTIVE || m->state == MBR_RECLAIMING)
113 grp->active_cnt--;
114}
115
Jon Maloyb7d42632017-10-13 11:04:26 +0200116static int tipc_group_rcvbuf_limit(struct tipc_group *grp)
117{
Jon Maloy04d7b572017-10-13 11:04:34 +0200118 int max_active, active_pool, idle_pool;
Jon Maloyb7d42632017-10-13 11:04:26 +0200119 int mcnt = grp->member_cnt + 1;
120
Jon Maloy04d7b572017-10-13 11:04:34 +0200121 /* Limit simultaneous reception from other members */
122 max_active = min(mcnt / 8, 64);
123 max_active = max(max_active, 16);
124 grp->max_active = max_active;
125
126 /* Reserve blocks for active and idle members */
127 active_pool = max_active * ADV_ACTIVE;
128 idle_pool = (mcnt - max_active) * ADV_IDLE;
129
Jon Maloyb7d42632017-10-13 11:04:26 +0200130 /* Scale to bytes, considering worst-case truesize/msgsize ratio */
Jon Maloy04d7b572017-10-13 11:04:34 +0200131 return (active_pool + idle_pool) * FLOWCTL_BLK_SZ * 4;
Jon Maloyb7d42632017-10-13 11:04:26 +0200132}
133
Jon Maloy75da2162017-10-13 11:04:23 +0200134u16 tipc_group_bc_snd_nxt(struct tipc_group *grp)
135{
136 return grp->bc_snd_nxt;
137}
138
Jon Maloyb7d42632017-10-13 11:04:26 +0200139static bool tipc_group_is_enabled(struct tipc_member *m)
140{
141 return m->state != MBR_QUARANTINED && m->state != MBR_LEAVING;
142}
143
Jon Maloy75da2162017-10-13 11:04:23 +0200144static bool tipc_group_is_receiver(struct tipc_member *m)
145{
146 return m && m->state >= MBR_JOINED;
147}
148
Jon Maloyee106d72017-10-13 11:04:28 +0200149u32 tipc_group_exclude(struct tipc_group *grp)
150{
151 if (!grp->loopback)
152 return grp->portid;
153 return 0;
154}
155
Jon Maloy75da2162017-10-13 11:04:23 +0200156int tipc_group_size(struct tipc_group *grp)
157{
158 return grp->member_cnt;
159}
160
161struct tipc_group *tipc_group_create(struct net *net, u32 portid,
162 struct tipc_group_req *mreq)
163{
164 struct tipc_group *grp;
165 u32 type = mreq->type;
166
167 grp = kzalloc(sizeof(*grp), GFP_ATOMIC);
168 if (!grp)
169 return NULL;
170 tipc_nlist_init(&grp->dests, tipc_own_addr(net));
Jon Maloyb7d42632017-10-13 11:04:26 +0200171 INIT_LIST_HEAD(&grp->congested);
Jon Maloy04d7b572017-10-13 11:04:34 +0200172 INIT_LIST_HEAD(&grp->active);
173 INIT_LIST_HEAD(&grp->pending);
174 INIT_LIST_HEAD(&grp->reclaiming);
Jon Maloy75da2162017-10-13 11:04:23 +0200175 grp->members = RB_ROOT;
176 grp->net = net;
177 grp->portid = portid;
178 grp->domain = addr_domain(net, mreq->scope);
179 grp->type = type;
180 grp->instance = mreq->instance;
181 grp->scope = mreq->scope;
182 grp->loopback = mreq->flags & TIPC_GROUP_LOOPBACK;
Jon Maloyae236fb2017-10-13 11:04:25 +0200183 grp->events = mreq->flags & TIPC_GROUP_MEMBER_EVTS;
Jon Maloy75da2162017-10-13 11:04:23 +0200184 if (tipc_topsrv_kern_subscr(net, portid, type, 0, ~0, &grp->subid))
185 return grp;
186 kfree(grp);
187 return NULL;
188}
189
190void tipc_group_delete(struct net *net, struct tipc_group *grp)
191{
192 struct rb_root *tree = &grp->members;
193 struct tipc_member *m, *tmp;
194 struct sk_buff_head xmitq;
195
196 __skb_queue_head_init(&xmitq);
197
198 rbtree_postorder_for_each_entry_safe(m, tmp, tree, tree_node) {
199 tipc_group_proto_xmit(grp, m, GRP_LEAVE_MSG, &xmitq);
200 list_del(&m->list);
201 kfree(m);
202 }
203 tipc_node_distr_xmit(net, &xmitq);
204 tipc_nlist_purge(&grp->dests);
205 tipc_topsrv_kern_unsubscr(net, grp->subid);
206 kfree(grp);
207}
208
209struct tipc_member *tipc_group_find_member(struct tipc_group *grp,
210 u32 node, u32 port)
211{
212 struct rb_node *n = grp->members.rb_node;
213 u64 nkey, key = (u64)node << 32 | port;
214 struct tipc_member *m;
215
216 while (n) {
217 m = container_of(n, struct tipc_member, tree_node);
218 nkey = (u64)m->node << 32 | m->port;
219 if (key < nkey)
220 n = n->rb_left;
221 else if (key > nkey)
222 n = n->rb_right;
223 else
224 return m;
225 }
226 return NULL;
227}
228
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200229static struct tipc_member *tipc_group_find_dest(struct tipc_group *grp,
230 u32 node, u32 port)
231{
232 struct tipc_member *m;
233
234 m = tipc_group_find_member(grp, node, port);
235 if (m && tipc_group_is_enabled(m))
236 return m;
237 return NULL;
238}
239
Jon Maloy75da2162017-10-13 11:04:23 +0200240static struct tipc_member *tipc_group_find_node(struct tipc_group *grp,
241 u32 node)
242{
243 struct tipc_member *m;
244 struct rb_node *n;
245
246 for (n = rb_first(&grp->members); n; n = rb_next(n)) {
247 m = container_of(n, struct tipc_member, tree_node);
248 if (m->node == node)
249 return m;
250 }
251 return NULL;
252}
253
254static void tipc_group_add_to_tree(struct tipc_group *grp,
255 struct tipc_member *m)
256{
257 u64 nkey, key = (u64)m->node << 32 | m->port;
258 struct rb_node **n, *parent = NULL;
259 struct tipc_member *tmp;
260
261 n = &grp->members.rb_node;
262 while (*n) {
263 tmp = container_of(*n, struct tipc_member, tree_node);
264 parent = *n;
265 tmp = container_of(parent, struct tipc_member, tree_node);
266 nkey = (u64)tmp->node << 32 | tmp->port;
267 if (key < nkey)
268 n = &(*n)->rb_left;
269 else if (key > nkey)
270 n = &(*n)->rb_right;
271 else
272 return;
273 }
274 rb_link_node(&m->tree_node, parent, n);
275 rb_insert_color(&m->tree_node, &grp->members);
276}
277
278static struct tipc_member *tipc_group_create_member(struct tipc_group *grp,
279 u32 node, u32 port,
280 int state)
281{
282 struct tipc_member *m;
283
284 m = kzalloc(sizeof(*m), GFP_ATOMIC);
285 if (!m)
286 return NULL;
287 INIT_LIST_HEAD(&m->list);
Jon Maloyb7d42632017-10-13 11:04:26 +0200288 INIT_LIST_HEAD(&m->congested);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200289 __skb_queue_head_init(&m->deferredq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200290 m->group = grp;
Jon Maloy75da2162017-10-13 11:04:23 +0200291 m->node = node;
292 m->port = port;
Jon Maloy2f487712017-10-13 11:04:31 +0200293 m->bc_acked = grp->bc_snd_nxt - 1;
Jon Maloy75da2162017-10-13 11:04:23 +0200294 grp->member_cnt++;
295 tipc_group_add_to_tree(grp, m);
296 tipc_nlist_add(&grp->dests, m->node);
297 m->state = state;
298 return m;
299}
300
301void tipc_group_add_member(struct tipc_group *grp, u32 node, u32 port)
302{
303 tipc_group_create_member(grp, node, port, MBR_DISCOVERED);
304}
305
306static void tipc_group_delete_member(struct tipc_group *grp,
307 struct tipc_member *m)
308{
309 rb_erase(&m->tree_node, &grp->members);
310 grp->member_cnt--;
Jon Maloy2f487712017-10-13 11:04:31 +0200311
312 /* Check if we were waiting for replicast ack from this member */
313 if (grp->bc_ackers && less(m->bc_acked, grp->bc_snd_nxt - 1))
314 grp->bc_ackers--;
315
Jon Maloy75da2162017-10-13 11:04:23 +0200316 list_del_init(&m->list);
Jon Maloyb7d42632017-10-13 11:04:26 +0200317 list_del_init(&m->congested);
Jon Maloy04d7b572017-10-13 11:04:34 +0200318 tipc_group_decr_active(grp, m);
Jon Maloy75da2162017-10-13 11:04:23 +0200319
320 /* If last member on a node, remove node from dest list */
321 if (!tipc_group_find_node(grp, m->node))
322 tipc_nlist_del(&grp->dests, m->node);
323
324 kfree(m);
325}
326
327struct tipc_nlist *tipc_group_dests(struct tipc_group *grp)
328{
329 return &grp->dests;
330}
331
332void tipc_group_self(struct tipc_group *grp, struct tipc_name_seq *seq,
333 int *scope)
334{
335 seq->type = grp->type;
336 seq->lower = grp->instance;
337 seq->upper = grp->instance;
338 *scope = grp->scope;
339}
340
Jon Maloyb7d42632017-10-13 11:04:26 +0200341void tipc_group_update_member(struct tipc_member *m, int len)
Jon Maloy75da2162017-10-13 11:04:23 +0200342{
Jon Maloyb7d42632017-10-13 11:04:26 +0200343 struct tipc_group *grp = m->group;
344 struct tipc_member *_m, *tmp;
345
346 if (!tipc_group_is_enabled(m))
347 return;
348
349 m->window -= len;
350
351 if (m->window >= ADV_IDLE)
352 return;
353
354 if (!list_empty(&m->congested))
355 return;
356
357 /* Sort member into congested members' list */
358 list_for_each_entry_safe(_m, tmp, &grp->congested, congested) {
359 if (m->window > _m->window)
360 continue;
361 list_add_tail(&m->congested, &_m->congested);
362 return;
363 }
364 list_add_tail(&m->congested, &grp->congested);
365}
366
Jon Maloy2f487712017-10-13 11:04:31 +0200367void tipc_group_update_bc_members(struct tipc_group *grp, int len, bool ack)
Jon Maloyb7d42632017-10-13 11:04:26 +0200368{
Jon Maloy2f487712017-10-13 11:04:31 +0200369 u16 prev = grp->bc_snd_nxt - 1;
Jon Maloyb7d42632017-10-13 11:04:26 +0200370 struct tipc_member *m;
371 struct rb_node *n;
372
373 for (n = rb_first(&grp->members); n; n = rb_next(n)) {
374 m = container_of(n, struct tipc_member, tree_node);
Jon Maloy2f487712017-10-13 11:04:31 +0200375 if (tipc_group_is_enabled(m)) {
Jon Maloyb7d42632017-10-13 11:04:26 +0200376 tipc_group_update_member(m, len);
Jon Maloy2f487712017-10-13 11:04:31 +0200377 m->bc_acked = prev;
378 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200379 }
Jon Maloy2f487712017-10-13 11:04:31 +0200380
381 /* Mark number of acknowledges to expect, if any */
382 if (ack)
383 grp->bc_ackers = grp->member_cnt;
Jon Maloy75da2162017-10-13 11:04:23 +0200384 grp->bc_snd_nxt++;
385}
386
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200387bool tipc_group_cong(struct tipc_group *grp, u32 dnode, u32 dport,
388 int len, struct tipc_member **mbr)
389{
390 struct sk_buff_head xmitq;
391 struct tipc_member *m;
392 int adv, state;
393
394 m = tipc_group_find_dest(grp, dnode, dport);
395 *mbr = m;
396 if (!m)
397 return false;
398 if (m->usr_pending)
399 return true;
400 if (m->window >= len)
401 return false;
402 m->usr_pending = true;
403
404 /* If not fully advertised, do it now to prevent mutual blocking */
405 adv = m->advertised;
406 state = m->state;
407 if (state < MBR_JOINED)
408 return true;
409 if (state == MBR_JOINED && adv == ADV_IDLE)
410 return true;
Jon Maloy04d7b572017-10-13 11:04:34 +0200411 if (state == MBR_ACTIVE && adv == ADV_ACTIVE)
412 return true;
413 if (state == MBR_PENDING && adv == ADV_IDLE)
414 return true;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200415 skb_queue_head_init(&xmitq);
416 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, &xmitq);
417 tipc_node_distr_xmit(grp->net, &xmitq);
418 return true;
419}
420
Jon Maloyb7d42632017-10-13 11:04:26 +0200421bool tipc_group_bc_cong(struct tipc_group *grp, int len)
422{
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200423 struct tipc_member *m = NULL;
Jon Maloyb7d42632017-10-13 11:04:26 +0200424
Jon Maloy2f487712017-10-13 11:04:31 +0200425 /* If prev bcast was replicast, reject until all receivers have acked */
426 if (grp->bc_ackers)
427 return true;
428
Jon Maloyb7d42632017-10-13 11:04:26 +0200429 if (list_empty(&grp->congested))
430 return false;
431
432 m = list_first_entry(&grp->congested, struct tipc_member, congested);
433 if (m->window >= len)
434 return false;
435
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200436 return tipc_group_cong(grp, m->node, m->port, len, &m);
Jon Maloyb7d42632017-10-13 11:04:26 +0200437}
438
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200439/* tipc_group_sort_msg() - sort msg into queue by bcast sequence number
440 */
441static void tipc_group_sort_msg(struct sk_buff *skb, struct sk_buff_head *defq)
442{
443 struct tipc_msg *_hdr, *hdr = buf_msg(skb);
444 u16 bc_seqno = msg_grp_bc_seqno(hdr);
445 struct sk_buff *_skb, *tmp;
446 int mtyp = msg_type(hdr);
447
Jon Maloya3bada72017-10-13 11:04:33 +0200448 /* Bcast/mcast may be bypassed by ucast or other bcast, - sort it in */
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200449 if (mtyp == TIPC_GRP_BCAST_MSG || mtyp == TIPC_GRP_MCAST_MSG) {
450 skb_queue_walk_safe(defq, _skb, tmp) {
451 _hdr = buf_msg(_skb);
452 if (!less(bc_seqno, msg_grp_bc_seqno(_hdr)))
453 continue;
454 __skb_queue_before(defq, _skb, skb);
455 return;
456 }
457 /* Bcast was not bypassed, - add to tail */
458 }
459 /* Unicasts are never bypassed, - always add to tail */
460 __skb_queue_tail(defq, skb);
461}
462
Jon Maloy75da2162017-10-13 11:04:23 +0200463/* tipc_group_filter_msg() - determine if we should accept arriving message
464 */
465void tipc_group_filter_msg(struct tipc_group *grp, struct sk_buff_head *inputq,
466 struct sk_buff_head *xmitq)
467{
468 struct sk_buff *skb = __skb_dequeue(inputq);
Jon Maloya3bada72017-10-13 11:04:33 +0200469 bool ack, deliver, update, leave = false;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200470 struct sk_buff_head *defq;
Jon Maloy75da2162017-10-13 11:04:23 +0200471 struct tipc_member *m;
472 struct tipc_msg *hdr;
473 u32 node, port;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200474 int mtyp, blks;
Jon Maloy75da2162017-10-13 11:04:23 +0200475
476 if (!skb)
477 return;
478
479 hdr = buf_msg(skb);
Jon Maloy75da2162017-10-13 11:04:23 +0200480 node = msg_orignode(hdr);
481 port = msg_origport(hdr);
482
483 if (!msg_in_group(hdr))
484 goto drop;
485
486 m = tipc_group_find_member(grp, node, port);
487 if (!tipc_group_is_receiver(m))
488 goto drop;
489
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200490 if (less(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt))
491 goto drop;
Jon Maloy5b8dddb2017-10-13 11:04:29 +0200492
Jon Maloy31c82a22017-10-13 11:04:24 +0200493 TIPC_SKB_CB(skb)->orig_member = m->instance;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200494 defq = &m->deferredq;
495 tipc_group_sort_msg(skb, defq);
Jon Maloy75da2162017-10-13 11:04:23 +0200496
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200497 while ((skb = skb_peek(defq))) {
498 hdr = buf_msg(skb);
499 mtyp = msg_type(hdr);
500 deliver = true;
Jon Maloy2f487712017-10-13 11:04:31 +0200501 ack = false;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200502 update = false;
503
504 if (more(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt))
505 break;
506
507 /* Decide what to do with message */
508 switch (mtyp) {
509 case TIPC_GRP_MCAST_MSG:
510 if (msg_nameinst(hdr) != grp->instance) {
511 update = true;
512 deliver = false;
513 }
514 /* Fall thru */
515 case TIPC_GRP_BCAST_MSG:
516 m->bc_rcv_nxt++;
Jon Maloy2f487712017-10-13 11:04:31 +0200517 ack = msg_grp_bc_ack_req(hdr);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200518 break;
519 case TIPC_GRP_UCAST_MSG:
520 break;
Jon Maloya3bada72017-10-13 11:04:33 +0200521 case TIPC_GRP_MEMBER_EVT:
522 if (m->state == MBR_LEAVING)
523 leave = true;
524 if (!grp->events)
525 deliver = false;
526 break;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200527 default:
528 break;
529 }
530
531 /* Execute decisions */
532 __skb_dequeue(defq);
533 if (deliver)
534 __skb_queue_tail(inputq, skb);
535 else
536 kfree_skb(skb);
537
Jon Maloy2f487712017-10-13 11:04:31 +0200538 if (ack)
539 tipc_group_proto_xmit(grp, m, GRP_ACK_MSG, xmitq);
540
Jon Maloya3bada72017-10-13 11:04:33 +0200541 if (leave) {
Jon Maloya3bada72017-10-13 11:04:33 +0200542 __skb_queue_purge(defq);
Jon Maloye0e853a2017-11-20 21:43:03 +0100543 tipc_group_delete_member(grp, m);
Jon Maloya3bada72017-10-13 11:04:33 +0200544 break;
545 }
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200546 if (!update)
547 continue;
548
549 blks = msg_blocks(hdr);
550 tipc_group_update_rcv_win(grp, blks, node, port, xmitq);
551 }
Jon Maloy75da2162017-10-13 11:04:23 +0200552 return;
553drop:
554 kfree_skb(skb);
555}
556
Jon Maloyb7d42632017-10-13 11:04:26 +0200557void tipc_group_update_rcv_win(struct tipc_group *grp, int blks, u32 node,
558 u32 port, struct sk_buff_head *xmitq)
559{
Jon Maloy04d7b572017-10-13 11:04:34 +0200560 struct list_head *active = &grp->active;
561 int max_active = grp->max_active;
562 int reclaim_limit = max_active * 3 / 4;
563 int active_cnt = grp->active_cnt;
564 struct tipc_member *m, *rm;
Jon Maloyb7d42632017-10-13 11:04:26 +0200565
566 m = tipc_group_find_member(grp, node, port);
567 if (!m)
568 return;
569
570 m->advertised -= blks;
571
572 switch (m->state) {
573 case MBR_JOINED:
Jon Maloy04d7b572017-10-13 11:04:34 +0200574 /* Reclaim advertised space from least active member */
575 if (!list_empty(active) && active_cnt >= reclaim_limit) {
576 rm = list_first_entry(active, struct tipc_member, list);
577 rm->state = MBR_RECLAIMING;
578 list_move_tail(&rm->list, &grp->reclaiming);
579 tipc_group_proto_xmit(grp, rm, GRP_RECLAIM_MSG, xmitq);
580 }
581 /* If max active, become pending and wait for reclaimed space */
582 if (active_cnt >= max_active) {
583 m->state = MBR_PENDING;
584 list_add_tail(&m->list, &grp->pending);
585 break;
586 }
587 /* Otherwise become active */
588 m->state = MBR_ACTIVE;
589 list_add_tail(&m->list, &grp->active);
590 grp->active_cnt++;
591 /* Fall through */
592 case MBR_ACTIVE:
593 if (!list_is_last(&m->list, &grp->active))
594 list_move_tail(&m->list, &grp->active);
595 if (m->advertised > (ADV_ACTIVE * 3 / 4))
596 break;
597 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200598 break;
Jon Maloy04d7b572017-10-13 11:04:34 +0200599 case MBR_REMITTED:
600 if (m->advertised > ADV_IDLE)
601 break;
602 m->state = MBR_JOINED;
603 if (m->advertised < ADV_IDLE) {
604 pr_warn_ratelimited("Rcv unexpected msg after REMIT\n");
605 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
606 }
607 break;
608 case MBR_RECLAIMING:
Jon Maloyb7d42632017-10-13 11:04:26 +0200609 case MBR_DISCOVERED:
610 case MBR_JOINING:
611 case MBR_LEAVING:
612 default:
613 break;
614 }
615}
616
Jon Maloy75da2162017-10-13 11:04:23 +0200617static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
618 int mtyp, struct sk_buff_head *xmitq)
619{
620 struct tipc_msg *hdr;
621 struct sk_buff *skb;
Jon Maloyb7d42632017-10-13 11:04:26 +0200622 int adv = 0;
Jon Maloy75da2162017-10-13 11:04:23 +0200623
624 skb = tipc_msg_create(GROUP_PROTOCOL, mtyp, INT_H_SIZE, 0,
625 m->node, tipc_own_addr(grp->net),
626 m->port, grp->portid, 0);
627 if (!skb)
628 return;
629
Jon Maloy04d7b572017-10-13 11:04:34 +0200630 if (m->state == MBR_ACTIVE)
Jon Maloyb7d42632017-10-13 11:04:26 +0200631 adv = ADV_ACTIVE - m->advertised;
Jon Maloy04d7b572017-10-13 11:04:34 +0200632 else if (m->state == MBR_JOINED || m->state == MBR_PENDING)
633 adv = ADV_IDLE - m->advertised;
Jon Maloyb7d42632017-10-13 11:04:26 +0200634
Jon Maloy75da2162017-10-13 11:04:23 +0200635 hdr = buf_msg(skb);
Jon Maloyb7d42632017-10-13 11:04:26 +0200636
637 if (mtyp == GRP_JOIN_MSG) {
Jon Maloy75da2162017-10-13 11:04:23 +0200638 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200639 msg_set_adv_win(hdr, adv);
640 m->advertised += adv;
Jon Maloya3bada72017-10-13 11:04:33 +0200641 } else if (mtyp == GRP_LEAVE_MSG) {
642 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200643 } else if (mtyp == GRP_ADV_MSG) {
644 msg_set_adv_win(hdr, adv);
645 m->advertised += adv;
Jon Maloy2f487712017-10-13 11:04:31 +0200646 } else if (mtyp == GRP_ACK_MSG) {
647 msg_set_grp_bc_acked(hdr, m->bc_rcv_nxt);
Jon Maloy04d7b572017-10-13 11:04:34 +0200648 } else if (mtyp == GRP_REMIT_MSG) {
649 msg_set_grp_remitted(hdr, m->window);
Jon Maloyb7d42632017-10-13 11:04:26 +0200650 }
Jon Maloy75da2162017-10-13 11:04:23 +0200651 __skb_queue_tail(xmitq, skb);
652}
653
Jon Maloyb7d42632017-10-13 11:04:26 +0200654void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup,
655 struct tipc_msg *hdr, struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200656 struct sk_buff_head *xmitq)
657{
658 u32 node = msg_orignode(hdr);
659 u32 port = msg_origport(hdr);
Jon Maloy04d7b572017-10-13 11:04:34 +0200660 struct tipc_member *m, *pm;
Jon Maloya3bada72017-10-13 11:04:33 +0200661 struct tipc_msg *ehdr;
Jon Maloy04d7b572017-10-13 11:04:34 +0200662 u16 remitted, in_flight;
Jon Maloy75da2162017-10-13 11:04:23 +0200663
664 if (!grp)
665 return;
666
667 m = tipc_group_find_member(grp, node, port);
668
669 switch (msg_type(hdr)) {
670 case GRP_JOIN_MSG:
671 if (!m)
672 m = tipc_group_create_member(grp, node, port,
673 MBR_QUARANTINED);
674 if (!m)
675 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200676 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
677 m->bc_rcv_nxt = m->bc_syncpt;
Jon Maloyb7d42632017-10-13 11:04:26 +0200678 m->window += msg_adv_win(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +0200679
680 /* Wait until PUBLISH event is received */
Jon Maloyae236fb2017-10-13 11:04:25 +0200681 if (m->state == MBR_DISCOVERED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200682 m->state = MBR_JOINING;
Jon Maloyae236fb2017-10-13 11:04:25 +0200683 } else if (m->state == MBR_PUBLISHED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200684 m->state = MBR_JOINED;
Jon Maloyb7d42632017-10-13 11:04:26 +0200685 *usr_wakeup = true;
686 m->usr_pending = false;
687 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
Jon Maloya3bada72017-10-13 11:04:33 +0200688 ehdr = buf_msg(m->event_msg);
689 msg_set_grp_bc_seqno(ehdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200690 __skb_queue_tail(inputq, m->event_msg);
691 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200692 if (m->window < ADV_IDLE)
693 tipc_group_update_member(m, 0);
694 else
695 list_del_init(&m->congested);
Jon Maloy75da2162017-10-13 11:04:23 +0200696 return;
697 case GRP_LEAVE_MSG:
698 if (!m)
699 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200700 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +0200701
702 /* Wait until WITHDRAW event is received */
703 if (m->state != MBR_LEAVING) {
Jon Maloy04d7b572017-10-13 11:04:34 +0200704 tipc_group_decr_active(grp, m);
Jon Maloy75da2162017-10-13 11:04:23 +0200705 m->state = MBR_LEAVING;
706 return;
707 }
708 /* Otherwise deliver already received WITHDRAW event */
Jon Maloya3bada72017-10-13 11:04:33 +0200709 ehdr = buf_msg(m->event_msg);
710 msg_set_grp_bc_seqno(ehdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200711 __skb_queue_tail(inputq, m->event_msg);
Jon Maloy2f487712017-10-13 11:04:31 +0200712 *usr_wakeup = true;
Jon Maloyb7d42632017-10-13 11:04:26 +0200713 list_del_init(&m->congested);
714 return;
715 case GRP_ADV_MSG:
716 if (!m)
717 return;
718 m->window += msg_adv_win(hdr);
719 *usr_wakeup = m->usr_pending;
720 m->usr_pending = false;
721 list_del_init(&m->congested);
Jon Maloy75da2162017-10-13 11:04:23 +0200722 return;
Jon Maloy2f487712017-10-13 11:04:31 +0200723 case GRP_ACK_MSG:
724 if (!m)
725 return;
726 m->bc_acked = msg_grp_bc_acked(hdr);
727 if (--grp->bc_ackers)
728 break;
729 *usr_wakeup = true;
730 m->usr_pending = false;
731 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200732 case GRP_RECLAIM_MSG:
733 if (!m)
734 return;
735 *usr_wakeup = m->usr_pending;
736 m->usr_pending = false;
737 tipc_group_proto_xmit(grp, m, GRP_REMIT_MSG, xmitq);
738 m->window = ADV_IDLE;
739 return;
740 case GRP_REMIT_MSG:
741 if (!m || m->state != MBR_RECLAIMING)
742 return;
743
744 list_del_init(&m->list);
745 grp->active_cnt--;
746 remitted = msg_grp_remitted(hdr);
747
748 /* Messages preceding the REMIT still in receive queue */
749 if (m->advertised > remitted) {
750 m->state = MBR_REMITTED;
751 in_flight = m->advertised - remitted;
752 }
753 /* All messages preceding the REMIT have been read */
754 if (m->advertised <= remitted) {
755 m->state = MBR_JOINED;
756 in_flight = 0;
757 }
758 /* ..and the REMIT overtaken by more messages => re-advertise */
759 if (m->advertised < remitted)
760 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
761
762 m->advertised = ADV_IDLE + in_flight;
763
764 /* Set oldest pending member to active and advertise */
765 if (list_empty(&grp->pending))
766 return;
767 pm = list_first_entry(&grp->pending, struct tipc_member, list);
768 pm->state = MBR_ACTIVE;
769 list_move_tail(&pm->list, &grp->active);
770 grp->active_cnt++;
771 if (pm->advertised <= (ADV_ACTIVE * 3 / 4))
772 tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
773 return;
Jon Maloy75da2162017-10-13 11:04:23 +0200774 default:
775 pr_warn("Received unknown GROUP_PROTO message\n");
776 }
777}
778
Jon Maloyb7d42632017-10-13 11:04:26 +0200779/* tipc_group_member_evt() - receive and handle a member up/down event
780 */
Jon Maloy75da2162017-10-13 11:04:23 +0200781void tipc_group_member_evt(struct tipc_group *grp,
Jon Maloyb7d42632017-10-13 11:04:26 +0200782 bool *usr_wakeup,
783 int *sk_rcvbuf,
Jon Maloy75da2162017-10-13 11:04:23 +0200784 struct sk_buff *skb,
Jon Maloyae236fb2017-10-13 11:04:25 +0200785 struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200786 struct sk_buff_head *xmitq)
787{
788 struct tipc_msg *hdr = buf_msg(skb);
789 struct tipc_event *evt = (void *)msg_data(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +0200790 u32 instance = evt->found_lower;
Jon Maloy75da2162017-10-13 11:04:23 +0200791 u32 node = evt->port.node;
792 u32 port = evt->port.ref;
Jon Maloyae236fb2017-10-13 11:04:25 +0200793 int event = evt->event;
Jon Maloy75da2162017-10-13 11:04:23 +0200794 struct tipc_member *m;
795 struct net *net;
Jon Maloya3bada72017-10-13 11:04:33 +0200796 bool node_up;
Jon Maloy75da2162017-10-13 11:04:23 +0200797 u32 self;
798
799 if (!grp)
800 goto drop;
801
802 net = grp->net;
803 self = tipc_own_addr(net);
804 if (!grp->loopback && node == self && port == grp->portid)
805 goto drop;
806
Jon Maloyae236fb2017-10-13 11:04:25 +0200807 /* Convert message before delivery to user */
808 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
809 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
810 msg_set_type(hdr, TIPC_GRP_MEMBER_EVT);
811 msg_set_origport(hdr, port);
812 msg_set_orignode(hdr, node);
813 msg_set_nametype(hdr, grp->type);
814 msg_set_grp_evt(hdr, event);
815
Jon Maloy75da2162017-10-13 11:04:23 +0200816 m = tipc_group_find_member(grp, node, port);
817
Jon Maloyae236fb2017-10-13 11:04:25 +0200818 if (event == TIPC_PUBLISHED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200819 if (!m)
820 m = tipc_group_create_member(grp, node, port,
821 MBR_DISCOVERED);
822 if (!m)
823 goto drop;
824
Jon Maloyae236fb2017-10-13 11:04:25 +0200825 /* Hold back event if JOIN message not yet received */
826 if (m->state == MBR_DISCOVERED) {
827 m->event_msg = skb;
Jon Maloy75da2162017-10-13 11:04:23 +0200828 m->state = MBR_PUBLISHED;
Jon Maloyae236fb2017-10-13 11:04:25 +0200829 } else {
Jon Maloya3bada72017-10-13 11:04:33 +0200830 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200831 __skb_queue_tail(inputq, skb);
Jon Maloy75da2162017-10-13 11:04:23 +0200832 m->state = MBR_JOINED;
Jon Maloyb7d42632017-10-13 11:04:26 +0200833 *usr_wakeup = true;
834 m->usr_pending = false;
Jon Maloyae236fb2017-10-13 11:04:25 +0200835 }
836 m->instance = instance;
837 TIPC_SKB_CB(skb)->orig_member = m->instance;
Jon Maloy75da2162017-10-13 11:04:23 +0200838 tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200839 if (m->window < ADV_IDLE)
840 tipc_group_update_member(m, 0);
841 else
842 list_del_init(&m->congested);
Jon Maloyae236fb2017-10-13 11:04:25 +0200843 } else if (event == TIPC_WITHDRAWN) {
Jon Maloy75da2162017-10-13 11:04:23 +0200844 if (!m)
845 goto drop;
846
Jon Maloyae236fb2017-10-13 11:04:25 +0200847 TIPC_SKB_CB(skb)->orig_member = m->instance;
848
Jon Maloy2f487712017-10-13 11:04:31 +0200849 *usr_wakeup = true;
Jon Maloyb7d42632017-10-13 11:04:26 +0200850 m->usr_pending = false;
Jon Maloya3bada72017-10-13 11:04:33 +0200851 node_up = tipc_node_is_up(net, node);
Jon Maloyb7d42632017-10-13 11:04:26 +0200852
Jon Maloyae236fb2017-10-13 11:04:25 +0200853 /* Hold back event if more messages might be expected */
Jon Maloya3bada72017-10-13 11:04:33 +0200854 if (m->state != MBR_LEAVING && node_up) {
Jon Maloyae236fb2017-10-13 11:04:25 +0200855 m->event_msg = skb;
Jon Maloy04d7b572017-10-13 11:04:34 +0200856 tipc_group_decr_active(grp, m);
Jon Maloy75da2162017-10-13 11:04:23 +0200857 m->state = MBR_LEAVING;
Jon Maloyae236fb2017-10-13 11:04:25 +0200858 } else {
Jon Maloya3bada72017-10-13 11:04:33 +0200859 if (node_up)
860 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
861 else
862 msg_set_grp_bc_seqno(hdr, m->bc_rcv_nxt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200863 __skb_queue_tail(inputq, skb);
Jon Maloyae236fb2017-10-13 11:04:25 +0200864 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200865 list_del_init(&m->congested);
Jon Maloy75da2162017-10-13 11:04:23 +0200866 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200867 *sk_rcvbuf = tipc_group_rcvbuf_limit(grp);
Jon Maloyae236fb2017-10-13 11:04:25 +0200868 return;
Jon Maloy75da2162017-10-13 11:04:23 +0200869drop:
870 kfree_skb(skb);
871}