blob: 652fa66a87f67155cf881a7e20647ea69e35cec3 [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 Maloy4ea5dab2018-01-08 21:03:24 +0100572 /* First, decide if member can go active */
573 if (active_cnt <= max_active) {
574 m->state = MBR_ACTIVE;
575 list_add_tail(&m->list, active);
576 grp->active_cnt++;
577 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
578 } else {
579 m->state = MBR_PENDING;
580 list_add_tail(&m->list, &grp->pending);
581 }
582
583 if (active_cnt < reclaim_limit)
584 break;
585
586 /* Reclaim from oldest active member, if possible */
587 if (!list_empty(active)) {
Jon Maloy04d7b572017-10-13 11:04:34 +0200588 rm = list_first_entry(active, struct tipc_member, list);
589 rm->state = MBR_RECLAIMING;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100590 list_del_init(&rm->list);
Jon Maloy04d7b572017-10-13 11:04:34 +0200591 tipc_group_proto_xmit(grp, rm, GRP_RECLAIM_MSG, xmitq);
Jon Maloy04d7b572017-10-13 11:04:34 +0200592 break;
593 }
Jon Maloy4ea5dab2018-01-08 21:03:24 +0100594 /* Nobody to reclaim from; - revert oldest pending to JOINED */
595 pm = list_first_entry(&grp->pending, struct tipc_member, list);
596 list_del_init(&pm->list);
597 pm->state = MBR_JOINED;
598 tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
599 break;
Jon Maloy04d7b572017-10-13 11:04:34 +0200600 case MBR_ACTIVE:
601 if (!list_is_last(&m->list, &grp->active))
602 list_move_tail(&m->list, &grp->active);
603 if (m->advertised > (ADV_ACTIVE * 3 / 4))
604 break;
605 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +0200606 break;
Jon Maloy04d7b572017-10-13 11:04:34 +0200607 case MBR_REMITTED:
608 if (m->advertised > ADV_IDLE)
609 break;
610 m->state = MBR_JOINED;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100611 grp->active_cnt--;
Jon Maloy04d7b572017-10-13 11:04:34 +0200612 if (m->advertised < ADV_IDLE) {
613 pr_warn_ratelimited("Rcv unexpected msg after REMIT\n");
614 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
615 }
Jon Maloy8d5dee22018-01-08 21:03:23 +0100616
Jon Maloyf9c935d2017-12-29 19:48:02 +0100617 if (list_empty(&grp->pending))
618 return;
619
620 /* Set oldest pending member to active and advertise */
621 pm = list_first_entry(&grp->pending, struct tipc_member, list);
622 pm->state = MBR_ACTIVE;
623 list_move_tail(&pm->list, &grp->active);
624 grp->active_cnt++;
625 tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
Jon Maloy04d7b572017-10-13 11:04:34 +0200626 break;
627 case MBR_RECLAIMING:
Jon Maloyb7d42632017-10-13 11:04:26 +0200628 case MBR_DISCOVERED:
629 case MBR_JOINING:
630 case MBR_LEAVING:
631 default:
632 break;
633 }
634}
635
Jon Maloy75da2162017-10-13 11:04:23 +0200636static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
637 int mtyp, struct sk_buff_head *xmitq)
638{
639 struct tipc_msg *hdr;
640 struct sk_buff *skb;
Jon Maloyb7d42632017-10-13 11:04:26 +0200641 int adv = 0;
Jon Maloy75da2162017-10-13 11:04:23 +0200642
643 skb = tipc_msg_create(GROUP_PROTOCOL, mtyp, INT_H_SIZE, 0,
644 m->node, tipc_own_addr(grp->net),
645 m->port, grp->portid, 0);
646 if (!skb)
647 return;
648
Jon Maloy04d7b572017-10-13 11:04:34 +0200649 if (m->state == MBR_ACTIVE)
Jon Maloyb7d42632017-10-13 11:04:26 +0200650 adv = ADV_ACTIVE - m->advertised;
Jon Maloy04d7b572017-10-13 11:04:34 +0200651 else if (m->state == MBR_JOINED || m->state == MBR_PENDING)
652 adv = ADV_IDLE - m->advertised;
Jon Maloyb7d42632017-10-13 11:04:26 +0200653
Jon Maloy75da2162017-10-13 11:04:23 +0200654 hdr = buf_msg(skb);
Jon Maloyb7d42632017-10-13 11:04:26 +0200655
656 if (mtyp == GRP_JOIN_MSG) {
Jon Maloy75da2162017-10-13 11:04:23 +0200657 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200658 msg_set_adv_win(hdr, adv);
659 m->advertised += adv;
Jon Maloya3bada72017-10-13 11:04:33 +0200660 } else if (mtyp == GRP_LEAVE_MSG) {
661 msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt);
Jon Maloyb7d42632017-10-13 11:04:26 +0200662 } else if (mtyp == GRP_ADV_MSG) {
663 msg_set_adv_win(hdr, adv);
664 m->advertised += adv;
Jon Maloy2f487712017-10-13 11:04:31 +0200665 } else if (mtyp == GRP_ACK_MSG) {
666 msg_set_grp_bc_acked(hdr, m->bc_rcv_nxt);
Jon Maloy04d7b572017-10-13 11:04:34 +0200667 } else if (mtyp == GRP_REMIT_MSG) {
668 msg_set_grp_remitted(hdr, m->window);
Jon Maloyb7d42632017-10-13 11:04:26 +0200669 }
Jon Maloy23483392017-12-18 17:34:16 +0100670 msg_set_dest_droppable(hdr, true);
Jon Maloy75da2162017-10-13 11:04:23 +0200671 __skb_queue_tail(xmitq, skb);
672}
673
Jon Maloyb7d42632017-10-13 11:04:26 +0200674void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup,
675 struct tipc_msg *hdr, struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200676 struct sk_buff_head *xmitq)
677{
678 u32 node = msg_orignode(hdr);
679 u32 port = msg_origport(hdr);
Jon Maloy04d7b572017-10-13 11:04:34 +0200680 struct tipc_member *m, *pm;
Jon Maloya3bada72017-10-13 11:04:33 +0200681 struct tipc_msg *ehdr;
Jon Maloy04d7b572017-10-13 11:04:34 +0200682 u16 remitted, in_flight;
Jon Maloy75da2162017-10-13 11:04:23 +0200683
684 if (!grp)
685 return;
686
687 m = tipc_group_find_member(grp, node, port);
688
689 switch (msg_type(hdr)) {
690 case GRP_JOIN_MSG:
691 if (!m)
692 m = tipc_group_create_member(grp, node, port,
693 MBR_QUARANTINED);
694 if (!m)
695 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200696 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
697 m->bc_rcv_nxt = m->bc_syncpt;
Jon Maloyb7d42632017-10-13 11:04:26 +0200698 m->window += msg_adv_win(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +0200699
700 /* Wait until PUBLISH event is received */
Jon Maloyae236fb2017-10-13 11:04:25 +0200701 if (m->state == MBR_DISCOVERED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200702 m->state = MBR_JOINING;
Jon Maloyae236fb2017-10-13 11:04:25 +0200703 } else if (m->state == MBR_PUBLISHED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200704 m->state = MBR_JOINED;
Jon Maloyb7d42632017-10-13 11:04:26 +0200705 *usr_wakeup = true;
706 m->usr_pending = false;
707 tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq);
Jon Maloya3bada72017-10-13 11:04:33 +0200708 ehdr = buf_msg(m->event_msg);
709 msg_set_grp_bc_seqno(ehdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200710 __skb_queue_tail(inputq, m->event_msg);
711 }
Jon Maloy38266ca2018-01-04 15:20:44 +0100712 list_del_init(&m->small_win);
Jon Maloybb25c382017-12-20 11:03:15 +0100713 tipc_group_update_member(m, 0);
Jon Maloy75da2162017-10-13 11:04:23 +0200714 return;
715 case GRP_LEAVE_MSG:
716 if (!m)
717 return;
Jon Maloya3bada72017-10-13 11:04:33 +0200718 m->bc_syncpt = msg_grp_bc_syncpt(hdr);
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100719 list_del_init(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100720 list_del_init(&m->small_win);
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100721 *usr_wakeup = true;
Jon Maloy75da2162017-10-13 11:04:23 +0200722
723 /* Wait until WITHDRAW event is received */
724 if (m->state != MBR_LEAVING) {
Jon Maloy04d7b572017-10-13 11:04:34 +0200725 tipc_group_decr_active(grp, m);
Jon Maloy75da2162017-10-13 11:04:23 +0200726 m->state = MBR_LEAVING;
727 return;
728 }
729 /* Otherwise deliver already received WITHDRAW event */
Jon Maloya3bada72017-10-13 11:04:33 +0200730 ehdr = buf_msg(m->event_msg);
731 msg_set_grp_bc_seqno(ehdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200732 __skb_queue_tail(inputq, m->event_msg);
Jon Maloyb7d42632017-10-13 11:04:26 +0200733 return;
734 case GRP_ADV_MSG:
735 if (!m)
736 return;
737 m->window += msg_adv_win(hdr);
738 *usr_wakeup = m->usr_pending;
739 m->usr_pending = false;
Jon Maloy38266ca2018-01-04 15:20:44 +0100740 list_del_init(&m->small_win);
Jon Maloy75da2162017-10-13 11:04:23 +0200741 return;
Jon Maloy2f487712017-10-13 11:04:31 +0200742 case GRP_ACK_MSG:
743 if (!m)
744 return;
745 m->bc_acked = msg_grp_bc_acked(hdr);
746 if (--grp->bc_ackers)
747 break;
748 *usr_wakeup = true;
749 m->usr_pending = false;
750 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200751 case GRP_RECLAIM_MSG:
752 if (!m)
753 return;
754 *usr_wakeup = m->usr_pending;
755 m->usr_pending = false;
756 tipc_group_proto_xmit(grp, m, GRP_REMIT_MSG, xmitq);
757 m->window = ADV_IDLE;
758 return;
759 case GRP_REMIT_MSG:
760 if (!m || m->state != MBR_RECLAIMING)
761 return;
762
Jon Maloy04d7b572017-10-13 11:04:34 +0200763 remitted = msg_grp_remitted(hdr);
764
765 /* Messages preceding the REMIT still in receive queue */
766 if (m->advertised > remitted) {
767 m->state = MBR_REMITTED;
768 in_flight = m->advertised - remitted;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100769 m->advertised = ADV_IDLE + in_flight;
770 return;
Jon Maloy04d7b572017-10-13 11:04:34 +0200771 }
Jon Maloy8d5dee22018-01-08 21:03:23 +0100772 /* This should never happen */
Jon Maloy04d7b572017-10-13 11:04:34 +0200773 if (m->advertised < remitted)
Jon Maloy8d5dee22018-01-08 21:03:23 +0100774 pr_warn_ratelimited("Unexpected REMIT msg\n");
Jon Maloy04d7b572017-10-13 11:04:34 +0200775
Jon Maloy8d5dee22018-01-08 21:03:23 +0100776 /* All messages preceding the REMIT have been read */
777 m->state = MBR_JOINED;
Jon Maloyf9c935d2017-12-29 19:48:02 +0100778 grp->active_cnt--;
Jon Maloy8d5dee22018-01-08 21:03:23 +0100779 m->advertised = ADV_IDLE;
Jon Maloy04d7b572017-10-13 11:04:34 +0200780
781 /* Set oldest pending member to active and advertise */
782 if (list_empty(&grp->pending))
783 return;
784 pm = list_first_entry(&grp->pending, struct tipc_member, list);
785 pm->state = MBR_ACTIVE;
786 list_move_tail(&pm->list, &grp->active);
787 grp->active_cnt++;
788 if (pm->advertised <= (ADV_ACTIVE * 3 / 4))
789 tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq);
790 return;
Jon Maloy75da2162017-10-13 11:04:23 +0200791 default:
792 pr_warn("Received unknown GROUP_PROTO message\n");
793 }
794}
795
Jon Maloyb7d42632017-10-13 11:04:26 +0200796/* tipc_group_member_evt() - receive and handle a member up/down event
797 */
Jon Maloy75da2162017-10-13 11:04:23 +0200798void tipc_group_member_evt(struct tipc_group *grp,
Jon Maloyb7d42632017-10-13 11:04:26 +0200799 bool *usr_wakeup,
800 int *sk_rcvbuf,
Jon Maloy75da2162017-10-13 11:04:23 +0200801 struct sk_buff *skb,
Jon Maloyae236fb2017-10-13 11:04:25 +0200802 struct sk_buff_head *inputq,
Jon Maloy75da2162017-10-13 11:04:23 +0200803 struct sk_buff_head *xmitq)
804{
805 struct tipc_msg *hdr = buf_msg(skb);
806 struct tipc_event *evt = (void *)msg_data(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +0200807 u32 instance = evt->found_lower;
Jon Maloy75da2162017-10-13 11:04:23 +0200808 u32 node = evt->port.node;
809 u32 port = evt->port.ref;
Jon Maloyae236fb2017-10-13 11:04:25 +0200810 int event = evt->event;
Jon Maloy75da2162017-10-13 11:04:23 +0200811 struct tipc_member *m;
812 struct net *net;
Jon Maloya3bada72017-10-13 11:04:33 +0200813 bool node_up;
Jon Maloy75da2162017-10-13 11:04:23 +0200814 u32 self;
815
816 if (!grp)
817 goto drop;
818
819 net = grp->net;
820 self = tipc_own_addr(net);
821 if (!grp->loopback && node == self && port == grp->portid)
822 goto drop;
823
Jon Maloyae236fb2017-10-13 11:04:25 +0200824 /* Convert message before delivery to user */
825 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
826 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
827 msg_set_type(hdr, TIPC_GRP_MEMBER_EVT);
828 msg_set_origport(hdr, port);
829 msg_set_orignode(hdr, node);
830 msg_set_nametype(hdr, grp->type);
831 msg_set_grp_evt(hdr, event);
832
Jon Maloy75da2162017-10-13 11:04:23 +0200833 m = tipc_group_find_member(grp, node, port);
834
Jon Maloyae236fb2017-10-13 11:04:25 +0200835 if (event == TIPC_PUBLISHED) {
Jon Maloy75da2162017-10-13 11:04:23 +0200836 if (!m)
837 m = tipc_group_create_member(grp, node, port,
838 MBR_DISCOVERED);
839 if (!m)
840 goto drop;
841
Jon Maloyae236fb2017-10-13 11:04:25 +0200842 /* Hold back event if JOIN message not yet received */
843 if (m->state == MBR_DISCOVERED) {
844 m->event_msg = skb;
Jon Maloy75da2162017-10-13 11:04:23 +0200845 m->state = MBR_PUBLISHED;
Jon Maloyae236fb2017-10-13 11:04:25 +0200846 } else {
Jon Maloya3bada72017-10-13 11:04:33 +0200847 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
Jon Maloyae236fb2017-10-13 11:04:25 +0200848 __skb_queue_tail(inputq, skb);
Jon Maloy75da2162017-10-13 11:04:23 +0200849 m->state = MBR_JOINED;
Jon Maloyb7d42632017-10-13 11:04:26 +0200850 *usr_wakeup = true;
851 m->usr_pending = false;
Jon Maloyae236fb2017-10-13 11:04:25 +0200852 }
853 m->instance = instance;
854 TIPC_SKB_CB(skb)->orig_member = m->instance;
Jon Maloy75da2162017-10-13 11:04:23 +0200855 tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, xmitq);
Jon Maloyd84d1b32018-01-04 15:20:45 +0100856 tipc_group_update_member(m, 0);
Jon Maloyae236fb2017-10-13 11:04:25 +0200857 } else if (event == TIPC_WITHDRAWN) {
Jon Maloy75da2162017-10-13 11:04:23 +0200858 if (!m)
859 goto drop;
860
Jon Maloyae236fb2017-10-13 11:04:25 +0200861 TIPC_SKB_CB(skb)->orig_member = m->instance;
862
Jon Maloy2f487712017-10-13 11:04:31 +0200863 *usr_wakeup = true;
Jon Maloyb7d42632017-10-13 11:04:26 +0200864 m->usr_pending = false;
Jon Maloya3bada72017-10-13 11:04:33 +0200865 node_up = tipc_node_is_up(net, node);
Jon Maloy3a33a192017-12-21 14:36:34 +0100866 m->event_msg = NULL;
Jon Maloyb7d42632017-10-13 11:04:26 +0200867
Jon Maloy3a33a192017-12-21 14:36:34 +0100868 if (node_up) {
869 /* Hold back event if a LEAVE msg should be expected */
870 if (m->state != MBR_LEAVING) {
871 m->event_msg = skb;
872 tipc_group_decr_active(grp, m);
873 m->state = MBR_LEAVING;
874 } else {
Jon Maloya3bada72017-10-13 11:04:33 +0200875 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
Jon Maloy3a33a192017-12-21 14:36:34 +0100876 __skb_queue_tail(inputq, skb);
877 }
878 } else {
879 if (m->state != MBR_LEAVING) {
880 tipc_group_decr_active(grp, m);
881 m->state = MBR_LEAVING;
Jon Maloya3bada72017-10-13 11:04:33 +0200882 msg_set_grp_bc_seqno(hdr, m->bc_rcv_nxt);
Jon Maloy3a33a192017-12-21 14:36:34 +0100883 } else {
884 msg_set_grp_bc_seqno(hdr, m->bc_syncpt);
885 }
Jon Maloyae236fb2017-10-13 11:04:25 +0200886 __skb_queue_tail(inputq, skb);
Jon Maloyae236fb2017-10-13 11:04:25 +0200887 }
Jon Maloy3f42f5f2017-12-18 18:13:34 +0100888 list_del_init(&m->list);
Jon Maloy38266ca2018-01-04 15:20:44 +0100889 list_del_init(&m->small_win);
Jon Maloy75da2162017-10-13 11:04:23 +0200890 }
Jon Maloyb7d42632017-10-13 11:04:26 +0200891 *sk_rcvbuf = tipc_group_rcvbuf_limit(grp);
Jon Maloyae236fb2017-10-13 11:04:25 +0200892 return;
Jon Maloy75da2162017-10-13 11:04:23 +0200893drop:
894 kfree_skb(skb);
895}