blob: a0906145e8293ca457fd0b1493ba3892f5f0729a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells17926a72007-04-26 15:48:28 -07002/* RxRPC packet transmission
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells17926a72007-04-26 15:48:28 -07006 */
7
Joe Perches9b6d5392016-06-02 12:08:52 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
David Howells17926a72007-04-26 15:48:28 -070010#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
David Howells17926a72007-04-26 15:48:28 -070012#include <linux/skbuff.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040013#include <linux/export.h>
David Howells17926a72007-04-26 15:48:28 -070014#include <net/sock.h>
15#include <net/af_rxrpc.h>
David Howellsed472b02022-03-22 11:07:20 +000016#include <net/udp.h>
David Howells17926a72007-04-26 15:48:28 -070017#include "ar-internal.h"
18
David Howellsed472b02022-03-22 11:07:20 +000019extern int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
20
David Howells6423ac22022-11-11 16:00:21 +000021static ssize_t do_udp_sendmsg(struct socket *socket, struct msghdr *msg, size_t len)
David Howellsed472b02022-03-22 11:07:20 +000022{
David Howellsed472b02022-03-22 11:07:20 +000023 struct sockaddr *sa = msg->msg_name;
David Howells6423ac22022-11-11 16:00:21 +000024 struct sock *sk = socket->sk;
David Howellsed472b02022-03-22 11:07:20 +000025
David Howells6423ac22022-11-11 16:00:21 +000026 if (IS_ENABLED(CONFIG_AF_RXRPC_IPV6)) {
27 if (sa->sa_family == AF_INET6) {
28 if (sk->sk_family != AF_INET6) {
29 pr_warn("AF_INET6 address on AF_INET socket\n");
30 return -ENOPROTOOPT;
31 }
32 return udpv6_sendmsg(sk, msg, len);
33 }
34 }
35 return udp_sendmsg(sk, msg, len);
David Howellsed472b02022-03-22 11:07:20 +000036}
37
David Howells26cb02a2016-10-06 08:11:49 +010038struct rxrpc_abort_buffer {
39 struct rxrpc_wire_header whdr;
40 __be32 abort_code;
41};
42
David Howellsace45be2018-03-30 21:04:43 +010043static const char rxrpc_keepalive_string[] = "";
44
David Howells8d94aa32016-09-07 09:19:31 +010045/*
David Howellsc7e86ac2018-11-01 13:39:53 +000046 * Increase Tx backoff on transmission failure and clear it on success.
47 */
48static void rxrpc_tx_backoff(struct rxrpc_call *call, int ret)
49{
50 if (ret < 0) {
51 u16 tx_backoff = READ_ONCE(call->tx_backoff);
52
53 if (tx_backoff < HZ)
54 WRITE_ONCE(call->tx_backoff, tx_backoff + 1);
55 } else {
56 WRITE_ONCE(call->tx_backoff, 0);
57 }
58}
59
60/*
David Howells415f44e2017-11-24 10:18:42 +000061 * Arrange for a keepalive ping a certain time after we last transmitted. This
62 * lets the far side know we're still interested in this call and helps keep
63 * the route through any intervening firewall open.
64 *
65 * Receiving a response to the ping will prevent the ->expect_rx_by timer from
66 * expiring.
67 */
68static void rxrpc_set_keepalive(struct rxrpc_call *call)
69{
70 unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
71
72 keepalive_at += now;
73 WRITE_ONCE(call->keepalive_at, keepalive_at);
74 rxrpc_reduce_call_timer(call, keepalive_at, now,
75 rxrpc_timer_set_for_keepalive);
76}
77
78/*
David Howells8d94aa32016-09-07 09:19:31 +010079 * Fill out an ACK packet.
80 */
David Howells1457cc42017-11-02 15:06:55 +000081static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
82 struct rxrpc_call *call,
David Howellsf789bff2023-01-31 15:31:49 +000083 struct rxrpc_txbuf *txb,
84 u16 *_rwind)
David Howells8d94aa32016-09-07 09:19:31 +010085{
David Howells72f0c6f2020-01-30 21:48:13 +000086 struct rxrpc_ackinfo ackinfo;
David Howellsf21e9342022-10-16 08:01:32 +010087 unsigned int qsize, sack, wrap, to;
88 rxrpc_seq_t window, wtop;
David Howells5d7edbc2022-08-27 14:27:56 +010089 int rsize;
David Howells8d94aa32016-09-07 09:19:31 +010090 u32 mtu, jmax;
David Howells72f0c6f2020-01-30 21:48:13 +000091 u8 *ackp = txb->acks;
David Howells8d94aa32016-09-07 09:19:31 +010092
David Howells5bbf95332022-10-17 11:44:22 +010093 call->ackr_nr_unacked = 0;
David Howells5d7edbc2022-08-27 14:27:56 +010094 atomic_set(&call->ackr_nr_consumed, 0);
David Howellsf2a676d2022-05-11 14:01:25 +010095 rxrpc_inc_stat(call->rxnet, stat_tx_ack_fill);
David Howellsf21e9342022-10-16 08:01:32 +010096 clear_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags);
David Howells9a3dedc2022-05-21 09:03:31 +010097
David Howells5bbf95332022-10-17 11:44:22 +010098 window = call->ackr_window;
99 wtop = call->ackr_wtop;
David Howellsf21e9342022-10-16 08:01:32 +0100100 sack = call->ackr_sack_base % RXRPC_SACK_SIZE;
David Howells5d7edbc2022-08-27 14:27:56 +0100101 txb->ack.firstPacket = htonl(window);
David Howellsf21e9342022-10-16 08:01:32 +0100102 txb->ack.nAcks = wtop - window;
David Howells248f2192016-09-08 11:10:12 +0100103
David Howells5d7edbc2022-08-27 14:27:56 +0100104 if (after(wtop, window)) {
David Howellsf21e9342022-10-16 08:01:32 +0100105 wrap = RXRPC_SACK_SIZE - sack;
106 to = min_t(unsigned int, txb->ack.nAcks, RXRPC_SACK_SIZE);
David Howells8d94aa32016-09-07 09:19:31 +0100107
David Howellsf21e9342022-10-16 08:01:32 +0100108 if (sack + txb->ack.nAcks <= RXRPC_SACK_SIZE) {
109 memcpy(txb->acks, call->ackr_sack_table + sack, txb->ack.nAcks);
David Howells5d7edbc2022-08-27 14:27:56 +0100110 } else {
David Howellsf21e9342022-10-16 08:01:32 +0100111 memcpy(txb->acks, call->ackr_sack_table + sack, wrap);
112 memcpy(txb->acks + wrap, call->ackr_sack_table,
113 to - wrap);
David Howells5d7edbc2022-08-27 14:27:56 +0100114 }
115
David Howellsf21e9342022-10-16 08:01:32 +0100116 ackp += to;
David Howells5d7edbc2022-08-27 14:27:56 +0100117 } else if (before(wtop, window)) {
118 pr_warn("ack window backward %x %x", window, wtop);
David Howells530403d2020-01-30 21:48:14 +0000119 } else if (txb->ack.reason == RXRPC_ACK_DELAY) {
120 txb->ack.reason = RXRPC_ACK_IDLE;
David Howells248f2192016-09-08 11:10:12 +0100121 }
122
David Howells2cc80082022-10-19 13:49:02 +0100123 mtu = conn->peer->if_mtu;
124 mtu -= conn->peer->hdrsize;
David Howellsd4d02d82022-10-07 17:44:39 +0100125 jmax = rxrpc_rx_jumbo_max;
David Howells5d7edbc2022-08-27 14:27:56 +0100126 qsize = (window - 1) - call->rx_consumed;
127 rsize = max_t(int, call->rx_winsize - qsize, 0);
David Howellsf789bff2023-01-31 15:31:49 +0000128 *_rwind = rsize;
David Howells72f0c6f2020-01-30 21:48:13 +0000129 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
130 ackinfo.maxMTU = htonl(mtu);
David Howells5d7edbc2022-08-27 14:27:56 +0100131 ackinfo.rwind = htonl(rsize);
David Howells72f0c6f2020-01-30 21:48:13 +0000132 ackinfo.jumbo_max = htonl(jmax);
David Howells8d94aa32016-09-07 09:19:31 +0100133
134 *ackp++ = 0;
135 *ackp++ = 0;
136 *ackp++ = 0;
David Howells72f0c6f2020-01-30 21:48:13 +0000137 memcpy(ackp, &ackinfo, sizeof(ackinfo));
David Howells5d7edbc2022-08-27 14:27:56 +0100138 return txb->ack.nAcks + 3 + sizeof(ackinfo);
David Howells8d94aa32016-09-07 09:19:31 +0100139}
140
141/*
David Howells4700c4d2020-08-19 23:29:16 +0100142 * Record the beginning of an RTT probe.
143 */
144static int rxrpc_begin_rtt_probe(struct rxrpc_call *call, rxrpc_serial_t serial,
145 enum rxrpc_rtt_tx_trace why)
146{
147 unsigned long avail = call->rtt_avail;
148 int rtt_slot = 9;
149
150 if (!(avail & RXRPC_CALL_RTT_AVAIL_MASK))
151 goto no_slot;
152
153 rtt_slot = __ffs(avail & RXRPC_CALL_RTT_AVAIL_MASK);
154 if (!test_and_clear_bit(rtt_slot, &call->rtt_avail))
155 goto no_slot;
156
157 call->rtt_serial[rtt_slot] = serial;
158 call->rtt_sent_at[rtt_slot] = ktime_get_real();
159 smp_wmb(); /* Write data before avail bit */
160 set_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
161
162 trace_rxrpc_rtt_tx(call, why, rtt_slot, serial);
163 return rtt_slot;
164
165no_slot:
166 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_no_slot, rtt_slot, serial);
167 return -1;
168}
169
170/*
171 * Cancel an RTT probe.
172 */
173static void rxrpc_cancel_rtt_probe(struct rxrpc_call *call,
174 rxrpc_serial_t serial, int rtt_slot)
175{
176 if (rtt_slot != -1) {
177 clear_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
178 smp_wmb(); /* Clear pending bit before setting slot */
179 set_bit(rtt_slot, &call->rtt_avail);
180 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_cancel, rtt_slot, serial);
181 }
182}
183
184/*
David Howellsb0346842020-01-30 21:48:13 +0000185 * Transmit an ACK packet.
David Howells8d94aa32016-09-07 09:19:31 +0100186 */
David Howellsb0346842020-01-30 21:48:13 +0000187int rxrpc_send_ack_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
David Howells8d94aa32016-09-07 09:19:31 +0100188{
David Howells5273a192020-01-30 21:50:36 +0000189 struct rxrpc_connection *conn;
David Howells8d94aa32016-09-07 09:19:31 +0100190 struct msghdr msg;
David Howells72f0c6f2020-01-30 21:48:13 +0000191 struct kvec iov[1];
David Howells8d94aa32016-09-07 09:19:31 +0100192 rxrpc_serial_t serial;
193 size_t len, n;
David Howells4700c4d2020-08-19 23:29:16 +0100194 int ret, rtt_slot = -1;
David Howellsf789bff2023-01-31 15:31:49 +0000195 u16 rwind;
David Howells8d94aa32016-09-07 09:19:31 +0100196
David Howells5273a192020-01-30 21:50:36 +0000197 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
David Howells8d94aa32016-09-07 09:19:31 +0100198 return -ECONNRESET;
199
David Howells5273a192020-01-30 21:50:36 +0000200 conn = call->conn;
David Howells8d94aa32016-09-07 09:19:31 +0100201
David Howells8d94aa32016-09-07 09:19:31 +0100202 msg.msg_name = &call->peer->srx.transport;
203 msg.msg_namelen = call->peer->srx.transport_len;
204 msg.msg_control = NULL;
205 msg.msg_controllen = 0;
206 msg.msg_flags = 0;
207
David Howells72f0c6f2020-01-30 21:48:13 +0000208 if (txb->ack.reason == RXRPC_ACK_PING)
209 txb->wire.flags |= RXRPC_REQUEST_ACK;
David Howells8d94aa32016-09-07 09:19:31 +0100210
David Howellsf789bff2023-01-31 15:31:49 +0000211 n = rxrpc_fill_out_ack(conn, call, txb, &rwind);
David Howells4e76bd42022-05-06 16:13:13 +0100212 if (n == 0)
David Howells9a3dedc2022-05-21 09:03:31 +0100213 return 0;
David Howells26cb02a2016-10-06 08:11:49 +0100214
David Howells72f0c6f2020-01-30 21:48:13 +0000215 iov[0].iov_base = &txb->wire;
216 iov[0].iov_len = sizeof(txb->wire) + sizeof(txb->ack) + n;
217 len = iov[0].iov_len;
David Howells8d94aa32016-09-07 09:19:31 +0100218
David Howellsb86e2182016-09-23 15:08:48 +0100219 serial = atomic_inc_return(&conn->serial);
David Howells72f0c6f2020-01-30 21:48:13 +0000220 txb->wire.serial = htonl(serial);
David Howells4764c0d2018-07-23 17:18:37 +0100221 trace_rxrpc_tx_ack(call->debug_id, serial,
David Howells72f0c6f2020-01-30 21:48:13 +0000222 ntohl(txb->ack.firstPacket),
David Howellsf789bff2023-01-31 15:31:49 +0000223 ntohl(txb->ack.serial), txb->ack.reason, txb->ack.nAcks,
224 rwind);
David Howellsb86e2182016-09-23 15:08:48 +0100225
David Howells72f0c6f2020-01-30 21:48:13 +0000226 if (txb->ack.reason == RXRPC_ACK_PING)
David Howells4700c4d2020-08-19 23:29:16 +0100227 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_ping);
David Howells26cb02a2016-10-06 08:11:49 +0100228
David Howellsf2a676d2022-05-11 14:01:25 +0100229 rxrpc_inc_stat(call->rxnet, stat_tx_ack_send);
David Howellsed472b02022-03-22 11:07:20 +0000230
David Howells5d7edbc2022-08-27 14:27:56 +0100231 /* Grab the highest received seq as late as possible */
232 txb->ack.previousPacket = htonl(call->rx_highest_seq);
233
David Howells72f0c6f2020-01-30 21:48:13 +0000234 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howells2cc80082022-10-19 13:49:02 +0100235 ret = do_udp_sendmsg(conn->local->socket, &msg, len);
David Howellsed472b02022-03-22 11:07:20 +0000236 call->peer->last_tx_at = ktime_get_seconds();
David Howells84e28aa2022-10-17 10:55:41 +0100237 if (ret < 0) {
David Howells6b47fe12018-05-10 23:26:01 +0100238 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100239 rxrpc_tx_point_call_ack);
David Howells84e28aa2022-10-17 10:55:41 +0100240 } else {
David Howells72f0c6f2020-01-30 21:48:13 +0000241 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
David Howells4764c0d2018-07-23 17:18:37 +0100242 rxrpc_tx_point_call_ack);
David Howells84e28aa2022-10-17 10:55:41 +0100243 if (txb->wire.flags & RXRPC_REQUEST_ACK)
244 call->peer->rtt_last_req = ktime_get_real();
245 }
David Howellsc7e86ac2018-11-01 13:39:53 +0000246 rxrpc_tx_backoff(call, ret);
David Howells8d94aa32016-09-07 09:19:31 +0100247
David Howells96b40592022-10-27 11:25:55 +0100248 if (!__rxrpc_call_is_complete(call)) {
David Howells72f0c6f2020-01-30 21:48:13 +0000249 if (ret < 0)
David Howells4700c4d2020-08-19 23:29:16 +0100250 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
David Howells415f44e2017-11-24 10:18:42 +0000251 rxrpc_set_keepalive(call);
David Howells248f2192016-09-08 11:10:12 +0100252 }
253
David Howells8d94aa32016-09-07 09:19:31 +0100254 return ret;
255}
256
David Howells5873c082014-02-07 18:58:44 +0000257/*
David Howells26cb02a2016-10-06 08:11:49 +0100258 * Send an ABORT call packet.
259 */
260int rxrpc_send_abort_packet(struct rxrpc_call *call)
261{
David Howells5273a192020-01-30 21:50:36 +0000262 struct rxrpc_connection *conn;
David Howells26cb02a2016-10-06 08:11:49 +0100263 struct rxrpc_abort_buffer pkt;
264 struct msghdr msg;
265 struct kvec iov[1];
266 rxrpc_serial_t serial;
267 int ret;
268
David Howellsdcbefc32017-11-02 15:06:26 +0000269 /* Don't bother sending aborts for a client call once the server has
270 * hard-ACK'd all of its request data. After that point, we're not
271 * going to stop the operation proceeding, and whilst we might limit
272 * the reply, it's not worth it if we can send a new call on the same
273 * channel instead, thereby closing off this call.
274 */
275 if (rxrpc_is_client_call(call) &&
David Howellsa4ea4c42022-03-31 23:55:08 +0100276 test_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags))
David Howellsdcbefc32017-11-02 15:06:26 +0000277 return 0;
278
David Howells5273a192020-01-30 21:50:36 +0000279 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
David Howells26cb02a2016-10-06 08:11:49 +0100280 return -ECONNRESET;
281
David Howells5273a192020-01-30 21:50:36 +0000282 conn = call->conn;
283
David Howells26cb02a2016-10-06 08:11:49 +0100284 msg.msg_name = &call->peer->srx.transport;
285 msg.msg_namelen = call->peer->srx.transport_len;
286 msg.msg_control = NULL;
287 msg.msg_controllen = 0;
288 msg.msg_flags = 0;
289
290 pkt.whdr.epoch = htonl(conn->proto.epoch);
291 pkt.whdr.cid = htonl(call->cid);
292 pkt.whdr.callNumber = htonl(call->call_id);
293 pkt.whdr.seq = 0;
294 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
295 pkt.whdr.flags = conn->out_clientflag;
296 pkt.whdr.userStatus = 0;
297 pkt.whdr.securityIndex = call->security_ix;
298 pkt.whdr._rsvd = 0;
David Howellsf3441d42022-10-20 21:58:36 +0100299 pkt.whdr.serviceId = htons(call->dest_srx.srx_service);
David Howells26cb02a2016-10-06 08:11:49 +0100300 pkt.abort_code = htonl(call->abort_code);
301
302 iov[0].iov_base = &pkt;
303 iov[0].iov_len = sizeof(pkt);
304
305 serial = atomic_inc_return(&conn->serial);
306 pkt.whdr.serial = htonl(serial);
307
David Howellsed472b02022-03-22 11:07:20 +0000308 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
David Howells2cc80082022-10-19 13:49:02 +0100309 ret = do_udp_sendmsg(conn->local->socket, &msg, sizeof(pkt));
310 conn->peer->last_tx_at = ktime_get_seconds();
David Howells6b47fe12018-05-10 23:26:01 +0100311 if (ret < 0)
312 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100313 rxrpc_tx_point_call_abort);
314 else
315 trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
316 rxrpc_tx_point_call_abort);
David Howellsc7e86ac2018-11-01 13:39:53 +0000317 rxrpc_tx_backoff(call, ret);
David Howells26cb02a2016-10-06 08:11:49 +0100318 return ret;
319}
320
321/*
David Howells17926a72007-04-26 15:48:28 -0700322 * send a packet through the transport endpoint
323 */
David Howellsa4ea4c42022-03-31 23:55:08 +0100324int rxrpc_send_data_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
David Howells17926a72007-04-26 15:48:28 -0700325{
David Howells4d843be2022-04-05 21:48:48 +0100326 enum rxrpc_req_ack_trace why;
David Howells5a924b82016-09-22 00:29:31 +0100327 struct rxrpc_connection *conn = call->conn;
David Howells17926a72007-04-26 15:48:28 -0700328 struct msghdr msg;
David Howellsa4ea4c42022-03-31 23:55:08 +0100329 struct kvec iov[1];
David Howells5a924b82016-09-22 00:29:31 +0100330 rxrpc_serial_t serial;
331 size_t len;
David Howells4700c4d2020-08-19 23:29:16 +0100332 int ret, rtt_slot = -1;
David Howells17926a72007-04-26 15:48:28 -0700333
David Howellsa4ea4c42022-03-31 23:55:08 +0100334 _enter("%x,{%d}", txb->seq, txb->len);
David Howells17926a72007-04-26 15:48:28 -0700335
David Howells5a924b82016-09-22 00:29:31 +0100336 /* Each transmission of a Tx packet needs a new serial number */
337 serial = atomic_inc_return(&conn->serial);
David Howellsa4ea4c42022-03-31 23:55:08 +0100338 txb->wire.serial = htonl(serial);
David Howells5a924b82016-09-22 00:29:31 +0100339
David Howells4e255722017-06-05 14:30:49 +0100340 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
David Howellsa4ea4c42022-03-31 23:55:08 +0100341 txb->seq == 1)
342 txb->wire.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
David Howells4e255722017-06-05 14:30:49 +0100343
David Howellsa4ea4c42022-03-31 23:55:08 +0100344 iov[0].iov_base = &txb->wire;
345 iov[0].iov_len = sizeof(txb->wire) + txb->len;
346 len = iov[0].iov_len;
347 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howells5a924b82016-09-22 00:29:31 +0100348
349 msg.msg_name = &call->peer->srx.transport;
350 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700351 msg.msg_control = NULL;
352 msg.msg_controllen = 0;
353 msg.msg_flags = 0;
354
David Howells57494342016-09-24 18:05:27 +0100355 /* If our RTT cache needs working on, request an ACK. Also request
356 * ACKs if a DATA packet appears to have been lost.
David Howellsb604dd92018-09-27 15:13:08 +0100357 *
358 * However, we mustn't request an ACK on the last reply packet of a
359 * service call, lest OpenAFS incorrectly send us an ACK with some
360 * soft-ACKs in it and then never follow up with a proper hard ACK.
David Howells57494342016-09-24 18:05:27 +0100361 */
David Howellsa4ea4c42022-03-31 23:55:08 +0100362 if (txb->wire.flags & RXRPC_REQUEST_ACK)
David Howells4d843be2022-04-05 21:48:48 +0100363 why = rxrpc_reqack_already_on;
David Howellsa4ea4c42022-03-31 23:55:08 +0100364 else if (test_bit(RXRPC_TXBUF_LAST, &txb->flags) && rxrpc_sending_to_client(txb))
David Howells4d843be2022-04-05 21:48:48 +0100365 why = rxrpc_reqack_no_srv_last;
366 else if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
367 why = rxrpc_reqack_ack_lost;
David Howellsa4ea4c42022-03-31 23:55:08 +0100368 else if (test_bit(RXRPC_TXBUF_RESENT, &txb->flags))
David Howells4d843be2022-04-05 21:48:48 +0100369 why = rxrpc_reqack_retrans;
370 else if (call->cong_mode == RXRPC_CALL_SLOW_START && call->cong_cwnd <= 2)
371 why = rxrpc_reqack_slow_start;
372 else if (call->tx_winsize <= 2)
373 why = rxrpc_reqack_small_txwin;
David Howellsa4ea4c42022-03-31 23:55:08 +0100374 else if (call->peer->rtt_count < 3 && txb->seq & 1)
David Howells4d843be2022-04-05 21:48:48 +0100375 why = rxrpc_reqack_more_rtt;
376 else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), ktime_get_real()))
377 why = rxrpc_reqack_old_rtt;
378 else
379 goto dont_set_request_ack;
380
David Howellsf7fa5242022-08-18 11:52:36 +0100381 rxrpc_inc_stat(call->rxnet, stat_why_req_ack[why]);
David Howellsa4ea4c42022-03-31 23:55:08 +0100382 trace_rxrpc_req_ack(call->debug_id, txb->seq, why);
David Howells4d843be2022-04-05 21:48:48 +0100383 if (why != rxrpc_reqack_no_srv_last)
David Howellsa4ea4c42022-03-31 23:55:08 +0100384 txb->wire.flags |= RXRPC_REQUEST_ACK;
David Howells4d843be2022-04-05 21:48:48 +0100385dont_set_request_ack:
David Howells0d4b1032016-09-22 00:29:31 +0100386
David Howells8a681c362016-09-17 10:49:15 +0100387 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
388 static int lose;
389 if ((lose++ & 7) == 7) {
David Howellsa1767072016-09-29 22:37:15 +0100390 ret = 0;
David Howellsa4ea4c42022-03-31 23:55:08 +0100391 trace_rxrpc_tx_data(call, txb->seq, serial,
392 txb->wire.flags,
393 test_bit(RXRPC_TXBUF_RESENT, &txb->flags),
394 true);
Arnd Bergmann526949e2019-03-22 15:18:43 +0100395 goto done;
David Howells8a681c362016-09-17 10:49:15 +0100396 }
397 }
398
David Howellsa4ea4c42022-03-31 23:55:08 +0100399 trace_rxrpc_tx_data(call, txb->seq, serial, txb->wire.flags,
400 test_bit(RXRPC_TXBUF_RESENT, &txb->flags), false);
David Howellscf37b592022-03-31 23:55:08 +0100401
402 /* Track what we've attempted to transmit at least once so that the
403 * retransmission algorithm doesn't try to resend what we haven't sent
404 * yet. However, this can race as we can receive an ACK before we get
405 * to this point. But, OTOH, if we won't get an ACK mentioning this
406 * packet unless the far side received it (though it could have
407 * discarded it anyway and NAK'd it).
408 */
David Howellsa4ea4c42022-03-31 23:55:08 +0100409 cmpxchg(&call->tx_transmitted, txb->seq - 1, txb->seq);
David Howells5a924b82016-09-22 00:29:31 +0100410
David Howells17926a72007-04-26 15:48:28 -0700411 /* send the packet with the don't fragment bit set if we currently
412 * think it's small enough */
David Howellsa4ea4c42022-03-31 23:55:08 +0100413 if (txb->len >= call->peer->maxdata)
David Howells5a924b82016-09-22 00:29:31 +0100414 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700415
David Howellsa4ea4c42022-03-31 23:55:08 +0100416 txb->last_sent = ktime_get_real();
417 if (txb->wire.flags & RXRPC_REQUEST_ACK)
David Howells4700c4d2020-08-19 23:29:16 +0100418 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
David Howellsb604dd92018-09-27 15:13:08 +0100419
David Howells5a924b82016-09-22 00:29:31 +0100420 /* send the packet by UDP
421 * - returns -EMSGSIZE if UDP would have to fragment the packet
422 * to go out of the interface
423 * - in which case, we'll have processed the ICMP error
424 * message and update the peer record
425 */
David Howellsb0154242022-05-11 14:01:25 +0100426 rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
David Howells2cc80082022-10-19 13:49:02 +0100427 ret = do_udp_sendmsg(conn->local->socket, &msg, len);
428 conn->peer->last_tx_at = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700429
David Howells4700c4d2020-08-19 23:29:16 +0100430 if (ret < 0) {
David Howells32cf8ed2022-11-11 13:47:35 +0000431 rxrpc_inc_stat(call->rxnet, stat_tx_data_send_fail);
David Howells4700c4d2020-08-19 23:29:16 +0100432 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
David Howells6b47fe12018-05-10 23:26:01 +0100433 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100434 rxrpc_tx_point_call_data_nofrag);
David Howells4700c4d2020-08-19 23:29:16 +0100435 } else {
David Howellsa4ea4c42022-03-31 23:55:08 +0100436 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
David Howells4764c0d2018-07-23 17:18:37 +0100437 rxrpc_tx_point_call_data_nofrag);
David Howells4700c4d2020-08-19 23:29:16 +0100438 }
439
David Howellsc7e86ac2018-11-01 13:39:53 +0000440 rxrpc_tx_backoff(call, ret);
David Howells5a924b82016-09-22 00:29:31 +0100441 if (ret == -EMSGSIZE)
442 goto send_fragmentable;
443
444done:
David Howells50235c42016-09-22 00:29:31 +0100445 if (ret >= 0) {
David Howells1fc4fa22022-10-03 18:49:11 +0100446 call->tx_last_sent = txb->last_sent;
David Howellsa4ea4c42022-03-31 23:55:08 +0100447 if (txb->wire.flags & RXRPC_REQUEST_ACK) {
448 call->peer->rtt_last_req = txb->last_sent;
David Howellsc410bf012020-05-11 14:54:34 +0100449 if (call->peer->rtt_count > 1) {
David Howellsbd1fdf82017-11-24 10:18:42 +0000450 unsigned long nowj = jiffies, ack_lost_at;
451
David Howells2c13c052022-01-21 23:12:58 +0000452 ack_lost_at = rxrpc_get_rto_backoff(call->peer, false);
David Howellsbd1fdf82017-11-24 10:18:42 +0000453 ack_lost_at += nowj;
454 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
455 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
456 rxrpc_timer_set_for_lost_ack);
457 }
David Howells0d4b1032016-09-22 00:29:31 +0100458 }
David Howellsc54e43d2018-05-10 23:26:00 +0100459
David Howellsa4ea4c42022-03-31 23:55:08 +0100460 if (txb->seq == 1 &&
David Howellsc54e43d2018-05-10 23:26:00 +0100461 !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
462 &call->flags)) {
463 unsigned long nowj = jiffies, expect_rx_by;
464
465 expect_rx_by = nowj + call->next_rx_timo;
466 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
467 rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
468 rxrpc_timer_set_for_normal);
469 }
David Howells415f44e2017-11-24 10:18:42 +0000470
David Howellsc7e86ac2018-11-01 13:39:53 +0000471 rxrpc_set_keepalive(call);
472 } else {
473 /* Cancel the call if the initial transmission fails,
474 * particularly if that's due to network routing issues that
475 * aren't going away anytime soon. The layer above can arrange
476 * the retransmission.
477 */
478 if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
479 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
480 RX_USER_ABORT, ret);
481 }
David Howells415f44e2017-11-24 10:18:42 +0000482
David Howells5a924b82016-09-22 00:29:31 +0100483 _leave(" = %d [%u]", ret, call->peer->maxdata);
484 return ret;
David Howells17926a72007-04-26 15:48:28 -0700485
486send_fragmentable:
487 /* attempt to send this message with fragmentation enabled */
488 _debug("send fragment");
489
David Howellsa4ea4c42022-03-31 23:55:08 +0100490 txb->last_sent = ktime_get_real();
491 if (txb->wire.flags & RXRPC_REQUEST_ACK)
David Howells4700c4d2020-08-19 23:29:16 +0100492 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
David Howellsb604dd92018-09-27 15:13:08 +0100493
David Howells2cc80082022-10-19 13:49:02 +0100494 switch (conn->local->srx.transport.family) {
David Howells0e631ee2020-04-13 13:57:14 +0100495 case AF_INET6:
David Howells985a5c82016-06-17 11:53:37 +0100496 case AF_INET:
David Howells87220142024-01-09 15:10:48 +0000497 rxrpc_local_dont_fragment(conn->local, false);
David Howellsb0154242022-05-11 14:01:25 +0100498 rxrpc_inc_stat(call->rxnet, stat_tx_data_send_frag);
David Howells2cc80082022-10-19 13:49:02 +0100499 ret = do_udp_sendmsg(conn->local->socket, &msg, len);
500 conn->peer->last_tx_at = ktime_get_seconds();
David Howells985a5c82016-06-17 11:53:37 +0100501
David Howells87220142024-01-09 15:10:48 +0000502 rxrpc_local_dont_fragment(conn->local, true);
David Howells985a5c82016-06-17 11:53:37 +0100503 break;
David Howells75b54cb2016-09-13 08:49:05 +0100504
David Howells3427beb2019-07-02 15:55:28 +0100505 default:
506 BUG();
David Howells17926a72007-04-26 15:48:28 -0700507 }
508
David Howells4700c4d2020-08-19 23:29:16 +0100509 if (ret < 0) {
David Howells32cf8ed2022-11-11 13:47:35 +0000510 rxrpc_inc_stat(call->rxnet, stat_tx_data_send_fail);
David Howells4700c4d2020-08-19 23:29:16 +0100511 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
David Howells6b47fe12018-05-10 23:26:01 +0100512 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100513 rxrpc_tx_point_call_data_frag);
David Howells4700c4d2020-08-19 23:29:16 +0100514 } else {
David Howellsa4ea4c42022-03-31 23:55:08 +0100515 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
David Howells4764c0d2018-07-23 17:18:37 +0100516 rxrpc_tx_point_call_data_frag);
David Howells4700c4d2020-08-19 23:29:16 +0100517 }
David Howellsc7e86ac2018-11-01 13:39:53 +0000518 rxrpc_tx_backoff(call, ret);
David Howells5a924b82016-09-22 00:29:31 +0100519 goto done;
David Howells17926a72007-04-26 15:48:28 -0700520}
David Howells248f2192016-09-08 11:10:12 +0100521
522/*
David Howellsa00ce282022-10-20 09:56:36 +0100523 * Transmit a connection-level abort.
524 */
525void rxrpc_send_conn_abort(struct rxrpc_connection *conn)
526{
527 struct rxrpc_wire_header whdr;
528 struct msghdr msg;
529 struct kvec iov[2];
530 __be32 word;
531 size_t len;
532 u32 serial;
533 int ret;
534
535 msg.msg_name = &conn->peer->srx.transport;
536 msg.msg_namelen = conn->peer->srx.transport_len;
537 msg.msg_control = NULL;
538 msg.msg_controllen = 0;
539 msg.msg_flags = 0;
540
541 whdr.epoch = htonl(conn->proto.epoch);
542 whdr.cid = htonl(conn->proto.cid);
543 whdr.callNumber = 0;
544 whdr.seq = 0;
545 whdr.type = RXRPC_PACKET_TYPE_ABORT;
546 whdr.flags = conn->out_clientflag;
547 whdr.userStatus = 0;
548 whdr.securityIndex = conn->security_ix;
549 whdr._rsvd = 0;
550 whdr.serviceId = htons(conn->service_id);
551
552 word = htonl(conn->abort_code);
553
554 iov[0].iov_base = &whdr;
555 iov[0].iov_len = sizeof(whdr);
556 iov[1].iov_base = &word;
557 iov[1].iov_len = sizeof(word);
558
559 len = iov[0].iov_len + iov[1].iov_len;
560
561 serial = atomic_inc_return(&conn->serial);
562 whdr.serial = htonl(serial);
563
564 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
565 ret = do_udp_sendmsg(conn->local->socket, &msg, len);
566 if (ret < 0) {
567 trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
568 rxrpc_tx_point_conn_abort);
569 _debug("sendmsg failed: %d", ret);
570 return;
571 }
572
573 trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort);
574
575 conn->peer->last_tx_at = ktime_get_seconds();
576}
577
578/*
David Howells5e6ef4f2020-01-23 13:13:41 +0000579 * Reject a packet through the local endpoint.
David Howells248f2192016-09-08 11:10:12 +0100580 */
David Howells5e6ef4f2020-01-23 13:13:41 +0000581void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
David Howells248f2192016-09-08 11:10:12 +0100582{
David Howells248f2192016-09-08 11:10:12 +0100583 struct rxrpc_wire_header whdr;
David Howells5e6ef4f2020-01-23 13:13:41 +0000584 struct sockaddr_rxrpc srx;
585 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells248f2192016-09-08 11:10:12 +0100586 struct msghdr msg;
587 struct kvec iov[2];
588 size_t size;
589 __be32 code;
David Howellsece64fe2018-09-27 15:13:08 +0100590 int ret, ioc;
David Howells248f2192016-09-08 11:10:12 +0100591
David Howells5e6ef4f2020-01-23 13:13:41 +0000592 rxrpc_see_skb(skb, rxrpc_skb_see_reject);
David Howells248f2192016-09-08 11:10:12 +0100593
594 iov[0].iov_base = &whdr;
595 iov[0].iov_len = sizeof(whdr);
596 iov[1].iov_base = &code;
597 iov[1].iov_len = sizeof(code);
David Howells248f2192016-09-08 11:10:12 +0100598
David Howells1c2bc7b2016-09-13 08:49:05 +0100599 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100600 msg.msg_control = NULL;
601 msg.msg_controllen = 0;
602 msg.msg_flags = 0;
603
David Howells248f2192016-09-08 11:10:12 +0100604 memset(&whdr, 0, sizeof(whdr));
David Howells248f2192016-09-08 11:10:12 +0100605
David Howells5e6ef4f2020-01-23 13:13:41 +0000606 switch (skb->mark) {
607 case RXRPC_SKB_MARK_REJECT_BUSY:
608 whdr.type = RXRPC_PACKET_TYPE_BUSY;
609 size = sizeof(whdr);
610 ioc = 1;
611 break;
612 case RXRPC_SKB_MARK_REJECT_ABORT:
613 whdr.type = RXRPC_PACKET_TYPE_ABORT;
614 code = htonl(skb->priority);
615 size = sizeof(whdr) + sizeof(code);
616 ioc = 2;
617 break;
618 default:
619 return;
David Howells248f2192016-09-08 11:10:12 +0100620 }
621
David Howells5e6ef4f2020-01-23 13:13:41 +0000622 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
623 msg.msg_namelen = srx.transport_len;
624
625 whdr.epoch = htonl(sp->hdr.epoch);
626 whdr.cid = htonl(sp->hdr.cid);
627 whdr.callNumber = htonl(sp->hdr.callNumber);
628 whdr.serviceId = htons(sp->hdr.serviceId);
629 whdr.flags = sp->hdr.flags;
630 whdr.flags ^= RXRPC_CLIENT_INITIATED;
631 whdr.flags &= RXRPC_CLIENT_INITIATED;
632
633 iov_iter_kvec(&msg.msg_iter, WRITE, iov, ioc, size);
634 ret = do_udp_sendmsg(local->socket, &msg, size);
635 if (ret < 0)
636 trace_rxrpc_tx_fail(local->debug_id, 0, ret,
637 rxrpc_tx_point_reject);
638 else
639 trace_rxrpc_tx_packet(local->debug_id, &whdr,
640 rxrpc_tx_point_reject);
641 }
David Howells248f2192016-09-08 11:10:12 +0100642}
David Howellsace45be2018-03-30 21:04:43 +0100643
644/*
645 * Send a VERSION reply to a peer as a keepalive.
646 */
647void rxrpc_send_keepalive(struct rxrpc_peer *peer)
648{
649 struct rxrpc_wire_header whdr;
650 struct msghdr msg;
651 struct kvec iov[2];
652 size_t len;
653 int ret;
654
655 _enter("");
656
657 msg.msg_name = &peer->srx.transport;
658 msg.msg_namelen = peer->srx.transport_len;
659 msg.msg_control = NULL;
660 msg.msg_controllen = 0;
661 msg.msg_flags = 0;
662
663 whdr.epoch = htonl(peer->local->rxnet->epoch);
664 whdr.cid = 0;
665 whdr.callNumber = 0;
666 whdr.seq = 0;
667 whdr.serial = 0;
668 whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
669 whdr.flags = RXRPC_LAST_PACKET;
670 whdr.userStatus = 0;
671 whdr.securityIndex = 0;
672 whdr._rsvd = 0;
673 whdr.serviceId = 0;
674
675 iov[0].iov_base = &whdr;
676 iov[0].iov_len = sizeof(whdr);
677 iov[1].iov_base = (char *)rxrpc_keepalive_string;
678 iov[1].iov_len = sizeof(rxrpc_keepalive_string);
679
680 len = iov[0].iov_len + iov[1].iov_len;
681
David Howellsed472b02022-03-22 11:07:20 +0000682 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
683 ret = do_udp_sendmsg(peer->local->socket, &msg, len);
David Howellsace45be2018-03-30 21:04:43 +0100684 if (ret < 0)
David Howells6b47fe12018-05-10 23:26:01 +0100685 trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100686 rxrpc_tx_point_version_keepalive);
687 else
688 trace_rxrpc_tx_packet(peer->debug_id, &whdr,
689 rxrpc_tx_point_version_keepalive);
David Howellsace45be2018-03-30 21:04:43 +0100690
David Howells330bdcf2018-08-08 11:30:02 +0100691 peer->last_tx_at = ktime_get_seconds();
David Howellsace45be2018-03-30 21:04:43 +0100692 _leave("");
693}
David Howellscf37b592022-03-31 23:55:08 +0100694
695/*
696 * Schedule an instant Tx resend.
697 */
698static inline void rxrpc_instant_resend(struct rxrpc_call *call,
699 struct rxrpc_txbuf *txb)
700{
David Howells96b40592022-10-27 11:25:55 +0100701 if (!__rxrpc_call_is_complete(call))
David Howellscf37b592022-03-31 23:55:08 +0100702 kdebug("resend");
703}
704
705/*
706 * Transmit one packet.
707 */
708void rxrpc_transmit_one(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
709{
710 int ret;
711
712 ret = rxrpc_send_data_packet(call, txb);
713 if (ret < 0) {
714 switch (ret) {
715 case -ENETUNREACH:
716 case -EHOSTUNREACH:
717 case -ECONNREFUSED:
718 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
719 0, ret);
720 break;
721 default:
722 _debug("need instant resend %d", ret);
723 rxrpc_instant_resend(call, txb);
724 }
725 } else {
726 unsigned long now = jiffies;
727 unsigned long resend_at = now + call->peer->rto_j;
728
729 WRITE_ONCE(call->resend_at, resend_at);
730 rxrpc_reduce_call_timer(call, resend_at, now,
731 rxrpc_timer_set_for_send);
732 }
733}