David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 1 | /* Peer event handling, typically ICMP messages. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/net.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/errqueue.h> |
| 16 | #include <linux/udp.h> |
| 17 | #include <linux/in.h> |
| 18 | #include <linux/in6.h> |
| 19 | #include <linux/icmp.h> |
| 20 | #include <net/sock.h> |
| 21 | #include <net/af_rxrpc.h> |
| 22 | #include <net/ip.h> |
| 23 | #include "ar-internal.h" |
| 24 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 25 | static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *); |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 26 | static void rxrpc_distribute_error(struct rxrpc_peer *, int, |
| 27 | enum rxrpc_call_completion); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 28 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 29 | /* |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 30 | * Find the peer associated with an ICMP packet. |
| 31 | */ |
| 32 | static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local, |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 33 | const struct sk_buff *skb, |
| 34 | struct sockaddr_rxrpc *srx) |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 35 | { |
| 36 | struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 37 | |
| 38 | _enter(""); |
| 39 | |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 40 | memset(srx, 0, sizeof(*srx)); |
| 41 | srx->transport_type = local->srx.transport_type; |
| 42 | srx->transport_len = local->srx.transport_len; |
| 43 | srx->transport.family = local->srx.transport.family; |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 44 | |
| 45 | /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice |
| 46 | * versa? |
| 47 | */ |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 48 | switch (srx->transport.family) { |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 49 | case AF_INET: |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 50 | srx->transport.sin.sin_port = serr->port; |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 51 | switch (serr->ee.ee_origin) { |
| 52 | case SO_EE_ORIGIN_ICMP: |
| 53 | _net("Rx ICMP"); |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 54 | memcpy(&srx->transport.sin.sin_addr, |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 55 | skb_network_header(skb) + serr->addr_offset, |
| 56 | sizeof(struct in_addr)); |
| 57 | break; |
| 58 | case SO_EE_ORIGIN_ICMP6: |
| 59 | _net("Rx ICMP6 on v4 sock"); |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 60 | memcpy(&srx->transport.sin.sin_addr, |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 61 | skb_network_header(skb) + serr->addr_offset + 12, |
| 62 | sizeof(struct in_addr)); |
| 63 | break; |
| 64 | default: |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 65 | memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr, |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 66 | sizeof(struct in_addr)); |
| 67 | break; |
| 68 | } |
| 69 | break; |
| 70 | |
David Howells | d191274 | 2016-09-17 07:26:01 +0100 | [diff] [blame] | 71 | #ifdef CONFIG_AF_RXRPC_IPV6 |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 72 | case AF_INET6: |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 73 | srx->transport.sin6.sin6_port = serr->port; |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 74 | switch (serr->ee.ee_origin) { |
| 75 | case SO_EE_ORIGIN_ICMP6: |
| 76 | _net("Rx ICMP6"); |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 77 | memcpy(&srx->transport.sin6.sin6_addr, |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 78 | skb_network_header(skb) + serr->addr_offset, |
| 79 | sizeof(struct in6_addr)); |
| 80 | break; |
| 81 | case SO_EE_ORIGIN_ICMP: |
| 82 | _net("Rx ICMP on v6 sock"); |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 83 | srx->transport.sin6.sin6_addr.s6_addr32[0] = 0; |
| 84 | srx->transport.sin6.sin6_addr.s6_addr32[1] = 0; |
| 85 | srx->transport.sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); |
| 86 | memcpy(srx->transport.sin6.sin6_addr.s6_addr + 12, |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 87 | skb_network_header(skb) + serr->addr_offset, |
| 88 | sizeof(struct in_addr)); |
| 89 | break; |
| 90 | default: |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 91 | memcpy(&srx->transport.sin6.sin6_addr, |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 92 | &ipv6_hdr(skb)->saddr, |
| 93 | sizeof(struct in6_addr)); |
| 94 | break; |
| 95 | } |
| 96 | break; |
David Howells | d191274 | 2016-09-17 07:26:01 +0100 | [diff] [blame] | 97 | #endif |
David Howells | 75b54cb | 2016-09-13 08:49:05 +0100 | [diff] [blame] | 98 | |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 99 | default: |
| 100 | BUG(); |
| 101 | } |
| 102 | |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 103 | return rxrpc_lookup_peer_rcu(local, srx); |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* |
David Howells | 1a70c05 | 2016-04-04 14:00:33 +0100 | [diff] [blame] | 107 | * Handle an MTU/fragmentation problem. |
| 108 | */ |
| 109 | static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr) |
| 110 | { |
| 111 | u32 mtu = serr->ee.ee_info; |
| 112 | |
| 113 | _net("Rx ICMP Fragmentation Needed (%d)", mtu); |
| 114 | |
| 115 | /* wind down the local interface MTU */ |
| 116 | if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) { |
| 117 | peer->if_mtu = mtu; |
| 118 | _net("I/F MTU %u", mtu); |
| 119 | } |
| 120 | |
| 121 | if (mtu == 0) { |
| 122 | /* they didn't give us a size, estimate one */ |
| 123 | mtu = peer->if_mtu; |
| 124 | if (mtu > 1500) { |
| 125 | mtu >>= 1; |
| 126 | if (mtu < 1500) |
| 127 | mtu = 1500; |
| 128 | } else { |
| 129 | mtu -= 100; |
| 130 | if (mtu < peer->hdrsize) |
| 131 | mtu = peer->hdrsize + 4; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (mtu < peer->mtu) { |
| 136 | spin_lock_bh(&peer->lock); |
| 137 | peer->mtu = mtu; |
| 138 | peer->maxdata = peer->mtu - peer->hdrsize; |
| 139 | spin_unlock_bh(&peer->lock); |
| 140 | _net("Net MTU %u (maxdata %u)", |
| 141 | peer->mtu, peer->maxdata); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 146 | * Handle an error received on the local endpoint. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 147 | */ |
David Howells | abe89ef | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 148 | void rxrpc_error_report(struct sock *sk) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 149 | { |
| 150 | struct sock_exterr_skb *serr; |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 151 | struct sockaddr_rxrpc srx; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 152 | struct rxrpc_local *local = sk->sk_user_data; |
| 153 | struct rxrpc_peer *peer; |
| 154 | struct sk_buff *skb; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 155 | |
| 156 | _enter("%p{%d}", sk, local->debug_id); |
| 157 | |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 158 | skb = sock_dequeue_err_skb(sk); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 159 | if (!skb) { |
| 160 | _leave("UDP socket errqueue empty"); |
| 161 | return; |
| 162 | } |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 163 | rxrpc_new_skb(skb, rxrpc_skb_rx_received); |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 164 | serr = SKB_EXT_ERR(skb); |
| 165 | if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) { |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 166 | _leave("UDP empty message"); |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 167 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 168 | return; |
| 169 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 170 | |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 171 | rcu_read_lock(); |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 172 | peer = rxrpc_lookup_peer_icmp_rcu(local, skb, &srx); |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 173 | if (peer && !rxrpc_get_peer_maybe(peer)) |
| 174 | peer = NULL; |
| 175 | if (!peer) { |
| 176 | rcu_read_unlock(); |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 177 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 178 | _leave(" [no peer]"); |
| 179 | return; |
| 180 | } |
| 181 | |
David Howells | 494337c | 2018-05-10 23:26:01 +0100 | [diff] [blame] | 182 | trace_rxrpc_rx_icmp(peer, &serr->ee, &srx); |
| 183 | |
David Howells | 1a70c05 | 2016-04-04 14:00:33 +0100 | [diff] [blame] | 184 | if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP && |
| 185 | serr->ee.ee_type == ICMP_DEST_UNREACH && |
| 186 | serr->ee.ee_code == ICMP_FRAG_NEEDED)) { |
| 187 | rxrpc_adjust_mtu(peer, serr); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 188 | rcu_read_unlock(); |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 189 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 190 | rxrpc_put_peer(peer); |
| 191 | _leave(" [MTU update]"); |
| 192 | return; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 193 | } |
| 194 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 195 | rxrpc_store_error(peer, serr); |
David Howells | be6e670 | 2016-04-04 14:00:32 +0100 | [diff] [blame] | 196 | rcu_read_unlock(); |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 197 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 198 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 199 | _leave(""); |
| 200 | } |
| 201 | |
| 202 | /* |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 203 | * Map an error report to error codes on the peer record. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 204 | */ |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 205 | static void rxrpc_store_error(struct rxrpc_peer *peer, |
| 206 | struct sock_exterr_skb *serr) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 207 | { |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 208 | enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 209 | struct sock_extended_err *ee; |
David S. Miller | c9d10c4 | 2011-05-19 18:37:11 -0400 | [diff] [blame] | 210 | int err; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 211 | |
| 212 | _enter(""); |
| 213 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 214 | ee = &serr->ee; |
| 215 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 216 | err = ee->ee_errno; |
| 217 | |
| 218 | switch (ee->ee_origin) { |
| 219 | case SO_EE_ORIGIN_ICMP: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 220 | switch (ee->ee_type) { |
| 221 | case ICMP_DEST_UNREACH: |
| 222 | switch (ee->ee_code) { |
| 223 | case ICMP_NET_UNREACH: |
| 224 | _net("Rx Received ICMP Network Unreachable"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 225 | break; |
| 226 | case ICMP_HOST_UNREACH: |
| 227 | _net("Rx Received ICMP Host Unreachable"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 228 | break; |
| 229 | case ICMP_PORT_UNREACH: |
| 230 | _net("Rx Received ICMP Port Unreachable"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 231 | break; |
| 232 | case ICMP_NET_UNKNOWN: |
| 233 | _net("Rx Received ICMP Unknown Network"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 234 | break; |
| 235 | case ICMP_HOST_UNKNOWN: |
| 236 | _net("Rx Received ICMP Unknown Host"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 237 | break; |
| 238 | default: |
| 239 | _net("Rx Received ICMP DestUnreach code=%u", |
| 240 | ee->ee_code); |
| 241 | break; |
| 242 | } |
| 243 | break; |
| 244 | |
| 245 | case ICMP_TIME_EXCEEDED: |
| 246 | _net("Rx Received ICMP TTL Exceeded"); |
| 247 | break; |
| 248 | |
| 249 | default: |
| 250 | _proto("Rx Received ICMP error { type=%u code=%u }", |
| 251 | ee->ee_type, ee->ee_code); |
| 252 | break; |
| 253 | } |
| 254 | break; |
| 255 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 256 | case SO_EE_ORIGIN_NONE: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 257 | case SO_EE_ORIGIN_LOCAL: |
David Howells | fe77d5f | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 258 | _proto("Rx Received local error { error=%d }", err); |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 259 | compl = RXRPC_CALL_LOCAL_ERROR; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 260 | break; |
| 261 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 262 | case SO_EE_ORIGIN_ICMP6: |
| 263 | default: |
David Howells | fe77d5f | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 264 | _proto("Rx Received error report { orig=%u }", ee->ee_origin); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 268 | rxrpc_distribute_error(peer, err, compl); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 269 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 270 | |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 271 | /* |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 272 | * Distribute an error that occurred on a peer. |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 273 | */ |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 274 | static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error, |
| 275 | enum rxrpc_call_completion compl) |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 276 | { |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 277 | struct rxrpc_call *call; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 278 | |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 279 | hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) { |
David Howells | e34d423 | 2016-08-30 09:49:29 +0100 | [diff] [blame] | 280 | rxrpc_see_call(call); |
David Howells | f334430 | 2018-09-27 15:13:09 +0100 | [diff] [blame] | 281 | if (call->state < RXRPC_CALL_COMPLETE && |
| 282 | rxrpc_set_call_completion(call, compl, 0, -error)) |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 283 | rxrpc_notify_socket(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 284 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 285 | } |
David Howells | cf1a647 | 2016-09-22 00:41:53 +0100 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * Add RTT information to cache. This is called in softirq mode and has |
| 289 | * exclusive access to the peer RTT data. |
| 290 | */ |
| 291 | void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why, |
| 292 | rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial, |
| 293 | ktime_t send_time, ktime_t resp_time) |
| 294 | { |
| 295 | struct rxrpc_peer *peer = call->peer; |
| 296 | s64 rtt; |
| 297 | u64 sum = peer->rtt_sum, avg; |
| 298 | u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage; |
| 299 | |
| 300 | rtt = ktime_to_ns(ktime_sub(resp_time, send_time)); |
| 301 | if (rtt < 0) |
| 302 | return; |
| 303 | |
David Howells | c1e15b4 | 2018-10-08 15:46:25 +0100 | [diff] [blame^] | 304 | spin_lock(&peer->rtt_input_lock); |
| 305 | |
David Howells | cf1a647 | 2016-09-22 00:41:53 +0100 | [diff] [blame] | 306 | /* Replace the oldest datum in the RTT buffer */ |
| 307 | sum -= peer->rtt_cache[cursor]; |
| 308 | sum += rtt; |
| 309 | peer->rtt_cache[cursor] = rtt; |
| 310 | peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1); |
| 311 | peer->rtt_sum = sum; |
| 312 | if (usage < RXRPC_RTT_CACHE_SIZE) { |
| 313 | usage++; |
| 314 | peer->rtt_usage = usage; |
| 315 | } |
| 316 | |
David Howells | c1e15b4 | 2018-10-08 15:46:25 +0100 | [diff] [blame^] | 317 | spin_unlock(&peer->rtt_input_lock); |
| 318 | |
David Howells | cf1a647 | 2016-09-22 00:41:53 +0100 | [diff] [blame] | 319 | /* Now recalculate the average */ |
| 320 | if (usage == RXRPC_RTT_CACHE_SIZE) { |
| 321 | avg = sum / RXRPC_RTT_CACHE_SIZE; |
| 322 | } else { |
| 323 | avg = sum; |
| 324 | do_div(avg, usage); |
| 325 | } |
| 326 | |
David Howells | c1e15b4 | 2018-10-08 15:46:25 +0100 | [diff] [blame^] | 327 | /* Don't need to update this under lock */ |
David Howells | cf1a647 | 2016-09-22 00:41:53 +0100 | [diff] [blame] | 328 | peer->rtt = avg; |
| 329 | trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt, |
| 330 | usage, avg); |
| 331 | } |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 332 | |
| 333 | /* |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 334 | * Perform keep-alive pings. |
| 335 | */ |
| 336 | static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet, |
| 337 | struct list_head *collector, |
| 338 | time64_t base, |
| 339 | u8 cursor) |
| 340 | { |
| 341 | struct rxrpc_peer *peer; |
| 342 | const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1; |
| 343 | time64_t keepalive_at; |
| 344 | int slot; |
| 345 | |
| 346 | spin_lock_bh(&rxnet->peer_hash_lock); |
| 347 | |
| 348 | while (!list_empty(collector)) { |
| 349 | peer = list_entry(collector->next, |
| 350 | struct rxrpc_peer, keepalive_link); |
| 351 | |
| 352 | list_del_init(&peer->keepalive_link); |
| 353 | if (!rxrpc_get_peer_maybe(peer)) |
| 354 | continue; |
| 355 | |
| 356 | spin_unlock_bh(&rxnet->peer_hash_lock); |
| 357 | |
| 358 | keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME; |
| 359 | slot = keepalive_at - base; |
| 360 | _debug("%02x peer %u t=%d {%pISp}", |
| 361 | cursor, peer->debug_id, slot, &peer->srx.transport); |
| 362 | |
| 363 | if (keepalive_at <= base || |
| 364 | keepalive_at > base + RXRPC_KEEPALIVE_TIME) { |
| 365 | rxrpc_send_keepalive(peer); |
| 366 | slot = RXRPC_KEEPALIVE_TIME; |
| 367 | } |
| 368 | |
| 369 | /* A transmission to this peer occurred since last we examined |
| 370 | * it so put it into the appropriate future bucket. |
| 371 | */ |
| 372 | slot += cursor; |
| 373 | slot &= mask; |
| 374 | spin_lock_bh(&rxnet->peer_hash_lock); |
| 375 | list_add_tail(&peer->keepalive_link, |
| 376 | &rxnet->peer_keepalive[slot & mask]); |
| 377 | rxrpc_put_peer(peer); |
| 378 | } |
| 379 | |
| 380 | spin_unlock_bh(&rxnet->peer_hash_lock); |
| 381 | } |
| 382 | |
| 383 | /* |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 384 | * Perform keep-alive pings with VERSION packets to keep any NAT alive. |
| 385 | */ |
| 386 | void rxrpc_peer_keepalive_worker(struct work_struct *work) |
| 387 | { |
| 388 | struct rxrpc_net *rxnet = |
| 389 | container_of(work, struct rxrpc_net, peer_keepalive_work); |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 390 | const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1; |
| 391 | time64_t base, now, delay; |
| 392 | u8 cursor, stop; |
| 393 | LIST_HEAD(collector); |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 394 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 395 | now = ktime_get_seconds(); |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 396 | base = rxnet->peer_keepalive_base; |
| 397 | cursor = rxnet->peer_keepalive_cursor; |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 398 | _enter("%lld,%u", base - now, cursor); |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 399 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 400 | if (!rxnet->live) |
| 401 | return; |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 402 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 403 | /* Remove to a temporary list all the peers that are currently lodged |
| 404 | * in expired buckets plus all new peers. |
| 405 | * |
| 406 | * Everything in the bucket at the cursor is processed this |
| 407 | * second; the bucket at cursor + 1 goes at now + 1s and so |
| 408 | * on... |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 409 | */ |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 410 | spin_lock_bh(&rxnet->peer_hash_lock); |
| 411 | list_splice_init(&rxnet->peer_keepalive_new, &collector); |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 412 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 413 | stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive); |
| 414 | while (base <= now && (s8)(cursor - stop) < 0) { |
| 415 | list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask], |
| 416 | &collector); |
| 417 | base++; |
| 418 | cursor++; |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 419 | } |
| 420 | |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 421 | base = now; |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 422 | spin_unlock_bh(&rxnet->peer_hash_lock); |
| 423 | |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 424 | rxnet->peer_keepalive_base = base; |
| 425 | rxnet->peer_keepalive_cursor = cursor; |
David Howells | 330bdcf | 2018-08-08 11:30:02 +0100 | [diff] [blame] | 426 | rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor); |
| 427 | ASSERT(list_empty(&collector)); |
| 428 | |
| 429 | /* Schedule the timer for the next occupied timeslot. */ |
| 430 | cursor = rxnet->peer_keepalive_cursor; |
| 431 | stop = cursor + RXRPC_KEEPALIVE_TIME - 1; |
| 432 | for (; (s8)(cursor - stop) < 0; cursor++) { |
| 433 | if (!list_empty(&rxnet->peer_keepalive[cursor & mask])) |
| 434 | break; |
| 435 | base++; |
| 436 | } |
| 437 | |
| 438 | now = ktime_get_seconds(); |
| 439 | delay = base - now; |
| 440 | if (delay < 1) |
| 441 | delay = 1; |
| 442 | delay *= HZ; |
| 443 | if (rxnet->live) |
| 444 | timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay); |
| 445 | |
David Howells | ace45be | 2018-03-30 21:04:43 +0100 | [diff] [blame] | 446 | _leave(""); |
| 447 | } |