1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet transmission
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/net.h>
11 #include <linux/gfp.h>
12 #include <linux/skbuff.h>
13 #include <linux/export.h>
15 #include <net/af_rxrpc.h>
16 #include "ar-internal.h"
18 struct rxrpc_ack_buffer
{
19 struct rxrpc_wire_header whdr
;
20 struct rxrpc_ackpacket ack
;
23 struct rxrpc_ackinfo ackinfo
;
26 struct rxrpc_abort_buffer
{
27 struct rxrpc_wire_header whdr
;
31 static const char rxrpc_keepalive_string
[] = "";
34 * Increase Tx backoff on transmission failure and clear it on success.
36 static void rxrpc_tx_backoff(struct rxrpc_call
*call
, int ret
)
39 u16 tx_backoff
= READ_ONCE(call
->tx_backoff
);
42 WRITE_ONCE(call
->tx_backoff
, tx_backoff
+ 1);
44 WRITE_ONCE(call
->tx_backoff
, 0);
49 * Arrange for a keepalive ping a certain time after we last transmitted. This
50 * lets the far side know we're still interested in this call and helps keep
51 * the route through any intervening firewall open.
53 * Receiving a response to the ping will prevent the ->expect_rx_by timer from
56 static void rxrpc_set_keepalive(struct rxrpc_call
*call
)
58 unsigned long now
= jiffies
, keepalive_at
= call
->next_rx_timo
/ 6;
61 WRITE_ONCE(call
->keepalive_at
, keepalive_at
);
62 rxrpc_reduce_call_timer(call
, keepalive_at
, now
,
63 rxrpc_timer_set_for_keepalive
);
67 * Fill out an ACK packet.
69 static size_t rxrpc_fill_out_ack(struct rxrpc_connection
*conn
,
70 struct rxrpc_call
*call
,
71 struct rxrpc_ack_buffer
*pkt
,
72 rxrpc_seq_t
*_hard_ack
,
76 rxrpc_serial_t serial
;
77 rxrpc_seq_t hard_ack
, top
, seq
;
82 /* Barrier against rxrpc_input_data(). */
83 serial
= call
->ackr_serial
;
84 hard_ack
= READ_ONCE(call
->rx_hard_ack
);
85 top
= smp_load_acquire(&call
->rx_top
);
86 *_hard_ack
= hard_ack
;
89 pkt
->ack
.bufferSpace
= htons(8);
90 pkt
->ack
.maxSkew
= htons(0);
91 pkt
->ack
.firstPacket
= htonl(hard_ack
+ 1);
92 pkt
->ack
.previousPacket
= htonl(call
->ackr_prev_seq
);
93 pkt
->ack
.serial
= htonl(serial
);
94 pkt
->ack
.reason
= reason
;
95 pkt
->ack
.nAcks
= top
- hard_ack
;
97 if (reason
== RXRPC_ACK_PING
)
98 pkt
->whdr
.flags
|= RXRPC_REQUEST_ACK
;
100 if (after(top
, hard_ack
)) {
103 ix
= seq
& RXRPC_RXTX_BUFF_MASK
;
104 if (call
->rxtx_buffer
[ix
])
105 *ackp
++ = RXRPC_ACK_TYPE_ACK
;
107 *ackp
++ = RXRPC_ACK_TYPE_NACK
;
109 } while (before_eq(seq
, top
));
112 mtu
= conn
->params
.peer
->if_mtu
;
113 mtu
-= conn
->params
.peer
->hdrsize
;
114 jmax
= (call
->nr_jumbo_bad
> 3) ? 1 : rxrpc_rx_jumbo_max
;
115 pkt
->ackinfo
.rxMTU
= htonl(rxrpc_rx_mtu
);
116 pkt
->ackinfo
.maxMTU
= htonl(mtu
);
117 pkt
->ackinfo
.rwind
= htonl(call
->rx_winsize
);
118 pkt
->ackinfo
.jumbo_max
= htonl(jmax
);
123 return top
- hard_ack
+ 3;
127 * Send an ACK call packet.
129 int rxrpc_send_ack_packet(struct rxrpc_call
*call
, bool ping
,
130 rxrpc_serial_t
*_serial
)
132 struct rxrpc_connection
*conn
= NULL
;
133 struct rxrpc_ack_buffer
*pkt
;
136 rxrpc_serial_t serial
;
137 rxrpc_seq_t hard_ack
, top
;
142 spin_lock_bh(&call
->lock
);
144 conn
= rxrpc_get_connection_maybe(call
->conn
);
145 spin_unlock_bh(&call
->lock
);
149 pkt
= kzalloc(sizeof(*pkt
), GFP_KERNEL
);
151 rxrpc_put_connection(conn
);
155 msg
.msg_name
= &call
->peer
->srx
.transport
;
156 msg
.msg_namelen
= call
->peer
->srx
.transport_len
;
157 msg
.msg_control
= NULL
;
158 msg
.msg_controllen
= 0;
161 pkt
->whdr
.epoch
= htonl(conn
->proto
.epoch
);
162 pkt
->whdr
.cid
= htonl(call
->cid
);
163 pkt
->whdr
.callNumber
= htonl(call
->call_id
);
165 pkt
->whdr
.type
= RXRPC_PACKET_TYPE_ACK
;
166 pkt
->whdr
.flags
= RXRPC_SLOW_START_OK
| conn
->out_clientflag
;
167 pkt
->whdr
.userStatus
= 0;
168 pkt
->whdr
.securityIndex
= call
->security_ix
;
170 pkt
->whdr
.serviceId
= htons(call
->service_id
);
172 spin_lock_bh(&call
->lock
);
174 reason
= RXRPC_ACK_PING
;
176 reason
= call
->ackr_reason
;
177 if (!call
->ackr_reason
) {
178 spin_unlock_bh(&call
->lock
);
182 call
->ackr_reason
= 0;
184 n
= rxrpc_fill_out_ack(conn
, call
, pkt
, &hard_ack
, &top
, reason
);
186 spin_unlock_bh(&call
->lock
);
188 iov
[0].iov_base
= pkt
;
189 iov
[0].iov_len
= sizeof(pkt
->whdr
) + sizeof(pkt
->ack
) + n
;
190 iov
[1].iov_base
= &pkt
->ackinfo
;
191 iov
[1].iov_len
= sizeof(pkt
->ackinfo
);
192 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
194 serial
= atomic_inc_return(&conn
->serial
);
195 pkt
->whdr
.serial
= htonl(serial
);
196 trace_rxrpc_tx_ack(call
->debug_id
, serial
,
197 ntohl(pkt
->ack
.firstPacket
),
198 ntohl(pkt
->ack
.serial
),
199 pkt
->ack
.reason
, pkt
->ack
.nAcks
);
204 call
->ping_serial
= serial
;
206 /* We need to stick a time in before we send the packet in case
207 * the reply gets back before kernel_sendmsg() completes - but
208 * asking UDP to send the packet can take a relatively long
211 call
->ping_time
= ktime_get_real();
212 set_bit(RXRPC_CALL_PINGING
, &call
->flags
);
213 trace_rxrpc_rtt_tx(call
, rxrpc_rtt_tx_ping
, serial
);
216 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, 2, len
);
217 conn
->params
.peer
->last_tx_at
= ktime_get_seconds();
219 trace_rxrpc_tx_fail(call
->debug_id
, serial
, ret
,
220 rxrpc_tx_point_call_ack
);
222 trace_rxrpc_tx_packet(call
->debug_id
, &pkt
->whdr
,
223 rxrpc_tx_point_call_ack
);
224 rxrpc_tx_backoff(call
, ret
);
226 if (call
->state
< RXRPC_CALL_COMPLETE
) {
229 clear_bit(RXRPC_CALL_PINGING
, &call
->flags
);
230 rxrpc_propose_ACK(call
, pkt
->ack
.reason
,
231 ntohl(pkt
->ack
.serial
),
233 rxrpc_propose_ack_retry_tx
);
235 spin_lock_bh(&call
->lock
);
236 if (after(hard_ack
, call
->ackr_consumed
))
237 call
->ackr_consumed
= hard_ack
;
238 if (after(top
, call
->ackr_seen
))
239 call
->ackr_seen
= top
;
240 spin_unlock_bh(&call
->lock
);
243 rxrpc_set_keepalive(call
);
247 rxrpc_put_connection(conn
);
253 * Send an ABORT call packet.
255 int rxrpc_send_abort_packet(struct rxrpc_call
*call
)
257 struct rxrpc_connection
*conn
= NULL
;
258 struct rxrpc_abort_buffer pkt
;
261 rxrpc_serial_t serial
;
264 /* Don't bother sending aborts for a client call once the server has
265 * hard-ACK'd all of its request data. After that point, we're not
266 * going to stop the operation proceeding, and whilst we might limit
267 * the reply, it's not worth it if we can send a new call on the same
268 * channel instead, thereby closing off this call.
270 if (rxrpc_is_client_call(call
) &&
271 test_bit(RXRPC_CALL_TX_LAST
, &call
->flags
))
274 spin_lock_bh(&call
->lock
);
276 conn
= rxrpc_get_connection_maybe(call
->conn
);
277 spin_unlock_bh(&call
->lock
);
281 msg
.msg_name
= &call
->peer
->srx
.transport
;
282 msg
.msg_namelen
= call
->peer
->srx
.transport_len
;
283 msg
.msg_control
= NULL
;
284 msg
.msg_controllen
= 0;
287 pkt
.whdr
.epoch
= htonl(conn
->proto
.epoch
);
288 pkt
.whdr
.cid
= htonl(call
->cid
);
289 pkt
.whdr
.callNumber
= htonl(call
->call_id
);
291 pkt
.whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
292 pkt
.whdr
.flags
= conn
->out_clientflag
;
293 pkt
.whdr
.userStatus
= 0;
294 pkt
.whdr
.securityIndex
= call
->security_ix
;
296 pkt
.whdr
.serviceId
= htons(call
->service_id
);
297 pkt
.abort_code
= htonl(call
->abort_code
);
299 iov
[0].iov_base
= &pkt
;
300 iov
[0].iov_len
= sizeof(pkt
);
302 serial
= atomic_inc_return(&conn
->serial
);
303 pkt
.whdr
.serial
= htonl(serial
);
305 ret
= kernel_sendmsg(conn
->params
.local
->socket
,
306 &msg
, iov
, 1, sizeof(pkt
));
307 conn
->params
.peer
->last_tx_at
= ktime_get_seconds();
309 trace_rxrpc_tx_fail(call
->debug_id
, serial
, ret
,
310 rxrpc_tx_point_call_abort
);
312 trace_rxrpc_tx_packet(call
->debug_id
, &pkt
.whdr
,
313 rxrpc_tx_point_call_abort
);
314 rxrpc_tx_backoff(call
, ret
);
316 rxrpc_put_connection(conn
);
321 * send a packet through the transport endpoint
323 int rxrpc_send_data_packet(struct rxrpc_call
*call
, struct sk_buff
*skb
,
326 struct rxrpc_connection
*conn
= call
->conn
;
327 struct rxrpc_wire_header whdr
;
328 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
331 rxrpc_serial_t serial
;
335 _enter(",{%d}", skb
->len
);
337 /* Each transmission of a Tx packet needs a new serial number */
338 serial
= atomic_inc_return(&conn
->serial
);
340 whdr
.epoch
= htonl(conn
->proto
.epoch
);
341 whdr
.cid
= htonl(call
->cid
);
342 whdr
.callNumber
= htonl(call
->call_id
);
343 whdr
.seq
= htonl(sp
->hdr
.seq
);
344 whdr
.serial
= htonl(serial
);
345 whdr
.type
= RXRPC_PACKET_TYPE_DATA
;
346 whdr
.flags
= sp
->hdr
.flags
;
348 whdr
.securityIndex
= call
->security_ix
;
349 whdr
._rsvd
= htons(sp
->hdr
._rsvd
);
350 whdr
.serviceId
= htons(call
->service_id
);
352 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE
, &conn
->flags
) &&
354 whdr
.userStatus
= RXRPC_USERSTATUS_SERVICE_UPGRADE
;
356 iov
[0].iov_base
= &whdr
;
357 iov
[0].iov_len
= sizeof(whdr
);
358 iov
[1].iov_base
= skb
->head
;
359 iov
[1].iov_len
= skb
->len
;
360 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
362 msg
.msg_name
= &call
->peer
->srx
.transport
;
363 msg
.msg_namelen
= call
->peer
->srx
.transport_len
;
364 msg
.msg_control
= NULL
;
365 msg
.msg_controllen
= 0;
368 /* If our RTT cache needs working on, request an ACK. Also request
369 * ACKs if a DATA packet appears to have been lost.
371 * However, we mustn't request an ACK on the last reply packet of a
372 * service call, lest OpenAFS incorrectly send us an ACK with some
373 * soft-ACKs in it and then never follow up with a proper hard ACK.
375 if ((!(sp
->hdr
.flags
& RXRPC_LAST_PACKET
) ||
378 (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST
, &call
->events
) ||
380 call
->cong_mode
== RXRPC_CALL_SLOW_START
||
381 (call
->peer
->rtt_usage
< 3 && sp
->hdr
.seq
& 1) ||
382 ktime_before(ktime_add_ms(call
->peer
->rtt_last_req
, 1000),
384 whdr
.flags
|= RXRPC_REQUEST_ACK
;
386 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS
)) {
388 if ((lose
++ & 7) == 7) {
390 trace_rxrpc_tx_data(call
, sp
->hdr
.seq
, serial
,
391 whdr
.flags
, retrans
, true);
396 trace_rxrpc_tx_data(call
, sp
->hdr
.seq
, serial
, whdr
.flags
, retrans
,
399 /* send the packet with the don't fragment bit set if we currently
400 * think it's small enough */
401 if (iov
[1].iov_len
>= call
->peer
->maxdata
)
402 goto send_fragmentable
;
404 down_read(&conn
->params
.local
->defrag_sem
);
406 sp
->hdr
.serial
= serial
;
407 smp_wmb(); /* Set serial before timestamp */
408 skb
->tstamp
= ktime_get_real();
410 /* send the packet by UDP
411 * - returns -EMSGSIZE if UDP would have to fragment the packet
412 * to go out of the interface
413 * - in which case, we'll have processed the ICMP error
414 * message and update the peer record
416 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, 2, len
);
417 conn
->params
.peer
->last_tx_at
= ktime_get_seconds();
419 up_read(&conn
->params
.local
->defrag_sem
);
421 trace_rxrpc_tx_fail(call
->debug_id
, serial
, ret
,
422 rxrpc_tx_point_call_data_nofrag
);
424 trace_rxrpc_tx_packet(call
->debug_id
, &whdr
,
425 rxrpc_tx_point_call_data_nofrag
);
426 rxrpc_tx_backoff(call
, ret
);
427 if (ret
== -EMSGSIZE
)
428 goto send_fragmentable
;
432 if (whdr
.flags
& RXRPC_REQUEST_ACK
) {
433 call
->peer
->rtt_last_req
= skb
->tstamp
;
434 trace_rxrpc_rtt_tx(call
, rxrpc_rtt_tx_data
, serial
);
435 if (call
->peer
->rtt_usage
> 1) {
436 unsigned long nowj
= jiffies
, ack_lost_at
;
438 ack_lost_at
= nsecs_to_jiffies(2 * call
->peer
->rtt
);
443 WRITE_ONCE(call
->ack_lost_at
, ack_lost_at
);
444 rxrpc_reduce_call_timer(call
, ack_lost_at
, nowj
,
445 rxrpc_timer_set_for_lost_ack
);
449 if (sp
->hdr
.seq
== 1 &&
450 !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER
,
452 unsigned long nowj
= jiffies
, expect_rx_by
;
454 expect_rx_by
= nowj
+ call
->next_rx_timo
;
455 WRITE_ONCE(call
->expect_rx_by
, expect_rx_by
);
456 rxrpc_reduce_call_timer(call
, expect_rx_by
, nowj
,
457 rxrpc_timer_set_for_normal
);
460 rxrpc_set_keepalive(call
);
462 /* Cancel the call if the initial transmission fails,
463 * particularly if that's due to network routing issues that
464 * aren't going away anytime soon. The layer above can arrange
465 * the retransmission.
467 if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER
, &call
->flags
))
468 rxrpc_set_call_completion(call
, RXRPC_CALL_LOCAL_ERROR
,
472 _leave(" = %d [%u]", ret
, call
->peer
->maxdata
);
476 /* attempt to send this message with fragmentation enabled */
477 _debug("send fragment");
479 down_write(&conn
->params
.local
->defrag_sem
);
481 sp
->hdr
.serial
= serial
;
482 smp_wmb(); /* Set serial before timestamp */
483 skb
->tstamp
= ktime_get_real();
485 switch (conn
->params
.local
->srx
.transport
.family
) {
487 opt
= IP_PMTUDISC_DONT
;
488 ret
= kernel_setsockopt(conn
->params
.local
->socket
,
489 SOL_IP
, IP_MTU_DISCOVER
,
490 (char *)&opt
, sizeof(opt
));
492 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
,
494 conn
->params
.peer
->last_tx_at
= ktime_get_seconds();
496 opt
= IP_PMTUDISC_DO
;
497 kernel_setsockopt(conn
->params
.local
->socket
, SOL_IP
,
499 (char *)&opt
, sizeof(opt
));
503 #ifdef CONFIG_AF_RXRPC_IPV6
505 opt
= IPV6_PMTUDISC_DONT
;
506 ret
= kernel_setsockopt(conn
->params
.local
->socket
,
507 SOL_IPV6
, IPV6_MTU_DISCOVER
,
508 (char *)&opt
, sizeof(opt
));
510 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
,
512 conn
->params
.peer
->last_tx_at
= ktime_get_seconds();
514 opt
= IPV6_PMTUDISC_DO
;
515 kernel_setsockopt(conn
->params
.local
->socket
,
516 SOL_IPV6
, IPV6_MTU_DISCOVER
,
517 (char *)&opt
, sizeof(opt
));
527 trace_rxrpc_tx_fail(call
->debug_id
, serial
, ret
,
528 rxrpc_tx_point_call_data_frag
);
530 trace_rxrpc_tx_packet(call
->debug_id
, &whdr
,
531 rxrpc_tx_point_call_data_frag
);
532 rxrpc_tx_backoff(call
, ret
);
534 up_write(&conn
->params
.local
->defrag_sem
);
539 * reject packets through the local endpoint
541 void rxrpc_reject_packets(struct rxrpc_local
*local
)
543 struct sockaddr_rxrpc srx
;
544 struct rxrpc_skb_priv
*sp
;
545 struct rxrpc_wire_header whdr
;
553 _enter("%d", local
->debug_id
);
555 iov
[0].iov_base
= &whdr
;
556 iov
[0].iov_len
= sizeof(whdr
);
557 iov
[1].iov_base
= &code
;
558 iov
[1].iov_len
= sizeof(code
);
560 msg
.msg_name
= &srx
.transport
;
561 msg
.msg_control
= NULL
;
562 msg
.msg_controllen
= 0;
565 memset(&whdr
, 0, sizeof(whdr
));
567 while ((skb
= skb_dequeue(&local
->reject_queue
))) {
568 rxrpc_see_skb(skb
, rxrpc_skb_seen
);
572 case RXRPC_SKB_MARK_REJECT_BUSY
:
573 whdr
.type
= RXRPC_PACKET_TYPE_BUSY
;
577 case RXRPC_SKB_MARK_REJECT_ABORT
:
578 whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
579 code
= htonl(skb
->priority
);
580 size
= sizeof(whdr
) + sizeof(code
);
584 rxrpc_free_skb(skb
, rxrpc_skb_freed
);
588 if (rxrpc_extract_addr_from_skb(&srx
, skb
) == 0) {
589 msg
.msg_namelen
= srx
.transport_len
;
591 whdr
.epoch
= htonl(sp
->hdr
.epoch
);
592 whdr
.cid
= htonl(sp
->hdr
.cid
);
593 whdr
.callNumber
= htonl(sp
->hdr
.callNumber
);
594 whdr
.serviceId
= htons(sp
->hdr
.serviceId
);
595 whdr
.flags
= sp
->hdr
.flags
;
596 whdr
.flags
^= RXRPC_CLIENT_INITIATED
;
597 whdr
.flags
&= RXRPC_CLIENT_INITIATED
;
599 ret
= kernel_sendmsg(local
->socket
, &msg
,
602 trace_rxrpc_tx_fail(local
->debug_id
, 0, ret
,
603 rxrpc_tx_point_reject
);
605 trace_rxrpc_tx_packet(local
->debug_id
, &whdr
,
606 rxrpc_tx_point_reject
);
609 rxrpc_free_skb(skb
, rxrpc_skb_freed
);
616 * Send a VERSION reply to a peer as a keepalive.
618 void rxrpc_send_keepalive(struct rxrpc_peer
*peer
)
620 struct rxrpc_wire_header whdr
;
628 msg
.msg_name
= &peer
->srx
.transport
;
629 msg
.msg_namelen
= peer
->srx
.transport_len
;
630 msg
.msg_control
= NULL
;
631 msg
.msg_controllen
= 0;
634 whdr
.epoch
= htonl(peer
->local
->rxnet
->epoch
);
639 whdr
.type
= RXRPC_PACKET_TYPE_VERSION
; /* Not client-initiated */
640 whdr
.flags
= RXRPC_LAST_PACKET
;
642 whdr
.securityIndex
= 0;
646 iov
[0].iov_base
= &whdr
;
647 iov
[0].iov_len
= sizeof(whdr
);
648 iov
[1].iov_base
= (char *)rxrpc_keepalive_string
;
649 iov
[1].iov_len
= sizeof(rxrpc_keepalive_string
);
651 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
653 _proto("Tx VERSION (keepalive)");
655 ret
= kernel_sendmsg(peer
->local
->socket
, &msg
, iov
, 2, len
);
657 trace_rxrpc_tx_fail(peer
->debug_id
, 0, ret
,
658 rxrpc_tx_point_version_keepalive
);
660 trace_rxrpc_tx_packet(peer
->debug_id
, &whdr
,
661 rxrpc_tx_point_version_keepalive
);
663 peer
->last_tx_at
= ktime_get_seconds();