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 { |
| 52 | MBR_QUARANTINED, |
| 53 | MBR_DISCOVERED, |
| 54 | MBR_JOINING, |
| 55 | MBR_PUBLISHED, |
| 56 | MBR_JOINED, |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 57 | MBR_PENDING, |
| 58 | MBR_ACTIVE, |
| 59 | MBR_RECLAIMING, |
| 60 | MBR_REMITTED, |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 61 | MBR_LEAVING |
| 62 | }; |
| 63 | |
| 64 | struct tipc_member { |
| 65 | struct rb_node tree_node; |
| 66 | struct list_head list; |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 67 | struct list_head small_win; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 68 | struct sk_buff *event_msg; |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 69 | struct sk_buff_head deferredq; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 70 | struct tipc_group *group; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 71 | u32 node; |
| 72 | u32 port; |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 73 | u32 instance; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 74 | enum mbr_state state; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 75 | u16 advertised; |
| 76 | u16 window; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 77 | u16 bc_rcv_nxt; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 78 | u16 bc_syncpt; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 79 | u16 bc_acked; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 80 | bool usr_pending; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct tipc_group { |
| 84 | struct rb_root members; |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 85 | struct list_head small_win; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 86 | struct list_head pending; |
| 87 | struct list_head active; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 88 | 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 Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 97 | u16 active_cnt; |
| 98 | u16 max_active; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 99 | u16 bc_snd_nxt; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 100 | u16 bc_ackers; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 101 | bool loopback; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 102 | bool events; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m, |
| 106 | int mtyp, struct sk_buff_head *xmitq); |
| 107 | |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 108 | static void tipc_group_decr_active(struct tipc_group *grp, |
| 109 | struct tipc_member *m) |
| 110 | { |
Jon Maloy | f9c935d | 2017-12-29 19:48:02 +0100 | [diff] [blame] | 111 | if (m->state == MBR_ACTIVE || m->state == MBR_RECLAIMING || |
| 112 | m->state == MBR_REMITTED) |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 113 | grp->active_cnt--; |
| 114 | } |
| 115 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 116 | static int tipc_group_rcvbuf_limit(struct tipc_group *grp) |
| 117 | { |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 118 | int max_active, active_pool, idle_pool; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 119 | int mcnt = grp->member_cnt + 1; |
| 120 | |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 121 | /* 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 Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 130 | /* Scale to bytes, considering worst-case truesize/msgsize ratio */ |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 131 | return (active_pool + idle_pool) * FLOWCTL_BLK_SZ * 4; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 134 | u16 tipc_group_bc_snd_nxt(struct tipc_group *grp) |
| 135 | { |
| 136 | return grp->bc_snd_nxt; |
| 137 | } |
| 138 | |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 139 | static bool tipc_group_is_receiver(struct tipc_member *m) |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 140 | { |
| 141 | return m->state != MBR_QUARANTINED && m->state != MBR_LEAVING; |
| 142 | } |
| 143 | |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 144 | static bool tipc_group_is_sender(struct tipc_member *m) |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 145 | { |
| 146 | return m && m->state >= MBR_JOINED; |
| 147 | } |
| 148 | |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 149 | u32 tipc_group_exclude(struct tipc_group *grp) |
| 150 | { |
| 151 | if (!grp->loopback) |
| 152 | return grp->portid; |
| 153 | return 0; |
| 154 | } |
| 155 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 156 | int tipc_group_size(struct tipc_group *grp) |
| 157 | { |
| 158 | return grp->member_cnt; |
| 159 | } |
| 160 | |
| 161 | struct 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 Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 171 | INIT_LIST_HEAD(&grp->small_win); |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 172 | INIT_LIST_HEAD(&grp->active); |
| 173 | INIT_LIST_HEAD(&grp->pending); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 174 | 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 Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 182 | grp->events = mreq->flags & TIPC_GROUP_MEMBER_EVTS; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 183 | if (tipc_topsrv_kern_subscr(net, portid, type, 0, ~0, &grp->subid)) |
| 184 | return grp; |
| 185 | kfree(grp); |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | void 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 | |
| 208 | struct 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 Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 228 | static 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 Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 234 | if (m && tipc_group_is_receiver(m)) |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 235 | return m; |
| 236 | return NULL; |
| 237 | } |
| 238 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 239 | static 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 | |
| 253 | static 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 | |
| 277 | static 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 Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 287 | INIT_LIST_HEAD(&m->small_win); |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 288 | __skb_queue_head_init(&m->deferredq); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 289 | m->group = grp; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 290 | m->node = node; |
| 291 | m->port = port; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 292 | m->bc_acked = grp->bc_snd_nxt - 1; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 293 | 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 | |
| 300 | void 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 | |
| 305 | static 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 Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 310 | |
| 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 Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 315 | list_del_init(&m->list); |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 316 | list_del_init(&m->small_win); |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 317 | tipc_group_decr_active(grp, m); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 318 | |
| 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 | |
| 326 | struct tipc_nlist *tipc_group_dests(struct tipc_group *grp) |
| 327 | { |
| 328 | return &grp->dests; |
| 329 | } |
| 330 | |
| 331 | void 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 Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 340 | void tipc_group_update_member(struct tipc_member *m, int len) |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 341 | { |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 342 | struct tipc_group *grp = m->group; |
| 343 | struct tipc_member *_m, *tmp; |
| 344 | |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 345 | if (!tipc_group_is_receiver(m)) |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 346 | return; |
| 347 | |
| 348 | m->window -= len; |
| 349 | |
| 350 | if (m->window >= ADV_IDLE) |
| 351 | return; |
| 352 | |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 353 | list_del_init(&m->small_win); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 354 | |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 355 | /* Sort member into small_window members' list */ |
| 356 | list_for_each_entry_safe(_m, tmp, &grp->small_win, small_win) { |
Jon Maloy | d84d1b3 | 2018-01-04 15:20:45 +0100 | [diff] [blame] | 357 | if (_m->window > m->window) |
| 358 | break; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 359 | } |
Jon Maloy | d84d1b3 | 2018-01-04 15:20:45 +0100 | [diff] [blame] | 360 | list_add_tail(&m->small_win, &_m->small_win); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 363 | 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] | 364 | { |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 365 | u16 prev = grp->bc_snd_nxt - 1; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 366 | struct tipc_member *m; |
| 367 | struct rb_node *n; |
Jon Maloy | 0a3d805 | 2017-12-21 13:07:11 +0100 | [diff] [blame] | 368 | u16 ackers = 0; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 369 | |
| 370 | for (n = rb_first(&grp->members); n; n = rb_next(n)) { |
| 371 | m = container_of(n, struct tipc_member, tree_node); |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 372 | if (tipc_group_is_receiver(m)) { |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 373 | tipc_group_update_member(m, len); |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 374 | m->bc_acked = prev; |
Jon Maloy | 0a3d805 | 2017-12-21 13:07:11 +0100 | [diff] [blame] | 375 | ackers++; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 376 | } |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 377 | } |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 378 | |
| 379 | /* Mark number of acknowledges to expect, if any */ |
| 380 | if (ack) |
Jon Maloy | 0a3d805 | 2017-12-21 13:07:11 +0100 | [diff] [blame] | 381 | grp->bc_ackers = ackers; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 382 | grp->bc_snd_nxt++; |
| 383 | } |
| 384 | |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 385 | bool 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 Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 409 | if (state == MBR_ACTIVE && adv == ADV_ACTIVE) |
| 410 | return true; |
| 411 | if (state == MBR_PENDING && adv == ADV_IDLE) |
| 412 | return true; |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 413 | 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 Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 419 | bool tipc_group_bc_cong(struct tipc_group *grp, int len) |
| 420 | { |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 421 | struct tipc_member *m = NULL; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 422 | |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 423 | /* If prev bcast was replicast, reject until all receivers have acked */ |
| 424 | if (grp->bc_ackers) |
| 425 | return true; |
| 426 | |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 427 | if (list_empty(&grp->small_win)) |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 428 | return false; |
| 429 | |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 430 | m = list_first_entry(&grp->small_win, struct tipc_member, small_win); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 431 | if (m->window >= len) |
| 432 | return false; |
| 433 | |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 434 | return tipc_group_cong(grp, m->node, m->port, len, &m); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 437 | /* tipc_group_sort_msg() - sort msg into queue by bcast sequence number |
| 438 | */ |
| 439 | static 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 Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 446 | /* 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] | 447 | 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 Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 461 | /* tipc_group_filter_msg() - determine if we should accept arriving message |
| 462 | */ |
| 463 | void 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 Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 467 | bool ack, deliver, update, leave = false; |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 468 | struct sk_buff_head *defq; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 469 | struct tipc_member *m; |
| 470 | struct tipc_msg *hdr; |
| 471 | u32 node, port; |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 472 | int mtyp, blks; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 473 | |
| 474 | if (!skb) |
| 475 | return; |
| 476 | |
| 477 | hdr = buf_msg(skb); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 478 | 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 Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 485 | if (!tipc_group_is_sender(m)) |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 486 | goto drop; |
| 487 | |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 488 | if (less(msg_grp_bc_seqno(hdr), m->bc_rcv_nxt)) |
| 489 | goto drop; |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 490 | |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 491 | TIPC_SKB_CB(skb)->orig_member = m->instance; |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 492 | defq = &m->deferredq; |
| 493 | tipc_group_sort_msg(skb, defq); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 494 | |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 495 | while ((skb = skb_peek(defq))) { |
| 496 | hdr = buf_msg(skb); |
| 497 | mtyp = msg_type(hdr); |
Jon Maloy | 2e724dc | 2017-11-27 20:13:39 +0100 | [diff] [blame] | 498 | blks = msg_blocks(hdr); |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 499 | deliver = true; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 500 | ack = false; |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 501 | 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 Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 516 | ack = msg_grp_bc_ack_req(hdr); |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 517 | break; |
| 518 | case TIPC_GRP_UCAST_MSG: |
| 519 | break; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 520 | case TIPC_GRP_MEMBER_EVT: |
| 521 | if (m->state == MBR_LEAVING) |
| 522 | leave = true; |
| 523 | if (!grp->events) |
| 524 | deliver = false; |
| 525 | break; |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 526 | 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 Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 537 | if (ack) |
| 538 | tipc_group_proto_xmit(grp, m, GRP_ACK_MSG, xmitq); |
| 539 | |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 540 | if (leave) { |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 541 | __skb_queue_purge(defq); |
Jon Maloy | e0e853a | 2017-11-20 21:43:03 +0100 | [diff] [blame] | 542 | tipc_group_delete_member(grp, m); |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 543 | break; |
| 544 | } |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 545 | if (!update) |
| 546 | continue; |
| 547 | |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 548 | tipc_group_update_rcv_win(grp, blks, node, port, xmitq); |
| 549 | } |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 550 | return; |
| 551 | drop: |
| 552 | kfree_skb(skb); |
| 553 | } |
| 554 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 555 | void tipc_group_update_rcv_win(struct tipc_group *grp, int blks, u32 node, |
| 556 | u32 port, struct sk_buff_head *xmitq) |
| 557 | { |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 558 | 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 Maloy | f9c935d | 2017-12-29 19:48:02 +0100 | [diff] [blame] | 562 | struct tipc_member *m, *rm, *pm; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 563 | |
| 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 Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 572 | /* Reclaim advertised space from least active member */ |
| 573 | if (!list_empty(active) && active_cnt >= reclaim_limit) { |
| 574 | rm = list_first_entry(active, struct tipc_member, list); |
| 575 | rm->state = MBR_RECLAIMING; |
Jon Maloy | 8d5dee2 | 2018-01-08 21:03:23 +0100 | [diff] [blame^] | 576 | list_del_init(&rm->list); |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 577 | tipc_group_proto_xmit(grp, rm, GRP_RECLAIM_MSG, xmitq); |
| 578 | } |
| 579 | /* If max active, become pending and wait for reclaimed space */ |
| 580 | if (active_cnt >= max_active) { |
| 581 | m->state = MBR_PENDING; |
| 582 | list_add_tail(&m->list, &grp->pending); |
| 583 | break; |
| 584 | } |
| 585 | /* Otherwise become active */ |
| 586 | m->state = MBR_ACTIVE; |
| 587 | list_add_tail(&m->list, &grp->active); |
| 588 | grp->active_cnt++; |
| 589 | /* Fall through */ |
| 590 | case MBR_ACTIVE: |
| 591 | if (!list_is_last(&m->list, &grp->active)) |
| 592 | list_move_tail(&m->list, &grp->active); |
| 593 | if (m->advertised > (ADV_ACTIVE * 3 / 4)) |
| 594 | break; |
| 595 | tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 596 | break; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 597 | case MBR_REMITTED: |
| 598 | if (m->advertised > ADV_IDLE) |
| 599 | break; |
| 600 | m->state = MBR_JOINED; |
Jon Maloy | 8d5dee2 | 2018-01-08 21:03:23 +0100 | [diff] [blame^] | 601 | grp->active_cnt--; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 602 | if (m->advertised < ADV_IDLE) { |
| 603 | pr_warn_ratelimited("Rcv unexpected msg after REMIT\n"); |
| 604 | tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq); |
| 605 | } |
Jon Maloy | 8d5dee2 | 2018-01-08 21:03:23 +0100 | [diff] [blame^] | 606 | |
Jon Maloy | f9c935d | 2017-12-29 19:48:02 +0100 | [diff] [blame] | 607 | if (list_empty(&grp->pending)) |
| 608 | return; |
| 609 | |
| 610 | /* Set oldest pending member to active and advertise */ |
| 611 | pm = list_first_entry(&grp->pending, struct tipc_member, list); |
| 612 | pm->state = MBR_ACTIVE; |
| 613 | list_move_tail(&pm->list, &grp->active); |
| 614 | grp->active_cnt++; |
| 615 | tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq); |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 616 | break; |
| 617 | case MBR_RECLAIMING: |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 618 | case MBR_DISCOVERED: |
| 619 | case MBR_JOINING: |
| 620 | case MBR_LEAVING: |
| 621 | default: |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 626 | static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m, |
| 627 | int mtyp, struct sk_buff_head *xmitq) |
| 628 | { |
| 629 | struct tipc_msg *hdr; |
| 630 | struct sk_buff *skb; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 631 | int adv = 0; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 632 | |
| 633 | skb = tipc_msg_create(GROUP_PROTOCOL, mtyp, INT_H_SIZE, 0, |
| 634 | m->node, tipc_own_addr(grp->net), |
| 635 | m->port, grp->portid, 0); |
| 636 | if (!skb) |
| 637 | return; |
| 638 | |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 639 | if (m->state == MBR_ACTIVE) |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 640 | adv = ADV_ACTIVE - m->advertised; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 641 | else if (m->state == MBR_JOINED || m->state == MBR_PENDING) |
| 642 | adv = ADV_IDLE - m->advertised; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 643 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 644 | hdr = buf_msg(skb); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 645 | |
| 646 | if (mtyp == GRP_JOIN_MSG) { |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 647 | msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 648 | msg_set_adv_win(hdr, adv); |
| 649 | m->advertised += adv; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 650 | } else if (mtyp == GRP_LEAVE_MSG) { |
| 651 | msg_set_grp_bc_syncpt(hdr, grp->bc_snd_nxt); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 652 | } else if (mtyp == GRP_ADV_MSG) { |
| 653 | msg_set_adv_win(hdr, adv); |
| 654 | m->advertised += adv; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 655 | } else if (mtyp == GRP_ACK_MSG) { |
| 656 | msg_set_grp_bc_acked(hdr, m->bc_rcv_nxt); |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 657 | } else if (mtyp == GRP_REMIT_MSG) { |
| 658 | msg_set_grp_remitted(hdr, m->window); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 659 | } |
Jon Maloy | 2348339 | 2017-12-18 17:34:16 +0100 | [diff] [blame] | 660 | msg_set_dest_droppable(hdr, true); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 661 | __skb_queue_tail(xmitq, skb); |
| 662 | } |
| 663 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 664 | void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup, |
| 665 | struct tipc_msg *hdr, struct sk_buff_head *inputq, |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 666 | struct sk_buff_head *xmitq) |
| 667 | { |
| 668 | u32 node = msg_orignode(hdr); |
| 669 | u32 port = msg_origport(hdr); |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 670 | struct tipc_member *m, *pm; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 671 | struct tipc_msg *ehdr; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 672 | u16 remitted, in_flight; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 673 | |
| 674 | if (!grp) |
| 675 | return; |
| 676 | |
| 677 | m = tipc_group_find_member(grp, node, port); |
| 678 | |
| 679 | switch (msg_type(hdr)) { |
| 680 | case GRP_JOIN_MSG: |
| 681 | if (!m) |
| 682 | m = tipc_group_create_member(grp, node, port, |
| 683 | MBR_QUARANTINED); |
| 684 | if (!m) |
| 685 | return; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 686 | m->bc_syncpt = msg_grp_bc_syncpt(hdr); |
| 687 | m->bc_rcv_nxt = m->bc_syncpt; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 688 | m->window += msg_adv_win(hdr); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 689 | |
| 690 | /* Wait until PUBLISH event is received */ |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 691 | if (m->state == MBR_DISCOVERED) { |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 692 | m->state = MBR_JOINING; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 693 | } else if (m->state == MBR_PUBLISHED) { |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 694 | m->state = MBR_JOINED; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 695 | *usr_wakeup = true; |
| 696 | m->usr_pending = false; |
| 697 | tipc_group_proto_xmit(grp, m, GRP_ADV_MSG, xmitq); |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 698 | ehdr = buf_msg(m->event_msg); |
| 699 | msg_set_grp_bc_seqno(ehdr, m->bc_syncpt); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 700 | __skb_queue_tail(inputq, m->event_msg); |
| 701 | } |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 702 | list_del_init(&m->small_win); |
Jon Maloy | bb25c38 | 2017-12-20 11:03:15 +0100 | [diff] [blame] | 703 | tipc_group_update_member(m, 0); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 704 | return; |
| 705 | case GRP_LEAVE_MSG: |
| 706 | if (!m) |
| 707 | return; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 708 | m->bc_syncpt = msg_grp_bc_syncpt(hdr); |
Jon Maloy | 3f42f5f | 2017-12-18 18:13:34 +0100 | [diff] [blame] | 709 | list_del_init(&m->list); |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 710 | list_del_init(&m->small_win); |
Jon Maloy | 3f42f5f | 2017-12-18 18:13:34 +0100 | [diff] [blame] | 711 | *usr_wakeup = true; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 712 | |
| 713 | /* Wait until WITHDRAW event is received */ |
| 714 | if (m->state != MBR_LEAVING) { |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 715 | tipc_group_decr_active(grp, m); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 716 | m->state = MBR_LEAVING; |
| 717 | return; |
| 718 | } |
| 719 | /* Otherwise deliver already received WITHDRAW event */ |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 720 | ehdr = buf_msg(m->event_msg); |
| 721 | msg_set_grp_bc_seqno(ehdr, m->bc_syncpt); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 722 | __skb_queue_tail(inputq, m->event_msg); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 723 | return; |
| 724 | case GRP_ADV_MSG: |
| 725 | if (!m) |
| 726 | return; |
| 727 | m->window += msg_adv_win(hdr); |
| 728 | *usr_wakeup = m->usr_pending; |
| 729 | m->usr_pending = false; |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 730 | list_del_init(&m->small_win); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 731 | return; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 732 | case GRP_ACK_MSG: |
| 733 | if (!m) |
| 734 | return; |
| 735 | m->bc_acked = msg_grp_bc_acked(hdr); |
| 736 | if (--grp->bc_ackers) |
| 737 | break; |
| 738 | *usr_wakeup = true; |
| 739 | m->usr_pending = false; |
| 740 | return; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 741 | case GRP_RECLAIM_MSG: |
| 742 | if (!m) |
| 743 | return; |
| 744 | *usr_wakeup = m->usr_pending; |
| 745 | m->usr_pending = false; |
| 746 | tipc_group_proto_xmit(grp, m, GRP_REMIT_MSG, xmitq); |
| 747 | m->window = ADV_IDLE; |
| 748 | return; |
| 749 | case GRP_REMIT_MSG: |
| 750 | if (!m || m->state != MBR_RECLAIMING) |
| 751 | return; |
| 752 | |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 753 | remitted = msg_grp_remitted(hdr); |
| 754 | |
| 755 | /* Messages preceding the REMIT still in receive queue */ |
| 756 | if (m->advertised > remitted) { |
| 757 | m->state = MBR_REMITTED; |
| 758 | in_flight = m->advertised - remitted; |
Jon Maloy | f9c935d | 2017-12-29 19:48:02 +0100 | [diff] [blame] | 759 | m->advertised = ADV_IDLE + in_flight; |
| 760 | return; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 761 | } |
Jon Maloy | 8d5dee2 | 2018-01-08 21:03:23 +0100 | [diff] [blame^] | 762 | /* This should never happen */ |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 763 | if (m->advertised < remitted) |
Jon Maloy | 8d5dee2 | 2018-01-08 21:03:23 +0100 | [diff] [blame^] | 764 | pr_warn_ratelimited("Unexpected REMIT msg\n"); |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 765 | |
Jon Maloy | 8d5dee2 | 2018-01-08 21:03:23 +0100 | [diff] [blame^] | 766 | /* All messages preceding the REMIT have been read */ |
| 767 | m->state = MBR_JOINED; |
Jon Maloy | f9c935d | 2017-12-29 19:48:02 +0100 | [diff] [blame] | 768 | grp->active_cnt--; |
Jon Maloy | 8d5dee2 | 2018-01-08 21:03:23 +0100 | [diff] [blame^] | 769 | m->advertised = ADV_IDLE; |
Jon Maloy | 04d7b57 | 2017-10-13 11:04:34 +0200 | [diff] [blame] | 770 | |
| 771 | /* Set oldest pending member to active and advertise */ |
| 772 | if (list_empty(&grp->pending)) |
| 773 | return; |
| 774 | pm = list_first_entry(&grp->pending, struct tipc_member, list); |
| 775 | pm->state = MBR_ACTIVE; |
| 776 | list_move_tail(&pm->list, &grp->active); |
| 777 | grp->active_cnt++; |
| 778 | if (pm->advertised <= (ADV_ACTIVE * 3 / 4)) |
| 779 | tipc_group_proto_xmit(grp, pm, GRP_ADV_MSG, xmitq); |
| 780 | return; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 781 | default: |
| 782 | pr_warn("Received unknown GROUP_PROTO message\n"); |
| 783 | } |
| 784 | } |
| 785 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 786 | /* tipc_group_member_evt() - receive and handle a member up/down event |
| 787 | */ |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 788 | void tipc_group_member_evt(struct tipc_group *grp, |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 789 | bool *usr_wakeup, |
| 790 | int *sk_rcvbuf, |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 791 | struct sk_buff *skb, |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 792 | struct sk_buff_head *inputq, |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 793 | struct sk_buff_head *xmitq) |
| 794 | { |
| 795 | struct tipc_msg *hdr = buf_msg(skb); |
| 796 | struct tipc_event *evt = (void *)msg_data(hdr); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 797 | u32 instance = evt->found_lower; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 798 | u32 node = evt->port.node; |
| 799 | u32 port = evt->port.ref; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 800 | int event = evt->event; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 801 | struct tipc_member *m; |
| 802 | struct net *net; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 803 | bool node_up; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 804 | u32 self; |
| 805 | |
| 806 | if (!grp) |
| 807 | goto drop; |
| 808 | |
| 809 | net = grp->net; |
| 810 | self = tipc_own_addr(net); |
| 811 | if (!grp->loopback && node == self && port == grp->portid) |
| 812 | goto drop; |
| 813 | |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 814 | /* Convert message before delivery to user */ |
| 815 | msg_set_hdr_sz(hdr, GROUP_H_SIZE); |
| 816 | msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE); |
| 817 | msg_set_type(hdr, TIPC_GRP_MEMBER_EVT); |
| 818 | msg_set_origport(hdr, port); |
| 819 | msg_set_orignode(hdr, node); |
| 820 | msg_set_nametype(hdr, grp->type); |
| 821 | msg_set_grp_evt(hdr, event); |
| 822 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 823 | m = tipc_group_find_member(grp, node, port); |
| 824 | |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 825 | if (event == TIPC_PUBLISHED) { |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 826 | if (!m) |
| 827 | m = tipc_group_create_member(grp, node, port, |
| 828 | MBR_DISCOVERED); |
| 829 | if (!m) |
| 830 | goto drop; |
| 831 | |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 832 | /* Hold back event if JOIN message not yet received */ |
| 833 | if (m->state == MBR_DISCOVERED) { |
| 834 | m->event_msg = skb; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 835 | m->state = MBR_PUBLISHED; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 836 | } else { |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 837 | msg_set_grp_bc_seqno(hdr, m->bc_syncpt); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 838 | __skb_queue_tail(inputq, skb); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 839 | m->state = MBR_JOINED; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 840 | *usr_wakeup = true; |
| 841 | m->usr_pending = false; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 842 | } |
| 843 | m->instance = instance; |
| 844 | TIPC_SKB_CB(skb)->orig_member = m->instance; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 845 | tipc_group_proto_xmit(grp, m, GRP_JOIN_MSG, xmitq); |
Jon Maloy | d84d1b3 | 2018-01-04 15:20:45 +0100 | [diff] [blame] | 846 | tipc_group_update_member(m, 0); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 847 | } else if (event == TIPC_WITHDRAWN) { |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 848 | if (!m) |
| 849 | goto drop; |
| 850 | |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 851 | TIPC_SKB_CB(skb)->orig_member = m->instance; |
| 852 | |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 853 | *usr_wakeup = true; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 854 | m->usr_pending = false; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 855 | node_up = tipc_node_is_up(net, node); |
Jon Maloy | 3a33a19 | 2017-12-21 14:36:34 +0100 | [diff] [blame] | 856 | m->event_msg = NULL; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 857 | |
Jon Maloy | 3a33a19 | 2017-12-21 14:36:34 +0100 | [diff] [blame] | 858 | if (node_up) { |
| 859 | /* Hold back event if a LEAVE msg should be expected */ |
| 860 | if (m->state != MBR_LEAVING) { |
| 861 | m->event_msg = skb; |
| 862 | tipc_group_decr_active(grp, m); |
| 863 | m->state = MBR_LEAVING; |
| 864 | } else { |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 865 | msg_set_grp_bc_seqno(hdr, m->bc_syncpt); |
Jon Maloy | 3a33a19 | 2017-12-21 14:36:34 +0100 | [diff] [blame] | 866 | __skb_queue_tail(inputq, skb); |
| 867 | } |
| 868 | } else { |
| 869 | if (m->state != MBR_LEAVING) { |
| 870 | tipc_group_decr_active(grp, m); |
| 871 | m->state = MBR_LEAVING; |
Jon Maloy | a3bada7 | 2017-10-13 11:04:33 +0200 | [diff] [blame] | 872 | msg_set_grp_bc_seqno(hdr, m->bc_rcv_nxt); |
Jon Maloy | 3a33a19 | 2017-12-21 14:36:34 +0100 | [diff] [blame] | 873 | } else { |
| 874 | msg_set_grp_bc_seqno(hdr, m->bc_syncpt); |
| 875 | } |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 876 | __skb_queue_tail(inputq, skb); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 877 | } |
Jon Maloy | 3f42f5f | 2017-12-18 18:13:34 +0100 | [diff] [blame] | 878 | list_del_init(&m->list); |
Jon Maloy | 38266ca | 2018-01-04 15:20:44 +0100 | [diff] [blame] | 879 | list_del_init(&m->small_win); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 880 | } |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 881 | *sk_rcvbuf = tipc_group_rcvbuf_limit(grp); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 882 | return; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 883 | drop: |
| 884 | kfree_skb(skb); |
| 885 | } |