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,
78 rxrpc_seq_t *_hard_ack,
81 struct rxrpc_ackinfo ackinfo;
83 rxrpc_seq_t hard_ack, top, seq;
88 tmp = atomic_xchg(&call->ackr_nr_unacked, 0);
89 tmp |= atomic_xchg(&call->ackr_nr_consumed, 0);
90 if (!tmp && (txb->ack.reason == RXRPC_ACK_DELAY ||
91 txb->ack.reason == RXRPC_ACK_IDLE)) {
92 rxrpc_inc_stat(call->rxnet, stat_tx_ack_skip);
96 rxrpc_inc_stat(call->rxnet, stat_tx_ack_fill);
98 /* Barrier against rxrpc_input_data(). */
99 hard_ack = READ_ONCE(call->rx_hard_ack);
100 top = smp_load_acquire(&call->rx_top);
101 *_hard_ack = hard_ack;
104 txb->ack.firstPacket = htonl(hard_ack + 1);
105 txb->ack.previousPacket = htonl(call->ackr_highest_seq);
106 txb->ack.nAcks = top - hard_ack;
108 if (txb->ack.nAcks) {
111 ix = seq & RXRPC_RXTX_BUFF_MASK;
112 if (call->rxtx_buffer[ix])
113 *ackp++ = RXRPC_ACK_TYPE_ACK;
115 *ackp++ = RXRPC_ACK_TYPE_NACK;
117 } while (before_eq(seq, top));
118 } else if (txb->ack.reason == RXRPC_ACK_DELAY) {
119 txb->ack.reason = RXRPC_ACK_IDLE;
122 mtu = conn->params.peer->if_mtu;
123 mtu -= conn->params.peer->hdrsize;
124 jmax = rxrpc_rx_jumbo_max;
125 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
126 ackinfo.maxMTU = htonl(mtu);
127 ackinfo.rwind = htonl(call->rx_winsize);
128 ackinfo.jumbo_max = htonl(jmax);
133 memcpy(ackp, &ackinfo, sizeof(ackinfo));
134 return top - hard_ack + 3 + sizeof(ackinfo);
138 * Record the beginning of an RTT probe.
140 static int rxrpc_begin_rtt_probe(struct rxrpc_call *call, rxrpc_serial_t serial,
141 enum rxrpc_rtt_tx_trace why)
143 unsigned long avail = call->rtt_avail;
146 if (!(avail & RXRPC_CALL_RTT_AVAIL_MASK))
149 rtt_slot = __ffs(avail & RXRPC_CALL_RTT_AVAIL_MASK);
150 if (!test_and_clear_bit(rtt_slot, &call->rtt_avail))
153 call->rtt_serial[rtt_slot] = serial;
154 call->rtt_sent_at[rtt_slot] = ktime_get_real();
155 smp_wmb(); /* Write data before avail bit */
156 set_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
158 trace_rxrpc_rtt_tx(call, why, rtt_slot, serial);
162 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_no_slot, rtt_slot, serial);
167 * Cancel an RTT probe.
169 static void rxrpc_cancel_rtt_probe(struct rxrpc_call *call,
170 rxrpc_serial_t serial, int rtt_slot)
172 if (rtt_slot != -1) {
173 clear_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
174 smp_wmb(); /* Clear pending bit before setting slot */
175 set_bit(rtt_slot, &call->rtt_avail);
176 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_cancel, rtt_slot, serial);
181 * Send an ACK call packet.
183 static int rxrpc_send_ack_packet(struct rxrpc_local *local, struct rxrpc_txbuf *txb)
185 struct rxrpc_connection *conn;
186 struct rxrpc_ack_buffer *pkt;
187 struct rxrpc_call *call = txb->call;
190 rxrpc_serial_t serial;
191 rxrpc_seq_t hard_ack, top;
193 int ret, rtt_slot = -1;
195 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
200 msg.msg_name = &call->peer->srx.transport;
201 msg.msg_namelen = call->peer->srx.transport_len;
202 msg.msg_control = NULL;
203 msg.msg_controllen = 0;
206 if (txb->ack.reason == RXRPC_ACK_PING)
207 txb->wire.flags |= RXRPC_REQUEST_ACK;
209 if (txb->ack.reason == RXRPC_ACK_DELAY)
210 clear_bit(RXRPC_CALL_DELAY_ACK_PENDING, &call->flags);
211 if (txb->ack.reason == RXRPC_ACK_IDLE)
212 clear_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags);
214 spin_lock_bh(&call->lock);
215 n = rxrpc_fill_out_ack(conn, call, txb, &hard_ack, &top);
216 spin_unlock_bh(&call->lock);
222 iov[0].iov_base = &txb->wire;
223 iov[0].iov_len = sizeof(txb->wire) + sizeof(txb->ack) + n;
224 len = iov[0].iov_len;
226 serial = atomic_inc_return(&conn->serial);
227 txb->wire.serial = htonl(serial);
228 trace_rxrpc_tx_ack(call->debug_id, serial,
229 ntohl(txb->ack.firstPacket),
230 ntohl(txb->ack.serial), txb->ack.reason, txb->ack.nAcks);
231 if (txb->ack_why == rxrpc_propose_ack_ping_for_lost_ack)
232 call->acks_lost_ping = serial;
234 if (txb->ack.reason == RXRPC_ACK_PING)
235 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_ping);
237 rxrpc_inc_stat(call->rxnet, stat_tx_ack_send);
239 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
240 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
241 call->peer->last_tx_at = ktime_get_seconds();
243 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
244 rxrpc_tx_point_call_ack);
246 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
247 rxrpc_tx_point_call_ack);
248 rxrpc_tx_backoff(call, ret);
250 if (call->state < RXRPC_CALL_COMPLETE) {
252 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
253 rxrpc_set_keepalive(call);
261 * ACK transmitter for a local endpoint. The UDP socket locks around each
262 * transmission, so we can only transmit one packet at a time, ACK, DATA or
265 void rxrpc_transmit_ack_packets(struct rxrpc_local *local)
270 trace_rxrpc_local(local->debug_id, rxrpc_local_tx_ack,
271 refcount_read(&local->ref), NULL);
273 if (list_empty(&local->ack_tx_queue))
276 spin_lock_bh(&local->ack_tx_lock);
277 list_splice_tail_init(&local->ack_tx_queue, &queue);
278 spin_unlock_bh(&local->ack_tx_lock);
280 while (!list_empty(&queue)) {
281 struct rxrpc_txbuf *txb =
282 list_entry(queue.next, struct rxrpc_txbuf, tx_link);
284 ret = rxrpc_send_ack_packet(local, txb);
285 if (ret < 0 && ret != -ECONNRESET) {
286 spin_lock_bh(&local->ack_tx_lock);
287 list_splice_init(&queue, &local->ack_tx_queue);
288 spin_unlock_bh(&local->ack_tx_lock);
292 list_del_init(&txb->tx_link);
293 rxrpc_put_call(txb->call, rxrpc_call_put);
294 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_ack_tx);
299 * Send an ABORT call packet.
301 int rxrpc_send_abort_packet(struct rxrpc_call *call)
303 struct rxrpc_connection *conn;
304 struct rxrpc_abort_buffer pkt;
307 rxrpc_serial_t serial;
310 /* Don't bother sending aborts for a client call once the server has
311 * hard-ACK'd all of its request data. After that point, we're not
312 * going to stop the operation proceeding, and whilst we might limit
313 * the reply, it's not worth it if we can send a new call on the same
314 * channel instead, thereby closing off this call.
316 if (rxrpc_is_client_call(call) &&
317 test_bit(RXRPC_CALL_TX_LAST, &call->flags))
320 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
325 msg.msg_name = &call->peer->srx.transport;
326 msg.msg_namelen = call->peer->srx.transport_len;
327 msg.msg_control = NULL;
328 msg.msg_controllen = 0;
331 pkt.whdr.epoch = htonl(conn->proto.epoch);
332 pkt.whdr.cid = htonl(call->cid);
333 pkt.whdr.callNumber = htonl(call->call_id);
335 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
336 pkt.whdr.flags = conn->out_clientflag;
337 pkt.whdr.userStatus = 0;
338 pkt.whdr.securityIndex = call->security_ix;
340 pkt.whdr.serviceId = htons(call->service_id);
341 pkt.abort_code = htonl(call->abort_code);
343 iov[0].iov_base = &pkt;
344 iov[0].iov_len = sizeof(pkt);
346 serial = atomic_inc_return(&conn->serial);
347 pkt.whdr.serial = htonl(serial);
349 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
350 ret = do_udp_sendmsg(conn->params.local->socket, &msg, sizeof(pkt));
351 conn->params.peer->last_tx_at = ktime_get_seconds();
353 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
354 rxrpc_tx_point_call_abort);
356 trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
357 rxrpc_tx_point_call_abort);
358 rxrpc_tx_backoff(call, ret);
363 * send a packet through the transport endpoint
365 int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
368 enum rxrpc_req_ack_trace why;
369 struct rxrpc_connection *conn = call->conn;
370 struct rxrpc_wire_header whdr;
371 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
374 rxrpc_serial_t serial;
376 int ret, rtt_slot = -1;
378 _enter(",{%d}", skb->len);
380 if (hlist_unhashed(&call->error_link)) {
381 spin_lock_bh(&call->peer->lock);
382 hlist_add_head_rcu(&call->error_link, &call->peer->error_targets);
383 spin_unlock_bh(&call->peer->lock);
386 /* Each transmission of a Tx packet needs a new serial number */
387 serial = atomic_inc_return(&conn->serial);
389 whdr.epoch = htonl(conn->proto.epoch);
390 whdr.cid = htonl(call->cid);
391 whdr.callNumber = htonl(call->call_id);
392 whdr.seq = htonl(sp->hdr.seq);
393 whdr.serial = htonl(serial);
394 whdr.type = RXRPC_PACKET_TYPE_DATA;
395 whdr.flags = sp->hdr.flags;
397 whdr.securityIndex = call->security_ix;
398 whdr._rsvd = htons(sp->hdr._rsvd);
399 whdr.serviceId = htons(call->service_id);
401 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
403 whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
405 iov[0].iov_base = &whdr;
406 iov[0].iov_len = sizeof(whdr);
407 iov[1].iov_base = skb->head;
408 iov[1].iov_len = skb->len;
409 len = iov[0].iov_len + iov[1].iov_len;
410 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
412 msg.msg_name = &call->peer->srx.transport;
413 msg.msg_namelen = call->peer->srx.transport_len;
414 msg.msg_control = NULL;
415 msg.msg_controllen = 0;
418 /* If our RTT cache needs working on, request an ACK. Also request
419 * ACKs if a DATA packet appears to have been lost.
421 * However, we mustn't request an ACK on the last reply packet of a
422 * service call, lest OpenAFS incorrectly send us an ACK with some
423 * soft-ACKs in it and then never follow up with a proper hard ACK.
425 if (whdr.flags & RXRPC_REQUEST_ACK)
426 why = rxrpc_reqack_already_on;
427 else if ((whdr.flags & RXRPC_LAST_PACKET) && rxrpc_to_client(sp))
428 why = rxrpc_reqack_no_srv_last;
429 else if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
430 why = rxrpc_reqack_ack_lost;
432 why = rxrpc_reqack_retrans;
433 else if (call->cong_mode == RXRPC_CALL_SLOW_START && call->cong_cwnd <= 2)
434 why = rxrpc_reqack_slow_start;
435 else if (call->tx_winsize <= 2)
436 why = rxrpc_reqack_small_txwin;
437 else if (call->peer->rtt_count < 3 && sp->hdr.seq & 1)
438 why = rxrpc_reqack_more_rtt;
439 else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), ktime_get_real()))
440 why = rxrpc_reqack_old_rtt;
442 goto dont_set_request_ack;
444 rxrpc_inc_stat(call->rxnet, stat_why_req_ack[why]);
445 trace_rxrpc_req_ack(call->debug_id, sp->hdr.seq, why);
446 if (why != rxrpc_reqack_no_srv_last)
447 whdr.flags |= RXRPC_REQUEST_ACK;
448 dont_set_request_ack:
450 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
452 if ((lose++ & 7) == 7) {
454 trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
455 whdr.flags, retrans, true);
460 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, retrans,
463 /* send the packet with the don't fragment bit set if we currently
464 * think it's small enough */
465 if (iov[1].iov_len >= call->peer->maxdata)
466 goto send_fragmentable;
468 down_read(&conn->params.local->defrag_sem);
470 sp->hdr.serial = serial;
471 smp_wmb(); /* Set serial before timestamp */
472 skb->tstamp = ktime_get_real();
473 if (whdr.flags & RXRPC_REQUEST_ACK)
474 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
476 /* send the packet by UDP
477 * - returns -EMSGSIZE if UDP would have to fragment the packet
478 * to go out of the interface
479 * - in which case, we'll have processed the ICMP error
480 * message and update the peer record
482 rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
483 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
484 conn->params.peer->last_tx_at = ktime_get_seconds();
486 up_read(&conn->params.local->defrag_sem);
488 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
489 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
490 rxrpc_tx_point_call_data_nofrag);
492 trace_rxrpc_tx_packet(call->debug_id, &whdr,
493 rxrpc_tx_point_call_data_nofrag);
496 rxrpc_tx_backoff(call, ret);
497 if (ret == -EMSGSIZE)
498 goto send_fragmentable;
502 if (whdr.flags & RXRPC_REQUEST_ACK) {
503 call->peer->rtt_last_req = skb->tstamp;
504 if (call->peer->rtt_count > 1) {
505 unsigned long nowj = jiffies, ack_lost_at;
507 ack_lost_at = rxrpc_get_rto_backoff(call->peer, false);
509 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
510 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
511 rxrpc_timer_set_for_lost_ack);
515 if (sp->hdr.seq == 1 &&
516 !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
518 unsigned long nowj = jiffies, expect_rx_by;
520 expect_rx_by = nowj + call->next_rx_timo;
521 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
522 rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
523 rxrpc_timer_set_for_normal);
526 rxrpc_set_keepalive(call);
528 /* Cancel the call if the initial transmission fails,
529 * particularly if that's due to network routing issues that
530 * aren't going away anytime soon. The layer above can arrange
531 * the retransmission.
533 if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
534 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
538 _leave(" = %d [%u]", ret, call->peer->maxdata);
542 /* attempt to send this message with fragmentation enabled */
543 _debug("send fragment");
545 down_write(&conn->params.local->defrag_sem);
547 sp->hdr.serial = serial;
548 smp_wmb(); /* Set serial before timestamp */
549 skb->tstamp = ktime_get_real();
550 if (whdr.flags & RXRPC_REQUEST_ACK)
551 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
553 switch (conn->params.local->srx.transport.family) {
556 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
558 rxrpc_inc_stat(call->rxnet, stat_tx_data_send_frag);
559 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
560 conn->params.peer->last_tx_at = ktime_get_seconds();
562 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
571 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
572 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
573 rxrpc_tx_point_call_data_frag);
575 trace_rxrpc_tx_packet(call->debug_id, &whdr,
576 rxrpc_tx_point_call_data_frag);
578 rxrpc_tx_backoff(call, ret);
580 up_write(&conn->params.local->defrag_sem);
585 * reject packets through the local endpoint
587 void rxrpc_reject_packets(struct rxrpc_local *local)
589 struct sockaddr_rxrpc srx;
590 struct rxrpc_skb_priv *sp;
591 struct rxrpc_wire_header whdr;
599 _enter("%d", local->debug_id);
601 iov[0].iov_base = &whdr;
602 iov[0].iov_len = sizeof(whdr);
603 iov[1].iov_base = &code;
604 iov[1].iov_len = sizeof(code);
606 msg.msg_name = &srx.transport;
607 msg.msg_control = NULL;
608 msg.msg_controllen = 0;
611 memset(&whdr, 0, sizeof(whdr));
613 while ((skb = skb_dequeue(&local->reject_queue))) {
614 rxrpc_see_skb(skb, rxrpc_skb_seen);
618 case RXRPC_SKB_MARK_REJECT_BUSY:
619 whdr.type = RXRPC_PACKET_TYPE_BUSY;
623 case RXRPC_SKB_MARK_REJECT_ABORT:
624 whdr.type = RXRPC_PACKET_TYPE_ABORT;
625 code = htonl(skb->priority);
626 size = sizeof(whdr) + sizeof(code);
630 rxrpc_free_skb(skb, rxrpc_skb_freed);
634 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
635 msg.msg_namelen = srx.transport_len;
637 whdr.epoch = htonl(sp->hdr.epoch);
638 whdr.cid = htonl(sp->hdr.cid);
639 whdr.callNumber = htonl(sp->hdr.callNumber);
640 whdr.serviceId = htons(sp->hdr.serviceId);
641 whdr.flags = sp->hdr.flags;
642 whdr.flags ^= RXRPC_CLIENT_INITIATED;
643 whdr.flags &= RXRPC_CLIENT_INITIATED;
645 iov_iter_kvec(&msg.msg_iter, WRITE, iov, ioc, size);
646 ret = do_udp_sendmsg(local->socket, &msg, size);
648 trace_rxrpc_tx_fail(local->debug_id, 0, ret,
649 rxrpc_tx_point_reject);
651 trace_rxrpc_tx_packet(local->debug_id, &whdr,
652 rxrpc_tx_point_reject);
655 rxrpc_free_skb(skb, rxrpc_skb_freed);
662 * Send a VERSION reply to a peer as a keepalive.
664 void rxrpc_send_keepalive(struct rxrpc_peer *peer)
666 struct rxrpc_wire_header whdr;
674 msg.msg_name = &peer->srx.transport;
675 msg.msg_namelen = peer->srx.transport_len;
676 msg.msg_control = NULL;
677 msg.msg_controllen = 0;
680 whdr.epoch = htonl(peer->local->rxnet->epoch);
685 whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
686 whdr.flags = RXRPC_LAST_PACKET;
688 whdr.securityIndex = 0;
692 iov[0].iov_base = &whdr;
693 iov[0].iov_len = sizeof(whdr);
694 iov[1].iov_base = (char *)rxrpc_keepalive_string;
695 iov[1].iov_len = sizeof(rxrpc_keepalive_string);
697 len = iov[0].iov_len + iov[1].iov_len;
699 _proto("Tx VERSION (keepalive)");
701 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
702 ret = do_udp_sendmsg(peer->local->socket, &msg, len);
704 trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
705 rxrpc_tx_point_version_keepalive);
707 trace_rxrpc_tx_packet(peer->debug_id, &whdr,
708 rxrpc_tx_point_version_keepalive);
710 peer->last_tx_at = ktime_get_seconds();