]>
Commit | Line | Data |
---|---|---|
1 | /* | |
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 | |
49 | #define ADV_ACTIVE (ADV_UNIT * 12) | |
50 | ||
51 | enum mbr_state { | |
52 | MBR_QUARANTINED, | |
53 | MBR_DISCOVERED, | |
54 | MBR_JOINING, | |
55 | MBR_PUBLISHED, | |
56 | MBR_JOINED, | |
57 | MBR_PENDING, | |
58 | MBR_ACTIVE, | |
59 | MBR_RECLAIMING, | |
60 | MBR_REMITTED, | |
61 | MBR_LEAVING | |
62 | }; | |
63 | ||
64 | struct tipc_member { | |
65 | struct rb_node tree_node; | |
66 | struct list_head list; | |
67 | struct list_head small_win; | |
68 | struct sk_buff *event_msg; | |
69 | struct sk_buff_head deferredq; | |
70 | struct tipc_group *group; | |
71 | u32 node; | |
72 | u32 port; | |
73 | u32 instance; | |
74 | enum mbr_state state; | |
75 | u16 advertised; | |
76 | u16 window; | |
77 | u16 bc_rcv_nxt; | |
78 | u16 bc_syncpt; | |
79 | u16 bc_acked; | |
80 | bool usr_pending; | |
81 | }; | |
82 | ||
83 | struct tipc_group { | |
84 | struct rb_root members; | |
85 | struct list_head small_win; | |
86 | struct list_head pending; | |
87 | struct list_head active; | |
88 | struct list_head reclaiming; | |
89 | 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; | |
98 | u16 active_cnt; | |
99 | u16 max_active; | |
100 | u16 bc_snd_nxt; | |
101 | u16 bc_ackers; | |
102 | bool loopback; | |
103 | bool events; | |
104 | }; | |
105 | ||
106 | static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m, | |
107 | int mtyp, struct sk_buff_head *xmitq); | |
108 | ||
109 | static 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 | m->state == MBR_REMITTED) | |
114 | grp->active_cnt--; | |
115 | } | |
116 | ||
117 | static int tipc_group_rcvbuf_limit(struct tipc_group *grp) | |
118 | { | |
119 | int max_active, active_pool, idle_pool; | |
120 | int mcnt = grp->member_cnt + 1; | |
121 | ||
122 | /* Limit simultaneous reception from other members */ | |
123 | max_active = min(mcnt / 8, 64); | |
124 | max_active = max(max_active, 16); | |
125 | grp->max_active = max_active; | |
126 | ||
127 | /* Reserve blocks for active and idle members */ | |
128 | active_pool = max_active * ADV_ACTIVE; | |
129 | idle_pool = (mcnt - max_active) * ADV_IDLE; | |
130 | ||
131 | /* Scale to bytes, considering worst-case truesize/msgsize ratio */ | |
132 | return (active_pool + idle_pool) * FLOWCTL_BLK_SZ * 4; | |
133 | } | |
134 | ||
135 | u16 tipc_group_bc_snd_nxt(struct tipc_group *grp) | |
136 | { | |
137 | return grp->bc_snd_nxt; | |
138 | } | |
139 | ||
140 | static bool tipc_group_is_receiver(struct tipc_member *m) | |
141 | { | |
142 | return m->state != MBR_QUARANTINED && m->state != MBR_LEAVING; | |
143 | } | |
144 | ||
145 | static bool tipc_group_is_sender(struct tipc_member *m) | |
146 | { | |
147 | return m && m->state >= MBR_JOINED; | |
148 | } | |
149 | ||
150 | u32 tipc_group_exclude(struct tipc_group *grp) | |
151 | { | |
152 | if (!grp->loopback) | |
153 | return grp->portid; | |
154 | return 0; | |
155 | } | |
156 | ||
157 | int tipc_group_size(struct tipc_group *grp) | |
158 | { | |
159 | return grp->member_cnt; | |
160 | } | |
161 | ||
162 | struct tipc_group *tipc_group_create(struct net *net, u32 portid, | |
163 | struct tipc_group_req *mreq) | |
164 | { | |
165 | struct tipc_group *grp; | |
166 | u32 type = mreq->type; | |
167 | ||
168 | grp = kzalloc(sizeof(*grp), GFP_ATOMIC); | |
169 | if (!grp) | |
170 | return NULL; | |
171 | tipc_nlist_init(&grp->dests, tipc_own_addr(net)); | |
172 | INIT_LIST_HEAD(&grp->small_win); | |
173 | INIT_LIST_HEAD(&grp->active); | |
174 | INIT_LIST_HEAD(&grp->pending); | |
175 | INIT_LIST_HEAD(&grp->reclaiming); | |
176 | grp->members = RB_ROOT; | |
177 | grp->net = net; | |
178 | grp->portid = portid; | |
179 | grp->domain = addr_domain(net, mreq->scope); | |
180 | grp->type = type; | |
181 | grp->instance = mreq->instance; | |
182 | grp->scope = mreq->scope; | |
183 | grp->loopback = mreq->flags & TIPC_GROUP_LOOPBACK; | |
184 | grp->events = mreq->flags & TIPC_GROUP_MEMBER_EVTS; | |
185 | if (tipc_topsrv_kern_subscr(net, portid, type, 0, ~0, &grp->subid)) | |
186 | return grp; | |
187 | kfree(grp); | |
188 | return NULL; | |
189 | } | |
190 | ||
191 | void tipc_group_delete(struct net *net, struct tipc_group *grp) | |
192 | { | |
193 | struct rb_root *tree = &grp->members; | |
194 | struct tipc_member *m, *tmp; | |
195 | struct sk_buff_head xmitq; | |
196 | ||
197 | __skb_queue_head_init(&xmitq); | |
198 | ||
199 | rbtree_postorder_for_each_entry_safe(m, tmp, tree, tree_node) { | |
200 | tipc_group_proto_xmit(grp, m, GRP_LEAVE_MSG, &xmitq); | |
201 | list_del(&m->list); | |
202 | kfree(m); | |
203 | } | |
204 | tipc_node_distr_xmit(net, &xmitq); | |
205 | tipc_nlist_purge(&grp->dests); | |
206 | tipc_topsrv_kern_unsubscr(net, grp->subid); | |
207 | kfree(grp); | |
208 | } | |
209 | ||
210 | struct tipc_member *tipc_group_find_member(struct tipc_group *grp, | |
211 | u32 node, u32 port) | |
212 | { | |
213 | struct rb_node *n = grp->members.rb_node; | |
214 | u64 nkey, key = (u64)node << 32 | port; | |
215 | struct tipc_member *m; | |
216 | ||
217 | while (n) { | |
218 | m = container_of(n, struct tipc_member, tree_node); | |
219 | nkey = (u64)m->node << 32 | m->port; | |
220 | if (key < nkey) | |
221 | n = n->rb_left; | |
222 | else if (key > nkey) | |
223 | n = n->rb_right; | |
224 | else | |
225 | return m; | |
226 | } | |
227 | return NULL; | |
228 | } | |
229 | ||
230 | static struct tipc_member *tipc_group_find_dest(struct tipc_group *grp, | |
231 | u32 node, u32 port) | |
232 | { | |
233 | struct tipc_member *m; | |
234 | ||
235 | m = tipc_group_find_member(grp, node, port); | |
236 | if (m && tipc_group_is_receiver(m)) | |
237 | return m; | |
238 | return NULL; | |
239 | } | |
240 | ||
241 | static struct tipc_member *tipc_group_find_node(struct tipc_group *grp, | |
242 | u32 node) | |
243 | { | |
244 | struct tipc_member *m; | |
245 | struct rb_node *n; | |
246 | ||
247 | for (n = rb_first(&grp->members); n; n = rb_next(n)) { | |
248 | m = container_of(n, struct tipc_member, tree_node); | |
249 | if (m->node == node) | |
250 | return m; | |
251 | } | |
252 | return NULL; | |
253 | } | |
254 | ||
255 | static void tipc_group_add_to_tree(struct tipc_group *grp, | |
256 | struct tipc_member *m) | |
257 | { | |
258 | u64 nkey, key = (u64)m->node << 32 | m->port; | |
259 | struct rb_node **n, *parent = NULL; | |
260 | struct tipc_member *tmp; | |
261 | ||
262 | n = &grp->members.rb_node; | |
263 | while (*n) { | |
264 | tmp = container_of(*n, struct tipc_member, tree_node); | |
265 | parent = *n; | |
266 | tmp = container_of(parent, struct tipc_member, tree_node); | |
267 | nkey = (u64)tmp->node << 32 | tmp->port; | |
268 | if (key < nkey) | |
269 | n = &(*n)->rb_left; | |
270 | else if (key > nkey) | |
271 | n = &(*n)->rb_right; | |
272 | else | |
273 | return; | |
274 | } | |
275 | rb_link_node(&m->tree_node, parent, n); | |
276 | rb_insert_color(&m->tree_node, &grp->members); | |
277 | } | |
278 | ||
279 | static struct tipc_member *tipc_group_create_member(struct tipc_group *grp, | |
280 | u32 node, u32 port, | |
281 | int state) | |
282 | { | |
283 | struct tipc_member *m; | |
284 | ||
285 | m = kzalloc(sizeof(*m), GFP_ATOMIC); | |
286 | if (!m) | |
287 | return NULL; | |
288 | INIT_LIST_HEAD(&m->list); | |
289 | INIT_LIST_HEAD(&m->small_win); | |
290 | __skb_queue_head_init(&m->deferredq); | |
291 | m->group = grp; | |
292 | m->node = node; | |
293 | m->port = port; | |
294 | m->bc_acked = grp->bc_snd_nxt - 1; | |
295 | grp->member_cnt++; | |
296 | tipc_group_add_to_tree(grp, m); | |
297 | tipc_nlist_add(&grp->dests, m->node); | |
298 | m->state = state; | |
299 | return m; | |
300 | } | |
301 | ||
302 | void tipc_group_add_member(struct tipc_group *grp, u32 node, u32 port) | |
303 | { | |
304 | tipc_group_create_member(grp, node, port, MBR_DISCOVERED); | |
305 | } | |
306 | ||
307 | static void tipc_group_delete_member(struct tipc_group *grp, | |
308 | struct tipc_member *m) | |
309 | { | |
310 | rb_erase(&m->tree_node, &grp->members); | |
311 | grp->member_cnt--; | |
312 | ||
313 | /* Check if we were waiting for replicast ack from this member */ | |
314 | if (grp->bc_ackers && less(m->bc_acked, grp->bc_snd_nxt - 1)) | |
315 | grp->bc_ackers--; | |
316 | ||
317 | list_del_init(&m->list); | |
318 | list_del_init(&m->small_win); | |
319 | tipc_group_decr_active(grp, m); | |
320 | ||
321 | /* If last member on a node, remove node from dest list */ | |
322 | if (!tipc_group_find_node(grp, m->node)) | |
323 | tipc_nlist_del(&grp->dests, m->node); | |
324 | ||
325 | kfree(m); | |
326 | } | |
327 | ||
328 | struct tipc_nlist *tipc_group_dests(struct tipc_group *grp) | |
329 | { | |
330 | return &grp->dests; | |
331 | } | |
332 | ||
333 | void tipc_group_self(struct tipc_group *grp, struct tipc_name_seq *seq, | |
334 | int *scope) | |
335 | { | |
336 | seq->type = grp->type; | |
337 | seq->lower = grp->instance; | |
338 | seq->upper = grp->instance; | |
339 | *scope = grp->scope; | |
340 | } | |
341 | ||
342 | void tipc_group_update_member(struct tipc_member *m, int len) | |
343 | { | |
344 | struct tipc_group *grp = m->group; | |
345 | struct tipc_member *_m, *tmp; | |
346 | ||
347 | if (!tipc_group_is_receiver(m)) | |
348 | return; | |
349 | ||
350 | m->window -= len; | |
351 | ||
352 | if (m->window >= ADV_IDLE) | |
353 | return; | |
354 | ||
355 | list_del_init(&m->small_win); | |
356 | ||
357 | /* Sort member into small_window members' list */ | |
358 | list_for_each_entry_safe(_m, tmp, &grp->small_win, small_win) { | |
359 | if (_m->window > m->window) | |
360 | break; | |
361 | } | |
362 | list_add_tail(&m->small_win, &_m->small_win); | |
363 | } | |
364 | ||
365 | void tipc_group_update_bc_members(struct tipc_group *grp, int len, bool ack) | |
366 | { | |
367 | u16 prev = grp->bc_snd_nxt - 1; | |
368 | struct tipc_member *m; | |
369 | struct rb_node *n; | |
370 | u16 ackers = 0; | |
371 | ||
372 | for (n = rb_first(&grp->members); n; n = rb_next(n)) { | |
373 | m = container_of(n, struct tipc_member, tree_node); | |
374 | if (tipc_group_is_receiver(m)) { | |
375 | tipc_group_update_member(m, len); | |
376 | m->bc_acked = prev; | |
377 | ackers++; | |
378 | } | |
379 | } | |
380 | ||
381 | /* Mark number of acknowledges to expect, if any */ | |
382 | if (ack) | |
383 | grp->bc_ackers = ackers; | |
384 | grp->bc_snd_nxt++; | |
385 | } | |
386 | ||
387 | bool 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; | |
411 | if (state == MBR_ACTIVE && adv == ADV_ACTIVE) | |
412 | return true; | |
413 | if (state == MBR_PENDING && adv == ADV_IDLE) | |
414 | return true; | |
415 | 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 | ||
421 | bool tipc_group_bc_cong(struct tipc_group *grp, int len) | |
422 | { | |
423 | struct tipc_member *m = NULL; | |
424 | ||
425 | /* If prev bcast was replicast, reject until all receivers have acked */ | |
426 | if (grp->bc_ackers) | |
427 | return true; | |
428 | ||
429 | if (list_empty(&grp->small_win)) | |
430 | return false; | |
431 | ||
432 | m = list_first_entry(&grp->small_win, struct tipc_member, small_win); | |
433 | if (m->window >= len) | |
434 | return false; | |
435 | ||
436 | return tipc_group_cong(grp, m->node, m->port, len, &m); | |
437 | } | |
438 | ||
439 | /* tipc_group_sort_msg() - sort msg into queue by bcast sequence number | |
440 | */ | |
441 | static 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 | ||
448 | /* Bcast/mcast may be bypassed by ucast or other bcast, - sort it in */ | |
449 | 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 | ||
463 | /* tipc_group_filter_msg() - determine if we should accept arriving message | |
464 | */ | |
465 | void 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); | |
469 | bool ack, deliver, update, leave = false; | |
470 | struct sk_buff_head *defq; | |
471 | struct tipc_member *m; | |
472 | struct tipc_msg *hdr; | |
473 | u32 node, port; | |
474 | int mtyp, blks; | |
475 | ||
476 | if (!skb) | |
477 | return; | |
478 | ||
479 | hdr = buf_msg(skb); | |
480 | 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_sender(m)) | |
488 | goto drop; | |
489 | ||
490 | if (less(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt)) | |
491 | goto drop; | |
492 | ||
493 | TIPC_SKB_CB(skb)->orig_member = m->instance; | |
494 | defq = &m->deferredq; | |
495 | tipc_group_sort_msg(skb, defq); | |
496 | ||
497 | while ((skb = skb_peek(defq))) { | |
498 | hdr = buf_msg(skb); | |
499 | mtyp = msg_type(hdr); | |
500 | blks = msg_blocks(hdr); | |
501 | deliver = true; | |
502 | ack = false; | |
503 | update = false; | |
504 | ||
505 | if (more(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt)) | |
506 | break; | |
507 | ||
508 | /* Decide what to do with message */ | |
509 | switch (mtyp) { | |
510 | case TIPC_GRP_MCAST_MSG: | |
511 | if (msg_nameinst(hdr) != grp->instance) { | |
512 | update = true; | |
513 | deliver = false; | |
514 | } | |
515 | /* Fall thru */ | |
516 | case TIPC_GRP_BCAST_MSG: | |
517 | m->bc_rcv_nxt++; | |
518 | ack = msg_grp_bc_ack_req(hdr); | |
519 | break; | |
520 | case TIPC_GRP_UCAST_MSG: | |
521 | break; | |
522 | case TIPC_GRP_MEMBER_EVT: | |
523 | if (m->state == MBR_LEAVING) | |
524 | leave = true; | |
525 | if (!grp->events) | |
526 | deliver = false; | |
527 | break; | |
528 | default: | |
529 | break; | |
530 | } | |
531 | ||
532 | /* Execute decisions */ | |
533 | __skb_dequeue(defq); | |
534 | if (deliver) | |
535 | __skb_queue_tail(inputq, skb); | |
536 | else | |
537 | kfree_skb(skb); | |
538 | ||
539 | if (ack) | |
540 | tipc_group_proto_xmit(grp, m, GRP_ACK_MSG, xmitq); | |
541 | ||
542 | if (leave) { | |
543 | __skb_queue_purge(defq); | |
544 | tipc_group_delete_member(grp, m); | |
545 | break; | |
546 | } | |
547 | if (!update) | |
548 | continue; | |
549 | ||
550 | tipc_group_update_rcv_win(grp, blks, node, port, xmitq); | |
551 | } | |
552 | return; | |
553 | drop: | |
554 | kfree_skb(skb); | |
555 | } | |
556 | ||
557 | void tipc_group_update_rcv_win(struct tipc_group *grp, int blks, u32 node, | |
558 | u32 port, struct sk_buff_head *xmitq) | |
559 | { | |
560 | 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, *pm; | |
565 | ||
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: | |
574 | /* 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); | |
598 | break; | |
599 | 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 | grp->active_cnt--; | |
608 | list_del_init(&m->list); | |
609 | if (list_empty(&grp->pending)) | |
610 | return; | |
611 | ||
612 | /* Set oldest pending member to active and advertise */ | |
613 | pm = list_first_entry(&grp->pending, struct tipc_member, list); | |
614 | pm->state = MBR_ACTIVE; | |
615 | list_move_tail(&pm->list, &grp->active); | |
616 | grp->active_cnt++; | |
617 | tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq); | |
618 | break; | |
619 | case MBR_RECLAIMING: | |
620 | case MBR_DISCOVERED: | |
621 | case MBR_JOINING: | |
622 | case MBR_LEAVING: | |
623 | default: | |
624 | break; | |
625 | } | |
626 | } | |
627 | ||
628 | static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m, | |
629 | int mtyp, struct sk_buff_head *xmitq) | |
630 | { | |
631 | struct tipc_msg *hdr; | |
632 | struct sk_buff *skb; | |
633 | int adv = 0; | |
634 | ||
635 | skb = tipc_msg_create(GROUP_PROTOCOL, mtyp, INT_H_SIZE, 0, | |
636 | m->node, tipc_own_addr(grp->net), | |
637 | m->port, grp->portid, 0); | |
638 | if (!skb) | |
639 | return; | |
640 | ||
641 | if (m->state == MBR_ACTIVE) | |
642 | adv = ADV_ACTIVE - m->advertised; | |
643 | else if (m->state == MBR_JOINED || m->state == MBR_PENDING) | |
644 | adv = ADV_IDLE - m->advertised; | |
645 | ||
646 | hdr = buf_msg(skb); | |
647 | ||
648 | if (mtyp == GRP_JOIN_MSG) { | |
649 | msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt); | |
650 | msg_set_adv_win(hdr, adv); | |
651 | m->advertised += adv; | |
652 | } else if (mtyp == GRP_LEAVE_MSG) { | |
653 | msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt); | |
654 | } else if (mtyp == GRP_ADV_MSG) { | |
655 | msg_set_adv_win(hdr, adv); | |
656 | m->advertised += adv; | |
657 | } else if (mtyp == GRP_ACK_MSG) { | |
658 | msg_set_grp_bc_acked(hdr, m->bc_rcv_nxt); | |
659 | } else if (mtyp == GRP_REMIT_MSG) { | |
660 | msg_set_grp_remitted(hdr, m->window); | |
661 | } | |
662 | msg_set_dest_droppable(hdr, true); | |
663 | __skb_queue_tail(xmitq, skb); | |
664 | } | |
665 | ||
666 | void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup, | |
667 | struct tipc_msg *hdr, struct sk_buff_head *inputq, | |
668 | struct sk_buff_head *xmitq) | |
669 | { | |
670 | u32 node = msg_orignode(hdr); | |
671 | u32 port = msg_origport(hdr); | |
672 | struct tipc_member *m, *pm; | |
673 | struct tipc_msg *ehdr; | |
674 | u16 remitted, in_flight; | |
675 | ||
676 | if (!grp) | |
677 | return; | |
678 | ||
679 | m = tipc_group_find_member(grp, node, port); | |
680 | ||
681 | switch (msg_type(hdr)) { | |
682 | case GRP_JOIN_MSG: | |
683 | if (!m) | |
684 | m = tipc_group_create_member(grp, node, port, | |
685 | MBR_QUARANTINED); | |
686 | if (!m) | |
687 | return; | |
688 | m->bc_syncpt = msg_grp_bc_syncpt(hdr); | |
689 | m->bc_rcv_nxt = m->bc_syncpt; | |
690 | m->window += msg_adv_win(hdr); | |
691 | ||
692 | /* Wait until PUBLISH event is received */ | |
693 | if (m->state == MBR_DISCOVERED) { | |
694 | m->state = MBR_JOINING; | |
695 | } else if (m->state == MBR_PUBLISHED) { | |
696 | m->state = MBR_JOINED; | |
697 | *usr_wakeup = true; | |
698 | m->usr_pending = false; | |
699 | tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq); | |
700 | ehdr = buf_msg(m->event_msg); | |
701 | msg_set_grp_bc_seqno(ehdr, m->bc_syncpt); | |
702 | __skb_queue_tail(inputq, m->event_msg); | |
703 | } | |
704 | list_del_init(&m->small_win); | |
705 | tipc_group_update_member(m, 0); | |
706 | return; | |
707 | case GRP_LEAVE_MSG: | |
708 | if (!m) | |
709 | return; | |
710 | m->bc_syncpt = msg_grp_bc_syncpt(hdr); | |
711 | list_del_init(&m->list); | |
712 | list_del_init(&m->small_win); | |
713 | *usr_wakeup = true; | |
714 | ||
715 | /* Wait until WITHDRAW event is received */ | |
716 | if (m->state != MBR_LEAVING) { | |
717 | tipc_group_decr_active(grp, m); | |
718 | m->state = MBR_LEAVING; | |
719 | return; | |
720 | } | |
721 | /* Otherwise deliver already received WITHDRAW event */ | |
722 | ehdr = buf_msg(m->event_msg); | |
723 | msg_set_grp_bc_seqno(ehdr, m->bc_syncpt); | |
724 | __skb_queue_tail(inputq, m->event_msg); | |
725 | return; | |
726 | case GRP_ADV_MSG: | |
727 | if (!m) | |
728 | return; | |
729 | m->window += msg_adv_win(hdr); | |
730 | *usr_wakeup = m->usr_pending; | |
731 | m->usr_pending = false; | |
732 | list_del_init(&m->small_win); | |
733 | return; | |
734 | case GRP_ACK_MSG: | |
735 | if (!m) | |
736 | return; | |
737 | m->bc_acked = msg_grp_bc_acked(hdr); | |
738 | if (--grp->bc_ackers) | |
739 | break; | |
740 | *usr_wakeup = true; | |
741 | m->usr_pending = false; | |
742 | return; | |
743 | case GRP_RECLAIM_MSG: | |
744 | if (!m) | |
745 | return; | |
746 | *usr_wakeup = m->usr_pending; | |
747 | m->usr_pending = false; | |
748 | tipc_group_proto_xmit(grp, m, GRP_REMIT_MSG, xmitq); | |
749 | m->window = ADV_IDLE; | |
750 | return; | |
751 | case GRP_REMIT_MSG: | |
752 | if (!m || m->state != MBR_RECLAIMING) | |
753 | return; | |
754 | ||
755 | remitted = msg_grp_remitted(hdr); | |
756 | ||
757 | /* Messages preceding the REMIT still in receive queue */ | |
758 | if (m->advertised > remitted) { | |
759 | m->state = MBR_REMITTED; | |
760 | in_flight = m->advertised - remitted; | |
761 | m->advertised = ADV_IDLE + in_flight; | |
762 | return; | |
763 | } | |
764 | /* All messages preceding the REMIT have been read */ | |
765 | if (m->advertised <= remitted) { | |
766 | m->state = MBR_JOINED; | |
767 | in_flight = 0; | |
768 | } | |
769 | /* ..and the REMIT overtaken by more messages => re-advertise */ | |
770 | if (m->advertised < remitted) | |
771 | tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq); | |
772 | ||
773 | m->advertised = ADV_IDLE + in_flight; | |
774 | grp->active_cnt--; | |
775 | list_del_init(&m->list); | |
776 | ||
777 | /* Set oldest pending member to active and advertise */ | |
778 | if (list_empty(&grp->pending)) | |
779 | return; | |
780 | pm = list_first_entry(&grp->pending, struct tipc_member, list); | |
781 | pm->state = MBR_ACTIVE; | |
782 | list_move_tail(&pm->list, &grp->active); | |
783 | grp->active_cnt++; | |
784 | if (pm->advertised <= (ADV_ACTIVE * 3 / 4)) | |
785 | tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq); | |
786 | return; | |
787 | default: | |
788 | pr_warn("Received unknown GROUP_PROTO message\n"); | |
789 | } | |
790 | } | |
791 | ||
792 | /* tipc_group_member_evt() - receive and handle a member up/down event | |
793 | */ | |
794 | void tipc_group_member_evt(struct tipc_group *grp, | |
795 | bool *usr_wakeup, | |
796 | int *sk_rcvbuf, | |
797 | struct sk_buff *skb, | |
798 | struct sk_buff_head *inputq, | |
799 | struct sk_buff_head *xmitq) | |
800 | { | |
801 | struct tipc_msg *hdr = buf_msg(skb); | |
802 | struct tipc_event *evt = (void *)msg_data(hdr); | |
803 | u32 instance = evt->found_lower; | |
804 | u32 node = evt->port.node; | |
805 | u32 port = evt->port.ref; | |
806 | int event = evt->event; | |
807 | struct tipc_member *m; | |
808 | struct net *net; | |
809 | bool node_up; | |
810 | u32 self; | |
811 | ||
812 | if (!grp) | |
813 | goto drop; | |
814 | ||
815 | net = grp->net; | |
816 | self = tipc_own_addr(net); | |
817 | if (!grp->loopback && node == self && port == grp->portid) | |
818 | goto drop; | |
819 | ||
820 | /* Convert message before delivery to user */ | |
821 | msg_set_hdr_sz(hdr, GROUP_H_SIZE); | |
822 | msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE); | |
823 | msg_set_type(hdr, TIPC_GRP_MEMBER_EVT); | |
824 | msg_set_origport(hdr, port); | |
825 | msg_set_orignode(hdr, node); | |
826 | msg_set_nametype(hdr, grp->type); | |
827 | msg_set_grp_evt(hdr, event); | |
828 | ||
829 | m = tipc_group_find_member(grp, node, port); | |
830 | ||
831 | if (event == TIPC_PUBLISHED) { | |
832 | if (!m) | |
833 | m = tipc_group_create_member(grp, node, port, | |
834 | MBR_DISCOVERED); | |
835 | if (!m) | |
836 | goto drop; | |
837 | ||
838 | /* Hold back event if JOIN message not yet received */ | |
839 | if (m->state == MBR_DISCOVERED) { | |
840 | m->event_msg = skb; | |
841 | m->state = MBR_PUBLISHED; | |
842 | } else { | |
843 | msg_set_grp_bc_seqno(hdr, m->bc_syncpt); | |
844 | __skb_queue_tail(inputq, skb); | |
845 | m->state = MBR_JOINED; | |
846 | *usr_wakeup = true; | |
847 | m->usr_pending = false; | |
848 | } | |
849 | m->instance = instance; | |
850 | TIPC_SKB_CB(skb)->orig_member = m->instance; | |
851 | tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, xmitq); | |
852 | tipc_group_update_member(m, 0); | |
853 | } else if (event == TIPC_WITHDRAWN) { | |
854 | if (!m) | |
855 | goto drop; | |
856 | ||
857 | TIPC_SKB_CB(skb)->orig_member = m->instance; | |
858 | ||
859 | *usr_wakeup = true; | |
860 | m->usr_pending = false; | |
861 | node_up = tipc_node_is_up(net, node); | |
862 | m->event_msg = NULL; | |
863 | ||
864 | if (node_up) { | |
865 | /* Hold back event if a LEAVE msg should be expected */ | |
866 | if (m->state != MBR_LEAVING) { | |
867 | m->event_msg = skb; | |
868 | tipc_group_decr_active(grp, m); | |
869 | m->state = MBR_LEAVING; | |
870 | } else { | |
871 | msg_set_grp_bc_seqno(hdr, m->bc_syncpt); | |
872 | __skb_queue_tail(inputq, skb); | |
873 | } | |
874 | } else { | |
875 | if (m->state != MBR_LEAVING) { | |
876 | tipc_group_decr_active(grp, m); | |
877 | m->state = MBR_LEAVING; | |
878 | msg_set_grp_bc_seqno(hdr, m->bc_rcv_nxt); | |
879 | } else { | |
880 | msg_set_grp_bc_seqno(hdr, m->bc_syncpt); | |
881 | } | |
882 | __skb_queue_tail(inputq, skb); | |
883 | } | |
884 | list_del_init(&m->list); | |
885 | list_del_init(&m->small_win); | |
886 | } | |
887 | *sk_rcvbuf = tipc_group_rcvbuf_limit(grp); | |
888 | return; | |
889 | drop: | |
890 | kfree_skb(skb); | |
891 | } |