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