blob: e5daeb093879057d604d4e03a860385ebef2888c [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 Maloy38266ca2018-01-04 15:20:44 +010067 struct list_head small_win;
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 Maloy38266ca2018-01-04 15:20:44 +010085 struct list_head small_win;
Jon Maloy04d7b572017-10-13 11:04:34 +020086 struct list_head pending;
87 struct list_head active;
Jon Maloy75da2162017-10-13 11:04:23 +020088 struct tipc_nlist dests;
89 struct net *net;
90 int subid;
91 u32 type;
92 u32 instance;
93 u32 domain;
94 u32 scope;
95 u32 portid;
96 u16 member_cnt;
Jon Maloy04d7b572017-10-13 11:04:34 +020097 u16 active_cnt;
98 u16 max_active;
Jon Maloy75da2162017-10-13 11:04:23 +020099 u16 bc_snd_nxt;
Jon Maloy2f487712017-10-13 11:04:31 +0200100 u16 bc_ackers;
Jon Maloy75da2162017-10-13 11:04:23 +0200101 bool loopback;
Jon Maloyae236fb2017-10-13 11:04:25 +0200102 bool events;
Jon Maloy75da2162017-10-13 11:04:23 +0200103};
104
105static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
106 int mtyp, struct sk_buff_head *xmitq);
107
Jon Maloy04d7b572017-10-13 11:04:34 +0200108static void tipc_group_decr_active(struct tipc_group *grp,
109 struct tipc_member *m)
110{
Jon Maloyf9c935d2017-12-29 19:48:02 +0100111 if (m->state == MBR_ACTIVE || m->state == MBR_RECLAIMING ||
112 m->state == MBR_REMITTED)
Jon Maloy04d7b572017-10-13 11:04:34 +0200113 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 Maloy38266ca2018-01-04 15:20:44 +0100139static bool tipc_group_is_receiver(struct tipc_member *m)
Jon Maloyb7d42632017-10-13 11:04:26 +0200140{
141 return m->state != MBR_QUARANTINED && m->state != MBR_LEAVING;
142}
143
Jon Maloy38266ca2018-01-04 15:20:44 +0100144static bool tipc_group_is_sender(struct tipc_member *m)
Jon Maloy75da2162017-10-13 11:04:23 +0200145{
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 Maloy38266ca2018-01-04 15:20:44 +0100171 INIT_LIST_HEAD(&grp->small_win);
Jon Maloy04d7b572017-10-13 11:04:34 +0200172 INIT_LIST_HEAD(&grp->active);
173 INIT_LIST_HEAD(&grp->pending);
Jon Maloy75da2162017-10-13 11:04:23 +0200174 grp->members = RB_ROOT;
175 grp->net = net;
176 grp->portid = portid;
177 grp->domain = addr_domain(net, mreq->scope);
178 grp->type = type;
179 grp->instance = mreq->instance;
180 grp->scope = mreq->scope;
181 grp->loopback = mreq->flags & TIPC_GROUP_LOOPBACK;
Jon Maloyae236fb2017-10-13 11:04:25 +0200182 grp->events = mreq->flags & TIPC_GROUP_MEMBER_EVTS;
Jon Maloy75da2162017-10-13 11:04:23 +0200183 if (tipc_topsrv_kern_subscr(net, portid, type, 0, ~0, &grp->subid))
184 return grp;
185 kfree(grp);
186 return NULL;
187}
188
189void tipc_group_delete(struct net *net, struct tipc_group *grp)
190{
191 struct rb_root *tree = &grp->members;
192 struct tipc_member *m, *tmp;
193 struct sk_buff_head xmitq;
194
195 __skb_queue_head_init(&xmitq);
196
197 rbtree_postorder_for_each_entry_safe(m, tmp, tree, tree_node) {
198 tipc_group_proto_xmit(grp, m, GRP_LEAVE_MSG, &xmitq);
199 list_del(&m->list);
200 kfree(m);
201 }
202 tipc_node_distr_xmit(net, &xmitq);
203 tipc_nlist_purge(&grp->dests);
204 tipc_topsrv_kern_unsubscr(net, grp->subid);
205 kfree(grp);
206}
207
208struct tipc_member *tipc_group_find_member(struct tipc_group *grp,
209 u32 node, u32 port)
210{
211 struct rb_node *n = grp->members.rb_node;
212 u64 nkey, key = (u64)node << 32 | port;
213 struct tipc_member *m;
214
215 while (n) {
216 m = container_of(n, struct tipc_member, tree_node);
217 nkey = (u64)m->node << 32 | m->port;
218 if (key < nkey)
219 n = n->rb_left;
220 else if (key > nkey)
221 n = n->rb_right;
222 else
223 return m;
224 }
225 return NULL;
226}
227
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200228static struct tipc_member *tipc_group_find_dest(struct tipc_group *grp,
229 u32 node, u32 port)
230{
231 struct tipc_member *m;
232
233 m = tipc_group_find_member(grp, node, port);
Jon Maloy38266ca2018-01-04 15:20:44 +0100234 if (m && tipc_group_is_receiver(m))
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200235 return m;
236 return NULL;
237}
238
Jon Maloy75da2162017-10-13 11:04:23 +0200239static struct tipc_member *tipc_group_find_node(struct tipc_group *grp,
240 u32 node)
241{
242 struct tipc_member *m;
243 struct rb_node *n;
244
245 for (n = rb_first(&grp->members); n; n = rb_next(n)) {
246 m = container_of(n, struct tipc_member, tree_node);
247 if (m->node == node)
248 return m;
249 }
250 return NULL;
251}
252
253static void tipc_group_add_to_tree(struct tipc_group *grp,
254 struct tipc_member *m)
255{
256 u64 nkey, key = (u64)m->node << 32 | m->port;
257 struct rb_node **n, *parent = NULL;
258 struct tipc_member *tmp;
259
260 n = &grp->members.rb_node;
261 while (*n) {
262 tmp = container_of(*n, struct tipc_member, tree_node);
263 parent = *n;
264 tmp = container_of(parent, struct tipc_member, tree_node);
265 nkey = (u64)tmp->node << 32 | tmp->port;
266 if (key < nkey)
267 n = &(*n)->rb_left;
268 else if (key > nkey)
269 n = &(*n)->rb_right;
270 else
271 return;
272 }
273 rb_link_node(&m->tree_node, parent, n);
274 rb_insert_color(&m->tree_node, &grp->members);
275}
276
277static struct tipc_member *tipc_group_create_member(struct tipc_group *grp,
278 u32 node, u32 port,
279 int state)
280{
281 struct tipc_member *m;
282
283 m = kzalloc(sizeof(*m), GFP_ATOMIC);
284 if (!m)
285 return NULL;
286 INIT_LIST_HEAD(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100287 INIT_LIST_HEAD(&m->small_win);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200288 __skb_queue_head_init(&m->deferredq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200289 m->group = grp;
Jon Maloy75da2162017-10-13 11:04:23 +0200290 m->node = node;
291 m->port = port;
Jon Maloy2f487712017-10-13 11:04:31 +0200292 m->bc_acked = grp->bc_snd_nxt - 1;
Jon Maloy75da2162017-10-13 11:04:23 +0200293 grp->member_cnt++;
294 tipc_group_add_to_tree(grp, m);
295 tipc_nlist_add(&grp->dests, m->node);
296 m->state = state;
297 return m;
298}
299
300void tipc_group_add_member(struct tipc_group *grp, u32 node, u32 port)
301{
302 tipc_group_create_member(grp, node, port, MBR_DISCOVERED);
303}
304
305static void tipc_group_delete_member(struct tipc_group *grp,
306 struct tipc_member *m)
307{
308 rb_erase(&m->tree_node, &grp->members);
309 grp->member_cnt--;
Jon Maloy2f487712017-10-13 11:04:31 +0200310
311 /* Check if we were waiting for replicast ack from this member */
312 if (grp->bc_ackers && less(m->bc_acked, grp->bc_snd_nxt - 1))
313 grp->bc_ackers--;
314
Jon Maloy75da2162017-10-13 11:04:23 +0200315 list_del_init(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100316 list_del_init(&m->small_win);
Jon Maloy04d7b572017-10-13 11:04:34 +0200317 tipc_group_decr_active(grp, m);
Jon Maloy75da2162017-10-13 11:04:23 +0200318
319 /* If last member on a node, remove node from dest list */
320 if (!tipc_group_find_node(grp, m->node))
321 tipc_nlist_del(&grp->dests, m->node);
322
323 kfree(m);
324}
325
326struct tipc_nlist *tipc_group_dests(struct tipc_group *grp)
327{
328 return &grp->dests;
329}
330
331void tipc_group_self(struct tipc_group *grp, struct tipc_name_seq *seq,
332 int *scope)
333{
334 seq->type = grp->type;
335 seq->lower = grp->instance;
336 seq->upper = grp->instance;
337 *scope = grp->scope;
338}
339
Jon Maloyb7d42632017-10-13 11:04:26 +0200340void tipc_group_update_member(struct tipc_member *m, int len)
Jon Maloy75da2162017-10-13 11:04:23 +0200341{
Jon Maloyb7d42632017-10-13 11:04:26 +0200342 struct tipc_group *grp = m->group;
343 struct tipc_member *_m, *tmp;
344
Jon Maloy38266ca2018-01-04 15:20:44 +0100345 if (!tipc_group_is_receiver(m))
Jon Maloyb7d42632017-10-13 11:04:26 +0200346 return;
347
348 m->window -= len;
349
350 if (m->window >= ADV_IDLE)
351 return;
352
Jon Maloy38266ca2018-01-04 15:20:44 +0100353 list_del_init(&m->small_win);
Jon Maloyb7d42632017-10-13 11:04:26 +0200354
Jon Maloy38266ca2018-01-04 15:20:44 +0100355 /* Sort member into small_window members' list */
356 list_for_each_entry_safe(_m, tmp, &grp->small_win, small_win) {
Jon Maloyd84d1b32018-01-04 15:20:45 +0100357 if (_m->window > m->window)
358 break;
Jon Maloyb7d42632017-10-13 11:04:26 +0200359 }
Jon Maloyd84d1b32018-01-04 15:20:45 +0100360 list_add_tail(&m->small_win, &_m->small_win);
Jon Maloyb7d42632017-10-13 11:04:26 +0200361}
362
Jon Maloy2f487712017-10-13 11:04:31 +0200363void tipc_group_update_bc_members(struct tipc_group *grp, int len, bool ack)
Jon Maloyb7d42632017-10-13 11:04:26 +0200364{
Jon Maloy2f487712017-10-13 11:04:31 +0200365 u16 prev = grp->bc_snd_nxt - 1;
Jon Maloyb7d42632017-10-13 11:04:26 +0200366 struct tipc_member *m;
367 struct rb_node *n;
Jon Maloy0a3d8052017-12-21 13:07:11 +0100368 u16 ackers = 0;
Jon Maloyb7d42632017-10-13 11:04:26 +0200369
370 for (n = rb_first(&grp->members); n; n = rb_next(n)) {
371 m = container_of(n, struct tipc_member, tree_node);
Jon Maloy38266ca2018-01-04 15:20:44 +0100372 if (tipc_group_is_receiver(m)) {
Jon Maloyb7d42632017-10-13 11:04:26 +0200373 tipc_group_update_member(m, len);
Jon Maloy2f487712017-10-13 11:04:31 +0200374 m->bc_acked = prev;
Jon Maloy0a3d8052017-12-21 13:07:11 +0100375 ackers++;
Jon Maloy2f487712017-10-13 11:04:31 +0200376 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200377 }
Jon Maloy2f487712017-10-13 11:04:31 +0200378
379 /* Mark number of acknowledges to expect, if any */
380 if (ack)
Jon Maloy0a3d8052017-12-21 13:07:11 +0100381 grp->bc_ackers = ackers;
Jon Maloy75da2162017-10-13 11:04:23 +0200382 grp->bc_snd_nxt++;
383}
384
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200385bool tipc_group_cong(struct tipc_group *grp, u32 dnode, u32 dport,
386 int len, struct tipc_member **mbr)
387{
388 struct sk_buff_head xmitq;
389 struct tipc_member *m;
390 int adv, state;
391
392 m = tipc_group_find_dest(grp, dnode, dport);
393 *mbr = m;
394 if (!m)
395 return false;
396 if (m->usr_pending)
397 return true;
398 if (m->window >= len)
399 return false;
400 m->usr_pending = true;
401
402 /* If not fully advertised, do it now to prevent mutual blocking */
403 adv = m->advertised;
404 state = m->state;
405 if (state < MBR_JOINED)
406 return true;
407 if (state == MBR_JOINED && adv == ADV_IDLE)
408 return true;
Jon Maloy04d7b572017-10-13 11:04:34 +0200409 if (state == MBR_ACTIVE && adv == ADV_ACTIVE)
410 return true;
411 if (state == MBR_PENDING && adv == ADV_IDLE)
412 return true;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200413 skb_queue_head_init(&xmitq);
414 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, &xmitq);
415 tipc_node_distr_xmit(grp->net, &xmitq);
416 return true;
417}
418
Jon Maloyb7d42632017-10-13 11:04:26 +0200419bool tipc_group_bc_cong(struct tipc_group *grp, int len)
420{
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200421 struct tipc_member *m = NULL;
Jon Maloyb7d42632017-10-13 11:04:26 +0200422
Jon Maloy2f487712017-10-13 11:04:31 +0200423 /* If prev bcast was replicast, reject until all receivers have acked */
424 if (grp->bc_ackers)
425 return true;
426
Jon Maloy38266ca2018-01-04 15:20:44 +0100427 if (list_empty(&grp->small_win))
Jon Maloyb7d42632017-10-13 11:04:26 +0200428 return false;
429
Jon Maloy38266ca2018-01-04 15:20:44 +0100430 m = list_first_entry(&grp->small_win, struct tipc_member, small_win);
Jon Maloyb7d42632017-10-13 11:04:26 +0200431 if (m->window >= len)
432 return false;
433
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200434 return tipc_group_cong(grp, m->node, m->port, len, &m);
Jon Maloyb7d42632017-10-13 11:04:26 +0200435}
436
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200437/* tipc_group_sort_msg() - sort msg into queue by bcast sequence number
438 */
439static void tipc_group_sort_msg(struct sk_buff *skb, struct sk_buff_head *defq)
440{
441 struct tipc_msg *_hdr, *hdr = buf_msg(skb);
442 u16 bc_seqno = msg_grp_bc_seqno(hdr);
443 struct sk_buff *_skb, *tmp;
444 int mtyp = msg_type(hdr);
445
Jon Maloya3bada72017-10-13 11:04:33 +0200446 /* Bcast/mcast may be bypassed by ucast or other bcast, - sort it in */
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200447 if (mtyp == TIPC_GRP_BCAST_MSG || mtyp == TIPC_GRP_MCAST_MSG) {
448 skb_queue_walk_safe(defq, _skb, tmp) {
449 _hdr = buf_msg(_skb);
450 if (!less(bc_seqno, msg_grp_bc_seqno(_hdr)))
451 continue;
452 __skb_queue_before(defq, _skb, skb);
453 return;
454 }
455 /* Bcast was not bypassed, - add to tail */
456 }
457 /* Unicasts are never bypassed, - always add to tail */
458 __skb_queue_tail(defq, skb);
459}
460
Jon Maloy75da2162017-10-13 11:04:23 +0200461/* tipc_group_filter_msg() - determine if we should accept arriving message
462 */
463void tipc_group_filter_msg(struct tipc_group *grp, struct sk_buff_head *inputq,
464 struct sk_buff_head *xmitq)
465{
466 struct sk_buff *skb = __skb_dequeue(inputq);
Jon Maloya3bada72017-10-13 11:04:33 +0200467 bool ack, deliver, update, leave = false;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200468 struct sk_buff_head *defq;
Jon Maloy75da2162017-10-13 11:04:23 +0200469 struct tipc_member *m;
470 struct tipc_msg *hdr;
471 u32 node, port;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200472 int mtyp, blks;
Jon Maloy75da2162017-10-13 11:04:23 +0200473
474 if (!skb)
475 return;
476
477 hdr = buf_msg(skb);
Jon Maloy75da2162017-10-13 11:04:23 +0200478 node = msg_orignode(hdr);
479 port = msg_origport(hdr);
480
481 if (!msg_in_group(hdr))
482 goto drop;
483
484 m = tipc_group_find_member(grp, node, port);
Jon Maloy38266ca2018-01-04 15:20:44 +0100485 if (!tipc_group_is_sender(m))
Jon Maloy75da2162017-10-13 11:04:23 +0200486 goto drop;
487
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200488 if (less(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt))
489 goto drop;
Jon Maloy5b8dddb2017-10-13 11:04:29 +0200490
Jon Maloy31c82a22017-10-13 11:04:24 +0200491 TIPC_SKB_CB(skb)->orig_member = m->instance;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200492 defq = &m->deferredq;
493 tipc_group_sort_msg(skb, defq);
Jon Maloy75da2162017-10-13 11:04:23 +0200494
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200495 while ((skb = skb_peek(defq))) {
496 hdr = buf_msg(skb);
497 mtyp = msg_type(hdr);
Jon Maloy2e724dc2017-11-27 20:13:39 +0100498 blks = msg_blocks(hdr);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200499 deliver = true;
Jon Maloy2f487712017-10-13 11:04:31 +0200500 ack = false;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200501 update = false;
502
503 if (more(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt))
504 break;
505
506 /* Decide what to do with message */
507 switch (mtyp) {
508 case TIPC_GRP_MCAST_MSG:
509 if (msg_nameinst(hdr) != grp->instance) {
510 update = true;
511 deliver = false;
512 }
513 /* Fall thru */
514 case TIPC_GRP_BCAST_MSG:
515 m->bc_rcv_nxt++;
Jon Maloy2f487712017-10-13 11:04:31 +0200516 ack = msg_grp_bc_ack_req(hdr);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200517 break;
518 case TIPC_GRP_UCAST_MSG:
519 break;
Jon Maloya3bada72017-10-13 11:04:33 +0200520 case TIPC_GRP_MEMBER_EVT:
521 if (m->state == MBR_LEAVING)
522 leave = true;
523 if (!grp->events)
524 deliver = false;
525 break;
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200526 default:
527 break;
528 }
529
530 /* Execute decisions */
531 __skb_dequeue(defq);
532 if (deliver)
533 __skb_queue_tail(inputq, skb);
534 else
535 kfree_skb(skb);
536
Jon Maloy2f487712017-10-13 11:04:31 +0200537 if (ack)
538 tipc_group_proto_xmit(grp, m, GRP_ACK_MSG, xmitq);
539
Jon Maloya3bada72017-10-13 11:04:33 +0200540 if (leave) {
Jon Maloya3bada72017-10-13 11:04:33 +0200541 __skb_queue_purge(defq);
Jon Maloye0e853a2017-11-20 21:43:03 +0100542 tipc_group_delete_member(grp, m);
Jon Maloya3bada72017-10-13 11:04:33 +0200543 break;
544 }
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200545 if (!update)
546 continue;
547
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200548 tipc_group_update_rcv_win(grp, blks, node, port, xmitq);
549 }
Jon Maloy75da2162017-10-13 11:04:23 +0200550 return;
551drop:
552 kfree_skb(skb);
553}
554
Jon Maloyb7d42632017-10-13 11:04:26 +0200555void tipc_group_update_rcv_win(struct tipc_group *grp, int blks, u32 node,
556 u32 port, struct sk_buff_head *xmitq)
557{
Jon Maloy04d7b572017-10-13 11:04:34 +0200558 struct list_head *active = &grp->active;
559 int max_active = grp->max_active;
560 int reclaim_limit = max_active * 3 / 4;
561 int active_cnt = grp->active_cnt;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100562 struct tipc_member *m, *rm, *pm;
Jon Maloyb7d42632017-10-13 11:04:26 +0200563
564 m = tipc_group_find_member(grp, node, port);
565 if (!m)
566 return;
567
568 m->advertised -= blks;
569
570 switch (m->state) {
571 case MBR_JOINED:
Jon Maloy04d7b572017-10-13 11:04:34 +0200572 /* Reclaim advertised space from least active member */
573 if (!list_empty(active) && active_cnt >= reclaim_limit) {
574 rm = list_first_entry(active, struct tipc_member, list);
575 rm->state = MBR_RECLAIMING;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100576 list_del_init(&rm->list);
Jon Maloy04d7b572017-10-13 11:04:34 +0200577 tipc_group_proto_xmit(grp, rm, GRP_RECLAIM_MSG, xmitq);
578 }
579 /* If max active, become pending and wait for reclaimed space */
580 if (active_cnt >= max_active) {
581 m->state = MBR_PENDING;
582 list_add_tail(&m->list, &grp->pending);
583 break;
584 }
585 /* Otherwise become active */
586 m->state = MBR_ACTIVE;
587 list_add_tail(&m->list, &grp->active);
588 grp->active_cnt++;
589 /* Fall through */
590 case MBR_ACTIVE:
591 if (!list_is_last(&m->list, &grp->active))
592 list_move_tail(&m->list, &grp->active);
593 if (m->advertised > (ADV_ACTIVE * 3 / 4))
594 break;
595 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200596 break;
Jon Maloy04d7b572017-10-13 11:04:34 +0200597 case MBR_REMITTED:
598 if (m->advertised > ADV_IDLE)
599 break;
600 m->state = MBR_JOINED;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100601 grp->active_cnt--;
Jon Maloy04d7b572017-10-13 11:04:34 +0200602 if (m->advertised < ADV_IDLE) {
603 pr_warn_ratelimited("Rcv unexpected msg after REMIT\n");
604 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
605 }
Jon Maloy8d5dee22018-01-08 21:03:23 +0100606
Jon Maloyf9c935d2017-12-29 19:48:02 +0100607 if (list_empty(&grp->pending))
608 return;
609
610 /* Set oldest pending member to active and advertise */
611 pm = list_first_entry(&grp->pending, struct tipc_member, list);
612 pm->state = MBR_ACTIVE;
613 list_move_tail(&pm->list, &grp->active);
614 grp->active_cnt++;
615 tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
Jon Maloy04d7b572017-10-13 11:04:34 +0200616 break;
617 case MBR_RECLAIMING:
Jon Maloyb7d42632017-10-13 11:04:26 +0200618 case MBR_DISCOVERED:
619 case MBR_JOINING:
620 case MBR_LEAVING:
621 default:
622 break;
623 }
624}
625
Jon Maloy75da2162017-10-13 11:04:23 +0200626static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
627 int mtyp, struct sk_buff_head *xmitq)
628{
629 struct tipc_msg *hdr;
630 struct sk_buff *skb;
Jon Maloyb7d42632017-10-13 11:04:26 +0200631 int adv = 0;
Jon Maloy75da2162017-10-13 11:04:23 +0200632
633 skb = tipc_msg_create(GROUP_PROTOCOL, mtyp, INT_H_SIZE, 0,
634 m->node, tipc_own_addr(grp->net),
635 m->port, grp->portid, 0);
636 if (!skb)
637 return;
638
Jon Maloy04d7b572017-10-13 11:04:34 +0200639 if (m->state == MBR_ACTIVE)
Jon Maloyb7d42632017-10-13 11:04:26 +0200640 adv = ADV_ACTIVE - m->advertised;
Jon Maloy04d7b572017-10-13 11:04:34 +0200641 else if (m->state == MBR_JOINED || m->state == MBR_PENDING)
642 adv = ADV_IDLE - m->advertised;
Jon Maloyb7d42632017-10-13 11:04:26 +0200643
Jon Maloy75da2162017-10-13 11:04:23 +0200644 hdr = buf_msg(skb);
Jon Maloyb7d42632017-10-13 11:04:26 +0200645
646 if (mtyp == GRP_JOIN_MSG) {
Jon Maloy75da2162017-10-13 11:04:23 +0200647 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200648 msg_set_adv_win(hdr, adv);
649 m->advertised += adv;
Jon Maloya3bada72017-10-13 11:04:33 +0200650 } else if (mtyp == GRP_LEAVE_MSG) {
651 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200652 } else if (mtyp == GRP_ADV_MSG) {
653 msg_set_adv_win(hdr, adv);
654 m->advertised += adv;
Jon Maloy2f487712017-10-13 11:04:31 +0200655 } else if (mtyp == GRP_ACK_MSG) {
656 msg_set_grp_bc_acked(hdr, m->bc_rcv_nxt);
Jon Maloy04d7b572017-10-13 11:04:34 +0200657 } else if (mtyp == GRP_REMIT_MSG) {
658 msg_set_grp_remitted(hdr, m->window);
Jon Maloyb7d42632017-10-13 11:04:26 +0200659 }
Jon Maloy23483392017-12-18 17:34:16 +0100660 msg_set_dest_droppable(hdr, true);
Jon Maloy75da2162017-10-13 11:04:23 +0200661 __skb_queue_tail(xmitq, skb);
662}
663
Jon Maloyb7d42632017-10-13 11:04:26 +0200664void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup,
665 struct tipc_msg *hdr, struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200666 struct sk_buff_head *xmitq)
667{
668 u32 node = msg_orignode(hdr);
669 u32 port = msg_origport(hdr);
Jon Maloy04d7b572017-10-13 11:04:34 +0200670 struct tipc_member *m, *pm;
Jon Maloya3bada72017-10-13 11:04:33 +0200671 struct tipc_msg *ehdr;
Jon Maloy04d7b572017-10-13 11:04:34 +0200672 u16 remitted, in_flight;
Jon Maloy75da2162017-10-13 11:04:23 +0200673
674 if (!grp)
675 return;
676
677 m = tipc_group_find_member(grp, node, port);
678
679 switch (msg_type(hdr)) {
680 case GRP_JOIN_MSG:
681 if (!m)
682 m = tipc_group_create_member(grp, node, port,
683 MBR_QUARANTINED);
684 if (!m)
685 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200686 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
687 m->bc_rcv_nxt = m->bc_syncpt;
Jon Maloyb7d42632017-10-13 11:04:26 +0200688 m->window += msg_adv_win(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +0200689
690 /* Wait until PUBLISH event is received */
Jon Maloyae236fb2017-10-13 11:04:25 +0200691 if (m->state == MBR_DISCOVERED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200692 m->state = MBR_JOINING;
Jon Maloyae236fb2017-10-13 11:04:25 +0200693 } else if (m->state == MBR_PUBLISHED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200694 m->state = MBR_JOINED;
Jon Maloyb7d42632017-10-13 11:04:26 +0200695 *usr_wakeup = true;
696 m->usr_pending = false;
697 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
Jon Maloya3bada72017-10-13 11:04:33 +0200698 ehdr = buf_msg(m->event_msg);
699 msg_set_grp_bc_seqno(ehdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200700 __skb_queue_tail(inputq, m->event_msg);
701 }
Jon Maloy38266ca2018-01-04 15:20:44 +0100702 list_del_init(&m->small_win);
Jon Maloybb25c382017-12-20 11:03:15 +0100703 tipc_group_update_member(m, 0);
Jon Maloy75da2162017-10-13 11:04:23 +0200704 return;
705 case GRP_LEAVE_MSG:
706 if (!m)
707 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200708 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100709 list_del_init(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100710 list_del_init(&m->small_win);
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100711 *usr_wakeup = true;
Jon Maloy75da2162017-10-13 11:04:23 +0200712
713 /* Wait until WITHDRAW event is received */
714 if (m->state != MBR_LEAVING) {
Jon Maloy04d7b572017-10-13 11:04:34 +0200715 tipc_group_decr_active(grp, m);
Jon Maloy75da2162017-10-13 11:04:23 +0200716 m->state = MBR_LEAVING;
717 return;
718 }
719 /* Otherwise deliver already received WITHDRAW event */
Jon Maloya3bada72017-10-13 11:04:33 +0200720 ehdr = buf_msg(m->event_msg);
721 msg_set_grp_bc_seqno(ehdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200722 __skb_queue_tail(inputq, m->event_msg);
Jon Maloyb7d42632017-10-13 11:04:26 +0200723 return;
724 case GRP_ADV_MSG:
725 if (!m)
726 return;
727 m->window += msg_adv_win(hdr);
728 *usr_wakeup = m->usr_pending;
729 m->usr_pending = false;
Jon Maloy38266ca2018-01-04 15:20:44 +0100730 list_del_init(&m->small_win);
Jon Maloy75da2162017-10-13 11:04:23 +0200731 return;
Jon Maloy2f487712017-10-13 11:04:31 +0200732 case GRP_ACK_MSG:
733 if (!m)
734 return;
735 m->bc_acked = msg_grp_bc_acked(hdr);
736 if (--grp->bc_ackers)
737 break;
738 *usr_wakeup = true;
739 m->usr_pending = false;
740 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200741 case GRP_RECLAIM_MSG:
742 if (!m)
743 return;
744 *usr_wakeup = m->usr_pending;
745 m->usr_pending = false;
746 tipc_group_proto_xmit(grp, m, GRP_REMIT_MSG, xmitq);
747 m->window = ADV_IDLE;
748 return;
749 case GRP_REMIT_MSG:
750 if (!m || m->state != MBR_RECLAIMING)
751 return;
752
Jon Maloy04d7b572017-10-13 11:04:34 +0200753 remitted = msg_grp_remitted(hdr);
754
755 /* Messages preceding the REMIT still in receive queue */
756 if (m->advertised > remitted) {
757 m->state = MBR_REMITTED;
758 in_flight = m->advertised - remitted;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100759 m->advertised = ADV_IDLE + in_flight;
760 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200761 }
Jon Maloy8d5dee22018-01-08 21:03:23 +0100762 /* This should never happen */
Jon Maloy04d7b572017-10-13 11:04:34 +0200763 if (m->advertised < remitted)
Jon Maloy8d5dee22018-01-08 21:03:23 +0100764 pr_warn_ratelimited("Unexpected REMIT msg\n");
Jon Maloy04d7b572017-10-13 11:04:34 +0200765
Jon Maloy8d5dee22018-01-08 21:03:23 +0100766 /* All messages preceding the REMIT have been read */
767 m->state = MBR_JOINED;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100768 grp->active_cnt--;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100769 m->advertised = ADV_IDLE;
Jon Maloy04d7b572017-10-13 11:04:34 +0200770
771 /* Set oldest pending member to active and advertise */
772 if (list_empty(&grp->pending))
773 return;
774 pm = list_first_entry(&grp->pending, struct tipc_member, list);
775 pm->state = MBR_ACTIVE;
776 list_move_tail(&pm->list, &grp->active);
777 grp->active_cnt++;
778 if (pm->advertised <= (ADV_ACTIVE * 3 / 4))
779 tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
780 return;
Jon Maloy75da2162017-10-13 11:04:23 +0200781 default:
782 pr_warn("Received unknown GROUP_PROTO message\n");
783 }
784}
785
Jon Maloyb7d42632017-10-13 11:04:26 +0200786/* tipc_group_member_evt() - receive and handle a member up/down event
787 */
Jon Maloy75da2162017-10-13 11:04:23 +0200788void tipc_group_member_evt(struct tipc_group *grp,
Jon Maloyb7d42632017-10-13 11:04:26 +0200789 bool *usr_wakeup,
790 int *sk_rcvbuf,
Jon Maloy75da2162017-10-13 11:04:23 +0200791 struct sk_buff *skb,
Jon Maloyae236fb2017-10-13 11:04:25 +0200792 struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200793 struct sk_buff_head *xmitq)
794{
795 struct tipc_msg *hdr = buf_msg(skb);
796 struct tipc_event *evt = (void *)msg_data(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +0200797 u32 instance = evt->found_lower;
Jon Maloy75da2162017-10-13 11:04:23 +0200798 u32 node = evt->port.node;
799 u32 port = evt->port.ref;
Jon Maloyae236fb2017-10-13 11:04:25 +0200800 int event = evt->event;
Jon Maloy75da2162017-10-13 11:04:23 +0200801 struct tipc_member *m;
802 struct net *net;
Jon Maloya3bada72017-10-13 11:04:33 +0200803 bool node_up;
Jon Maloy75da2162017-10-13 11:04:23 +0200804 u32 self;
805
806 if (!grp)
807 goto drop;
808
809 net = grp->net;
810 self = tipc_own_addr(net);
811 if (!grp->loopback && node == self && port == grp->portid)
812 goto drop;
813
Jon Maloyae236fb2017-10-13 11:04:25 +0200814 /* Convert message before delivery to user */
815 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
816 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
817 msg_set_type(hdr, TIPC_GRP_MEMBER_EVT);
818 msg_set_origport(hdr, port);
819 msg_set_orignode(hdr, node);
820 msg_set_nametype(hdr, grp->type);
821 msg_set_grp_evt(hdr, event);
822
Jon Maloy75da2162017-10-13 11:04:23 +0200823 m = tipc_group_find_member(grp, node, port);
824
Jon Maloyae236fb2017-10-13 11:04:25 +0200825 if (event == TIPC_PUBLISHED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200826 if (!m)
827 m = tipc_group_create_member(grp, node, port,
828 MBR_DISCOVERED);
829 if (!m)
830 goto drop;
831
Jon Maloyae236fb2017-10-13 11:04:25 +0200832 /* Hold back event if JOIN message not yet received */
833 if (m->state == MBR_DISCOVERED) {
834 m->event_msg = skb;
Jon Maloy75da2162017-10-13 11:04:23 +0200835 m->state = MBR_PUBLISHED;
Jon Maloyae236fb2017-10-13 11:04:25 +0200836 } else {
Jon Maloya3bada72017-10-13 11:04:33 +0200837 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200838 __skb_queue_tail(inputq, skb);
Jon Maloy75da2162017-10-13 11:04:23 +0200839 m->state = MBR_JOINED;
Jon Maloyb7d42632017-10-13 11:04:26 +0200840 *usr_wakeup = true;
841 m->usr_pending = false;
Jon Maloyae236fb2017-10-13 11:04:25 +0200842 }
843 m->instance = instance;
844 TIPC_SKB_CB(skb)->orig_member = m->instance;
Jon Maloy75da2162017-10-13 11:04:23 +0200845 tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, xmitq);
Jon Maloyd84d1b32018-01-04 15:20:45 +0100846 tipc_group_update_member(m, 0);
Jon Maloyae236fb2017-10-13 11:04:25 +0200847 } else if (event == TIPC_WITHDRAWN) {
Jon Maloy75da2162017-10-13 11:04:23 +0200848 if (!m)
849 goto drop;
850
Jon Maloyae236fb2017-10-13 11:04:25 +0200851 TIPC_SKB_CB(skb)->orig_member = m->instance;
852
Jon Maloy2f487712017-10-13 11:04:31 +0200853 *usr_wakeup = true;
Jon Maloyb7d42632017-10-13 11:04:26 +0200854 m->usr_pending = false;
Jon Maloya3bada72017-10-13 11:04:33 +0200855 node_up = tipc_node_is_up(net, node);
Jon Maloy3a33a192017-12-21 14:36:34 +0100856 m->event_msg = NULL;
Jon Maloyb7d42632017-10-13 11:04:26 +0200857
Jon Maloy3a33a192017-12-21 14:36:34 +0100858 if (node_up) {
859 /* Hold back event if a LEAVE msg should be expected */
860 if (m->state != MBR_LEAVING) {
861 m->event_msg = skb;
862 tipc_group_decr_active(grp, m);
863 m->state = MBR_LEAVING;
864 } else {
Jon Maloya3bada72017-10-13 11:04:33 +0200865 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
Jon Maloy3a33a192017-12-21 14:36:34 +0100866 __skb_queue_tail(inputq, skb);
867 }
868 } else {
869 if (m->state != MBR_LEAVING) {
870 tipc_group_decr_active(grp, m);
871 m->state = MBR_LEAVING;
Jon Maloya3bada72017-10-13 11:04:33 +0200872 msg_set_grp_bc_seqno(hdr, m->bc_rcv_nxt);
Jon Maloy3a33a192017-12-21 14:36:34 +0100873 } else {
874 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
875 }
Jon Maloyae236fb2017-10-13 11:04:25 +0200876 __skb_queue_tail(inputq, skb);
Jon Maloyae236fb2017-10-13 11:04:25 +0200877 }
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100878 list_del_init(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100879 list_del_init(&m->small_win);
Jon Maloy75da2162017-10-13 11:04:23 +0200880 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200881 *sk_rcvbuf = tipc_group_rcvbuf_limit(grp);
Jon Maloyae236fb2017-10-13 11:04:25 +0200882 return;
Jon Maloy75da2162017-10-13 11:04:23 +0200883drop:
884 kfree_skb(skb);
885}