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>
17 #include "ar-internal.h"
19 extern int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
21 static ssize_t do_udp_sendmsg(struct socket *sk, struct msghdr *msg, size_t len)
23 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
24 struct sockaddr *sa = msg->msg_name;
26 if (sa->sa_family == AF_INET6)
27 return udpv6_sendmsg(sk->sk, msg, len);
29 return udp_sendmsg(sk->sk, msg, len);
32 struct rxrpc_abort_buffer {
33 struct rxrpc_wire_header whdr;
37 static const char rxrpc_keepalive_string[] = "";
40 * Increase Tx backoff on transmission failure and clear it on success.
42 static void rxrpc_tx_backoff(struct rxrpc_call *call, int ret)
45 u16 tx_backoff = READ_ONCE(call->tx_backoff);
48 WRITE_ONCE(call->tx_backoff, tx_backoff + 1);
50 WRITE_ONCE(call->tx_backoff, 0);
55 * Arrange for a keepalive ping a certain time after we last transmitted. This
56 * lets the far side know we're still interested in this call and helps keep
57 * the route through any intervening firewall open.
59 * Receiving a response to the ping will prevent the ->expect_rx_by timer from
62 static void rxrpc_set_keepalive(struct rxrpc_call *call)
64 unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
67 WRITE_ONCE(call->keepalive_at, keepalive_at);
68 rxrpc_reduce_call_timer(call, keepalive_at, now,
69 rxrpc_timer_set_for_keepalive);
73 * Fill out an ACK packet.
75 static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
76 struct rxrpc_call *call,
77 struct rxrpc_txbuf *txb)
79 struct rxrpc_ackinfo ackinfo;
81 rxrpc_seq_t window, wtop, wrap_point, ix, first;
86 u8 sack_buffer[sizeof(call->ackr_sack_table)] __aligned(8);
88 atomic_set(&call->ackr_nr_unacked, 0);
89 atomic_set(&call->ackr_nr_consumed, 0);
90 rxrpc_inc_stat(call->rxnet, stat_tx_ack_fill);
92 /* Barrier against rxrpc_input_data(). */
94 wtmp = atomic64_read_acquire(&call->ackr_window);
95 window = lower_32_bits(wtmp);
96 wtop = upper_32_bits(wtmp);
97 txb->ack.firstPacket = htonl(window);
100 if (after(wtop, window)) {
101 /* Try to copy the SACK ring locklessly. We can use the copy,
102 * only if the now-current top of the window didn't go past the
103 * previously read base - otherwise we can't know whether we
104 * have old data or new data.
106 memcpy(sack_buffer, call->ackr_sack_table, sizeof(sack_buffer));
107 wrap_point = window + RXRPC_SACK_SIZE - 1;
108 wtmp = atomic64_read_acquire(&call->ackr_window);
109 window = lower_32_bits(wtmp);
110 wtop = upper_32_bits(wtmp);
111 if (after(wtop, wrap_point)) {
116 /* The buffer is maintained as a ring with an invariant mapping
117 * between bit position and sequence number, so we'll probably
120 txb->ack.nAcks = wtop - window;
121 ix = window % RXRPC_SACK_SIZE;
122 first = sizeof(sack_buffer) - ix;
124 if (ix + txb->ack.nAcks <= RXRPC_SACK_SIZE) {
125 memcpy(txb->acks, sack_buffer + ix, txb->ack.nAcks);
127 memcpy(txb->acks, sack_buffer + ix, first);
128 memcpy(txb->acks + first, sack_buffer,
129 txb->ack.nAcks - first);
132 ackp += txb->ack.nAcks;
133 } else if (before(wtop, window)) {
134 pr_warn("ack window backward %x %x", window, wtop);
135 } else if (txb->ack.reason == RXRPC_ACK_DELAY) {
136 txb->ack.reason = RXRPC_ACK_IDLE;
139 mtu = conn->params.peer->if_mtu;
140 mtu -= conn->params.peer->hdrsize;
141 jmax = rxrpc_rx_jumbo_max;
142 qsize = (window - 1) - call->rx_consumed;
143 rsize = max_t(int, call->rx_winsize - qsize, 0);
144 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
145 ackinfo.maxMTU = htonl(mtu);
146 ackinfo.rwind = htonl(rsize);
147 ackinfo.jumbo_max = htonl(jmax);
152 memcpy(ackp, &ackinfo, sizeof(ackinfo));
153 return txb->ack.nAcks + 3 + sizeof(ackinfo);
157 * Record the beginning of an RTT probe.
159 static int rxrpc_begin_rtt_probe(struct rxrpc_call *call, rxrpc_serial_t serial,
160 enum rxrpc_rtt_tx_trace why)
162 unsigned long avail = call->rtt_avail;
165 if (!(avail & RXRPC_CALL_RTT_AVAIL_MASK))
168 rtt_slot = __ffs(avail & RXRPC_CALL_RTT_AVAIL_MASK);
169 if (!test_and_clear_bit(rtt_slot, &call->rtt_avail))
172 call->rtt_serial[rtt_slot] = serial;
173 call->rtt_sent_at[rtt_slot] = ktime_get_real();
174 smp_wmb(); /* Write data before avail bit */
175 set_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
177 trace_rxrpc_rtt_tx(call, why, rtt_slot, serial);
181 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_no_slot, rtt_slot, serial);
186 * Cancel an RTT probe.
188 static void rxrpc_cancel_rtt_probe(struct rxrpc_call *call,
189 rxrpc_serial_t serial, int rtt_slot)
191 if (rtt_slot != -1) {
192 clear_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
193 smp_wmb(); /* Clear pending bit before setting slot */
194 set_bit(rtt_slot, &call->rtt_avail);
195 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_cancel, rtt_slot, serial);
200 * Send an ACK call packet.
202 static int rxrpc_send_ack_packet(struct rxrpc_local *local, struct rxrpc_txbuf *txb)
204 struct rxrpc_connection *conn;
205 struct rxrpc_ack_buffer *pkt;
206 struct rxrpc_call *call = txb->call;
209 rxrpc_serial_t serial;
211 int ret, rtt_slot = -1;
213 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
218 msg.msg_name = &call->peer->srx.transport;
219 msg.msg_namelen = call->peer->srx.transport_len;
220 msg.msg_control = NULL;
221 msg.msg_controllen = 0;
224 if (txb->ack.reason == RXRPC_ACK_PING)
225 txb->wire.flags |= RXRPC_REQUEST_ACK;
227 if (txb->ack.reason == RXRPC_ACK_DELAY)
228 clear_bit(RXRPC_CALL_DELAY_ACK_PENDING, &call->flags);
229 if (txb->ack.reason == RXRPC_ACK_IDLE)
230 clear_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags);
232 n = rxrpc_fill_out_ack(conn, call, txb);
236 iov[0].iov_base = &txb->wire;
237 iov[0].iov_len = sizeof(txb->wire) + sizeof(txb->ack) + n;
238 len = iov[0].iov_len;
240 serial = atomic_inc_return(&conn->serial);
241 txb->wire.serial = htonl(serial);
242 trace_rxrpc_tx_ack(call->debug_id, serial,
243 ntohl(txb->ack.firstPacket),
244 ntohl(txb->ack.serial), txb->ack.reason, txb->ack.nAcks);
245 if (txb->ack_why == rxrpc_propose_ack_ping_for_lost_ack)
246 call->acks_lost_ping = serial;
248 if (txb->ack.reason == RXRPC_ACK_PING)
249 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_ping);
251 rxrpc_inc_stat(call->rxnet, stat_tx_ack_send);
253 /* Grab the highest received seq as late as possible */
254 txb->ack.previousPacket = htonl(call->rx_highest_seq);
256 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
257 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
258 call->peer->last_tx_at = ktime_get_seconds();
260 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
261 rxrpc_tx_point_call_ack);
263 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
264 rxrpc_tx_point_call_ack);
265 rxrpc_tx_backoff(call, ret);
267 if (call->state < RXRPC_CALL_COMPLETE) {
269 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
270 rxrpc_set_keepalive(call);
278 * ACK transmitter for a local endpoint. The UDP socket locks around each
279 * transmission, so we can only transmit one packet at a time, ACK, DATA or
282 void rxrpc_transmit_ack_packets(struct rxrpc_local *local)
287 trace_rxrpc_local(local->debug_id, rxrpc_local_tx_ack,
288 refcount_read(&local->ref), NULL);
290 if (list_empty(&local->ack_tx_queue))
293 spin_lock_bh(&local->ack_tx_lock);
294 list_splice_tail_init(&local->ack_tx_queue, &queue);
295 spin_unlock_bh(&local->ack_tx_lock);
297 while (!list_empty(&queue)) {
298 struct rxrpc_txbuf *txb =
299 list_entry(queue.next, struct rxrpc_txbuf, tx_link);
301 ret = rxrpc_send_ack_packet(local, txb);
302 if (ret < 0 && ret != -ECONNRESET) {
303 spin_lock_bh(&local->ack_tx_lock);
304 list_splice_init(&queue, &local->ack_tx_queue);
305 spin_unlock_bh(&local->ack_tx_lock);
309 list_del_init(&txb->tx_link);
310 rxrpc_put_call(txb->call, rxrpc_call_put);
311 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_ack_tx);
316 * Send an ABORT call packet.
318 int rxrpc_send_abort_packet(struct rxrpc_call *call)
320 struct rxrpc_connection *conn;
321 struct rxrpc_abort_buffer pkt;
324 rxrpc_serial_t serial;
327 /* Don't bother sending aborts for a client call once the server has
328 * hard-ACK'd all of its request data. After that point, we're not
329 * going to stop the operation proceeding, and whilst we might limit
330 * the reply, it's not worth it if we can send a new call on the same
331 * channel instead, thereby closing off this call.
333 if (rxrpc_is_client_call(call) &&
334 test_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags))
337 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
342 msg.msg_name = &call->peer->srx.transport;
343 msg.msg_namelen = call->peer->srx.transport_len;
344 msg.msg_control = NULL;
345 msg.msg_controllen = 0;
348 pkt.whdr.epoch = htonl(conn->proto.epoch);
349 pkt.whdr.cid = htonl(call->cid);
350 pkt.whdr.callNumber = htonl(call->call_id);
352 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
353 pkt.whdr.flags = conn->out_clientflag;
354 pkt.whdr.userStatus = 0;
355 pkt.whdr.securityIndex = call->security_ix;
357 pkt.whdr.serviceId = htons(call->service_id);
358 pkt.abort_code = htonl(call->abort_code);
360 iov[0].iov_base = &pkt;
361 iov[0].iov_len = sizeof(pkt);
363 serial = atomic_inc_return(&conn->serial);
364 pkt.whdr.serial = htonl(serial);
366 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
367 ret = do_udp_sendmsg(conn->params.local->socket, &msg, sizeof(pkt));
368 conn->params.peer->last_tx_at = ktime_get_seconds();
370 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
371 rxrpc_tx_point_call_abort);
373 trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
374 rxrpc_tx_point_call_abort);
375 rxrpc_tx_backoff(call, ret);
380 * send a packet through the transport endpoint
382 int rxrpc_send_data_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
384 enum rxrpc_req_ack_trace why;
385 struct rxrpc_connection *conn = call->conn;
388 rxrpc_serial_t serial;
390 int ret, rtt_slot = -1;
392 _enter("%x,{%d}", txb->seq, txb->len);
394 if (hlist_unhashed(&call->error_link)) {
395 spin_lock_bh(&call->peer->lock);
396 hlist_add_head_rcu(&call->error_link, &call->peer->error_targets);
397 spin_unlock_bh(&call->peer->lock);
400 /* Each transmission of a Tx packet needs a new serial number */
401 serial = atomic_inc_return(&conn->serial);
402 txb->wire.serial = htonl(serial);
404 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
406 txb->wire.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
408 iov[0].iov_base = &txb->wire;
409 iov[0].iov_len = sizeof(txb->wire) + txb->len;
410 len = iov[0].iov_len;
411 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
413 msg.msg_name = &call->peer->srx.transport;
414 msg.msg_namelen = call->peer->srx.transport_len;
415 msg.msg_control = NULL;
416 msg.msg_controllen = 0;
419 /* If our RTT cache needs working on, request an ACK. Also request
420 * ACKs if a DATA packet appears to have been lost.
422 * However, we mustn't request an ACK on the last reply packet of a
423 * service call, lest OpenAFS incorrectly send us an ACK with some
424 * soft-ACKs in it and then never follow up with a proper hard ACK.
426 if (txb->wire.flags & RXRPC_REQUEST_ACK)
427 why = rxrpc_reqack_already_on;
428 else if (test_bit(RXRPC_TXBUF_LAST, &txb->flags) && rxrpc_sending_to_client(txb))
429 why = rxrpc_reqack_no_srv_last;
430 else if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
431 why = rxrpc_reqack_ack_lost;
432 else if (test_bit(RXRPC_TXBUF_RESENT, &txb->flags))
433 why = rxrpc_reqack_retrans;
434 else if (call->cong_mode == RXRPC_CALL_SLOW_START && call->cong_cwnd <= 2)
435 why = rxrpc_reqack_slow_start;
436 else if (call->tx_winsize <= 2)
437 why = rxrpc_reqack_small_txwin;
438 else if (call->peer->rtt_count < 3 && txb->seq & 1)
439 why = rxrpc_reqack_more_rtt;
440 else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), ktime_get_real()))
441 why = rxrpc_reqack_old_rtt;
443 goto dont_set_request_ack;
445 rxrpc_inc_stat(call->rxnet, stat_why_req_ack[why]);
446 trace_rxrpc_req_ack(call->debug_id, txb->seq, why);
447 if (why != rxrpc_reqack_no_srv_last)
448 txb->wire.flags |= RXRPC_REQUEST_ACK;
449 dont_set_request_ack:
451 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
453 if ((lose++ & 7) == 7) {
455 trace_rxrpc_tx_data(call, txb->seq, serial,
457 test_bit(RXRPC_TXBUF_RESENT, &txb->flags),
463 trace_rxrpc_tx_data(call, txb->seq, serial, txb->wire.flags,
464 test_bit(RXRPC_TXBUF_RESENT, &txb->flags), false);
465 cmpxchg(&call->tx_transmitted, txb->seq - 1, txb->seq);
467 /* send the packet with the don't fragment bit set if we currently
468 * think it's small enough */
469 if (txb->len >= call->peer->maxdata)
470 goto send_fragmentable;
472 down_read(&conn->params.local->defrag_sem);
474 txb->last_sent = ktime_get_real();
475 if (txb->wire.flags & RXRPC_REQUEST_ACK)
476 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
478 /* send the packet by UDP
479 * - returns -EMSGSIZE if UDP would have to fragment the packet
480 * to go out of the interface
481 * - in which case, we'll have processed the ICMP error
482 * message and update the peer record
484 rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
485 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
486 conn->params.peer->last_tx_at = ktime_get_seconds();
488 up_read(&conn->params.local->defrag_sem);
490 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
491 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
492 rxrpc_tx_point_call_data_nofrag);
494 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
495 rxrpc_tx_point_call_data_nofrag);
498 rxrpc_tx_backoff(call, ret);
499 if (ret == -EMSGSIZE)
500 goto send_fragmentable;
504 call->tx_last_sent = txb->last_sent;
505 if (txb->wire.flags & RXRPC_REQUEST_ACK) {
506 call->peer->rtt_last_req = txb->last_sent;
507 if (call->peer->rtt_count > 1) {
508 unsigned long nowj = jiffies, ack_lost_at;
510 ack_lost_at = rxrpc_get_rto_backoff(call->peer, false);
512 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
513 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
514 rxrpc_timer_set_for_lost_ack);
519 !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
521 unsigned long nowj = jiffies, expect_rx_by;
523 expect_rx_by = nowj + call->next_rx_timo;
524 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
525 rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
526 rxrpc_timer_set_for_normal);
529 rxrpc_set_keepalive(call);
531 /* Cancel the call if the initial transmission fails,
532 * particularly if that's due to network routing issues that
533 * aren't going away anytime soon. The layer above can arrange
534 * the retransmission.
536 if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
537 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
541 _leave(" = %d [%u]", ret, call->peer->maxdata);
545 /* attempt to send this message with fragmentation enabled */
546 _debug("send fragment");
548 down_write(&conn->params.local->defrag_sem);
550 txb->last_sent = ktime_get_real();
551 if (txb->wire.flags & RXRPC_REQUEST_ACK)
552 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
554 switch (conn->params.local->srx.transport.family) {
557 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
559 rxrpc_inc_stat(call->rxnet, stat_tx_data_send_frag);
560 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
561 conn->params.peer->last_tx_at = ktime_get_seconds();
563 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
572 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
573 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
574 rxrpc_tx_point_call_data_frag);
576 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
577 rxrpc_tx_point_call_data_frag);
579 rxrpc_tx_backoff(call, ret);
581 up_write(&conn->params.local->defrag_sem);
586 * reject packets through the local endpoint
588 void rxrpc_reject_packets(struct rxrpc_local *local)
590 struct sockaddr_rxrpc srx;
591 struct rxrpc_skb_priv *sp;
592 struct rxrpc_wire_header whdr;
600 _enter("%d", local->debug_id);
602 iov[0].iov_base = &whdr;
603 iov[0].iov_len = sizeof(whdr);
604 iov[1].iov_base = &code;
605 iov[1].iov_len = sizeof(code);
607 msg.msg_name = &srx.transport;
608 msg.msg_control = NULL;
609 msg.msg_controllen = 0;
612 memset(&whdr, 0, sizeof(whdr));
614 while ((skb = skb_dequeue(&local->reject_queue))) {
615 rxrpc_see_skb(skb, rxrpc_skb_seen);
619 case RXRPC_SKB_MARK_REJECT_BUSY:
620 whdr.type = RXRPC_PACKET_TYPE_BUSY;
624 case RXRPC_SKB_MARK_REJECT_ABORT:
625 whdr.type = RXRPC_PACKET_TYPE_ABORT;
626 code = htonl(skb->priority);
627 size = sizeof(whdr) + sizeof(code);
631 rxrpc_free_skb(skb, rxrpc_skb_freed);
635 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
636 msg.msg_namelen = srx.transport_len;
638 whdr.epoch = htonl(sp->hdr.epoch);
639 whdr.cid = htonl(sp->hdr.cid);
640 whdr.callNumber = htonl(sp->hdr.callNumber);
641 whdr.serviceId = htons(sp->hdr.serviceId);
642 whdr.flags = sp->hdr.flags;
643 whdr.flags ^= RXRPC_CLIENT_INITIATED;
644 whdr.flags &= RXRPC_CLIENT_INITIATED;
646 iov_iter_kvec(&msg.msg_iter, WRITE, iov, ioc, size);
647 ret = do_udp_sendmsg(local->socket, &msg, size);
649 trace_rxrpc_tx_fail(local->debug_id, 0, ret,
650 rxrpc_tx_point_reject);
652 trace_rxrpc_tx_packet(local->debug_id, &whdr,
653 rxrpc_tx_point_reject);
656 rxrpc_free_skb(skb, rxrpc_skb_freed);
663 * Send a VERSION reply to a peer as a keepalive.
665 void rxrpc_send_keepalive(struct rxrpc_peer *peer)
667 struct rxrpc_wire_header whdr;
675 msg.msg_name = &peer->srx.transport;
676 msg.msg_namelen = peer->srx.transport_len;
677 msg.msg_control = NULL;
678 msg.msg_controllen = 0;
681 whdr.epoch = htonl(peer->local->rxnet->epoch);
686 whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
687 whdr.flags = RXRPC_LAST_PACKET;
689 whdr.securityIndex = 0;
693 iov[0].iov_base = &whdr;
694 iov[0].iov_len = sizeof(whdr);
695 iov[1].iov_base = (char *)rxrpc_keepalive_string;
696 iov[1].iov_len = sizeof(rxrpc_keepalive_string);
698 len = iov[0].iov_len + iov[1].iov_len;
700 _proto("Tx VERSION (keepalive)");
702 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
703 ret = do_udp_sendmsg(peer->local->socket, &msg, len);
705 trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
706 rxrpc_tx_point_version_keepalive);
708 trace_rxrpc_tx_packet(peer->debug_id, &whdr,
709 rxrpc_tx_point_version_keepalive);
711 peer->last_tx_at = ktime_get_seconds();