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));
120 mtu = conn->params.peer->if_mtu;
121 mtu -= conn->params.peer->hdrsize;
122 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
123 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
124 ackinfo.maxMTU = htonl(mtu);
125 ackinfo.rwind = htonl(call->rx_winsize);
126 ackinfo.jumbo_max = htonl(jmax);
131 memcpy(ackp, &ackinfo, sizeof(ackinfo));
132 return top - hard_ack + 3 + sizeof(ackinfo);
136 * Record the beginning of an RTT probe.
138 static int rxrpc_begin_rtt_probe(struct rxrpc_call *call, rxrpc_serial_t serial,
139 enum rxrpc_rtt_tx_trace why)
141 unsigned long avail = call->rtt_avail;
144 if (!(avail & RXRPC_CALL_RTT_AVAIL_MASK))
147 rtt_slot = __ffs(avail & RXRPC_CALL_RTT_AVAIL_MASK);
148 if (!test_and_clear_bit(rtt_slot, &call->rtt_avail))
151 call->rtt_serial[rtt_slot] = serial;
152 call->rtt_sent_at[rtt_slot] = ktime_get_real();
153 smp_wmb(); /* Write data before avail bit */
154 set_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
156 trace_rxrpc_rtt_tx(call, why, rtt_slot, serial);
160 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_no_slot, rtt_slot, serial);
165 * Cancel an RTT probe.
167 static void rxrpc_cancel_rtt_probe(struct rxrpc_call *call,
168 rxrpc_serial_t serial, int rtt_slot)
170 if (rtt_slot != -1) {
171 clear_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
172 smp_wmb(); /* Clear pending bit before setting slot */
173 set_bit(rtt_slot, &call->rtt_avail);
174 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_cancel, rtt_slot, serial);
179 * Send an ACK call packet.
181 static int rxrpc_send_ack_packet(struct rxrpc_local *local, struct rxrpc_txbuf *txb)
183 struct rxrpc_connection *conn;
184 struct rxrpc_ack_buffer *pkt;
185 struct rxrpc_call *call = txb->call;
188 rxrpc_serial_t serial;
189 rxrpc_seq_t hard_ack, top;
191 int ret, rtt_slot = -1;
193 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
198 msg.msg_name = &call->peer->srx.transport;
199 msg.msg_namelen = call->peer->srx.transport_len;
200 msg.msg_control = NULL;
201 msg.msg_controllen = 0;
204 if (txb->ack.reason == RXRPC_ACK_PING)
205 txb->wire.flags |= RXRPC_REQUEST_ACK;
207 spin_lock_bh(&call->lock);
208 n = rxrpc_fill_out_ack(conn, call, txb, &hard_ack, &top);
209 spin_unlock_bh(&call->lock);
215 iov[0].iov_base = &txb->wire;
216 iov[0].iov_len = sizeof(txb->wire) + sizeof(txb->ack) + n;
217 len = iov[0].iov_len;
219 serial = atomic_inc_return(&conn->serial);
220 txb->wire.serial = htonl(serial);
221 trace_rxrpc_tx_ack(call->debug_id, serial,
222 ntohl(txb->ack.firstPacket),
223 ntohl(txb->ack.serial), txb->ack.reason, txb->ack.nAcks);
224 if (txb->ack_why == rxrpc_propose_ack_ping_for_lost_ack)
225 call->acks_lost_ping = serial;
227 if (txb->ack.reason == RXRPC_ACK_PING)
228 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_ping);
230 rxrpc_inc_stat(call->rxnet, stat_tx_ack_send);
232 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
233 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
234 call->peer->last_tx_at = ktime_get_seconds();
236 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
237 rxrpc_tx_point_call_ack);
239 trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
240 rxrpc_tx_point_call_ack);
241 rxrpc_tx_backoff(call, ret);
243 if (call->state < RXRPC_CALL_COMPLETE) {
245 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
246 rxrpc_set_keepalive(call);
254 * ACK transmitter for a local endpoint. The UDP socket locks around each
255 * transmission, so we can only transmit one packet at a time, ACK, DATA or
258 void rxrpc_transmit_ack_packets(struct rxrpc_local *local)
263 trace_rxrpc_local(local->debug_id, rxrpc_local_tx_ack,
264 refcount_read(&local->ref), NULL);
266 if (list_empty(&local->ack_tx_queue))
269 spin_lock_bh(&local->ack_tx_lock);
270 list_splice_tail_init(&local->ack_tx_queue, &queue);
271 spin_unlock_bh(&local->ack_tx_lock);
273 while (!list_empty(&queue)) {
274 struct rxrpc_txbuf *txb =
275 list_entry(queue.next, struct rxrpc_txbuf, tx_link);
277 ret = rxrpc_send_ack_packet(local, txb);
278 if (ret < 0 && ret != -ECONNRESET) {
279 spin_lock_bh(&local->ack_tx_lock);
280 list_splice_init(&queue, &local->ack_tx_queue);
281 spin_unlock_bh(&local->ack_tx_lock);
285 list_del_init(&txb->tx_link);
286 rxrpc_put_call(txb->call, rxrpc_call_put);
287 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_ack_tx);
292 * Send an ABORT call packet.
294 int rxrpc_send_abort_packet(struct rxrpc_call *call)
296 struct rxrpc_connection *conn;
297 struct rxrpc_abort_buffer pkt;
300 rxrpc_serial_t serial;
303 /* Don't bother sending aborts for a client call once the server has
304 * hard-ACK'd all of its request data. After that point, we're not
305 * going to stop the operation proceeding, and whilst we might limit
306 * the reply, it's not worth it if we can send a new call on the same
307 * channel instead, thereby closing off this call.
309 if (rxrpc_is_client_call(call) &&
310 test_bit(RXRPC_CALL_TX_LAST, &call->flags))
313 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
318 msg.msg_name = &call->peer->srx.transport;
319 msg.msg_namelen = call->peer->srx.transport_len;
320 msg.msg_control = NULL;
321 msg.msg_controllen = 0;
324 pkt.whdr.epoch = htonl(conn->proto.epoch);
325 pkt.whdr.cid = htonl(call->cid);
326 pkt.whdr.callNumber = htonl(call->call_id);
328 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
329 pkt.whdr.flags = conn->out_clientflag;
330 pkt.whdr.userStatus = 0;
331 pkt.whdr.securityIndex = call->security_ix;
333 pkt.whdr.serviceId = htons(call->service_id);
334 pkt.abort_code = htonl(call->abort_code);
336 iov[0].iov_base = &pkt;
337 iov[0].iov_len = sizeof(pkt);
339 serial = atomic_inc_return(&conn->serial);
340 pkt.whdr.serial = htonl(serial);
342 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
343 ret = do_udp_sendmsg(conn->params.local->socket, &msg, sizeof(pkt));
344 conn->params.peer->last_tx_at = ktime_get_seconds();
346 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
347 rxrpc_tx_point_call_abort);
349 trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
350 rxrpc_tx_point_call_abort);
351 rxrpc_tx_backoff(call, ret);
356 * send a packet through the transport endpoint
358 int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
361 enum rxrpc_req_ack_trace why;
362 struct rxrpc_connection *conn = call->conn;
363 struct rxrpc_wire_header whdr;
364 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
367 rxrpc_serial_t serial;
369 int ret, rtt_slot = -1;
371 _enter(",{%d}", skb->len);
373 if (hlist_unhashed(&call->error_link)) {
374 spin_lock_bh(&call->peer->lock);
375 hlist_add_head_rcu(&call->error_link, &call->peer->error_targets);
376 spin_unlock_bh(&call->peer->lock);
379 /* Each transmission of a Tx packet needs a new serial number */
380 serial = atomic_inc_return(&conn->serial);
382 whdr.epoch = htonl(conn->proto.epoch);
383 whdr.cid = htonl(call->cid);
384 whdr.callNumber = htonl(call->call_id);
385 whdr.seq = htonl(sp->hdr.seq);
386 whdr.serial = htonl(serial);
387 whdr.type = RXRPC_PACKET_TYPE_DATA;
388 whdr.flags = sp->hdr.flags;
390 whdr.securityIndex = call->security_ix;
391 whdr._rsvd = htons(sp->hdr._rsvd);
392 whdr.serviceId = htons(call->service_id);
394 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
396 whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
398 iov[0].iov_base = &whdr;
399 iov[0].iov_len = sizeof(whdr);
400 iov[1].iov_base = skb->head;
401 iov[1].iov_len = skb->len;
402 len = iov[0].iov_len + iov[1].iov_len;
403 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
405 msg.msg_name = &call->peer->srx.transport;
406 msg.msg_namelen = call->peer->srx.transport_len;
407 msg.msg_control = NULL;
408 msg.msg_controllen = 0;
411 /* If our RTT cache needs working on, request an ACK. Also request
412 * ACKs if a DATA packet appears to have been lost.
414 * However, we mustn't request an ACK on the last reply packet of a
415 * service call, lest OpenAFS incorrectly send us an ACK with some
416 * soft-ACKs in it and then never follow up with a proper hard ACK.
418 if (whdr.flags & RXRPC_REQUEST_ACK)
419 why = rxrpc_reqack_already_on;
420 else if ((whdr.flags & RXRPC_LAST_PACKET) && rxrpc_to_client(sp))
421 why = rxrpc_reqack_no_srv_last;
422 else if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
423 why = rxrpc_reqack_ack_lost;
425 why = rxrpc_reqack_retrans;
426 else if (call->cong_mode == RXRPC_CALL_SLOW_START && call->cong_cwnd <= 2)
427 why = rxrpc_reqack_slow_start;
428 else if (call->tx_winsize <= 2)
429 why = rxrpc_reqack_small_txwin;
430 else if (call->peer->rtt_count < 3 && sp->hdr.seq & 1)
431 why = rxrpc_reqack_more_rtt;
432 else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), ktime_get_real()))
433 why = rxrpc_reqack_old_rtt;
435 goto dont_set_request_ack;
437 rxrpc_inc_stat(call->rxnet, stat_why_req_ack[why]);
438 trace_rxrpc_req_ack(call->debug_id, sp->hdr.seq, why);
439 if (why != rxrpc_reqack_no_srv_last)
440 whdr.flags |= RXRPC_REQUEST_ACK;
441 dont_set_request_ack:
443 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
445 if ((lose++ & 7) == 7) {
447 trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
448 whdr.flags, retrans, true);
453 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, retrans,
456 /* send the packet with the don't fragment bit set if we currently
457 * think it's small enough */
458 if (iov[1].iov_len >= call->peer->maxdata)
459 goto send_fragmentable;
461 down_read(&conn->params.local->defrag_sem);
463 sp->hdr.serial = serial;
464 smp_wmb(); /* Set serial before timestamp */
465 skb->tstamp = ktime_get_real();
466 if (whdr.flags & RXRPC_REQUEST_ACK)
467 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
469 /* send the packet by UDP
470 * - returns -EMSGSIZE if UDP would have to fragment the packet
471 * to go out of the interface
472 * - in which case, we'll have processed the ICMP error
473 * message and update the peer record
475 rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
476 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
477 conn->params.peer->last_tx_at = ktime_get_seconds();
479 up_read(&conn->params.local->defrag_sem);
481 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
482 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
483 rxrpc_tx_point_call_data_nofrag);
485 trace_rxrpc_tx_packet(call->debug_id, &whdr,
486 rxrpc_tx_point_call_data_nofrag);
489 rxrpc_tx_backoff(call, ret);
490 if (ret == -EMSGSIZE)
491 goto send_fragmentable;
495 if (whdr.flags & RXRPC_REQUEST_ACK) {
496 call->peer->rtt_last_req = skb->tstamp;
497 if (call->peer->rtt_count > 1) {
498 unsigned long nowj = jiffies, ack_lost_at;
500 ack_lost_at = rxrpc_get_rto_backoff(call->peer, false);
502 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
503 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
504 rxrpc_timer_set_for_lost_ack);
508 if (sp->hdr.seq == 1 &&
509 !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
511 unsigned long nowj = jiffies, expect_rx_by;
513 expect_rx_by = nowj + call->next_rx_timo;
514 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
515 rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
516 rxrpc_timer_set_for_normal);
519 rxrpc_set_keepalive(call);
521 /* Cancel the call if the initial transmission fails,
522 * particularly if that's due to network routing issues that
523 * aren't going away anytime soon. The layer above can arrange
524 * the retransmission.
526 if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
527 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
531 _leave(" = %d [%u]", ret, call->peer->maxdata);
535 /* attempt to send this message with fragmentation enabled */
536 _debug("send fragment");
538 down_write(&conn->params.local->defrag_sem);
540 sp->hdr.serial = serial;
541 smp_wmb(); /* Set serial before timestamp */
542 skb->tstamp = ktime_get_real();
543 if (whdr.flags & RXRPC_REQUEST_ACK)
544 rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
546 switch (conn->params.local->srx.transport.family) {
549 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
551 rxrpc_inc_stat(call->rxnet, stat_tx_data_send_frag);
552 ret = do_udp_sendmsg(conn->params.local->socket, &msg, len);
553 conn->params.peer->last_tx_at = ktime_get_seconds();
555 ip_sock_set_mtu_discover(conn->params.local->socket->sk,
564 rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
565 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
566 rxrpc_tx_point_call_data_frag);
568 trace_rxrpc_tx_packet(call->debug_id, &whdr,
569 rxrpc_tx_point_call_data_frag);
571 rxrpc_tx_backoff(call, ret);
573 up_write(&conn->params.local->defrag_sem);
578 * reject packets through the local endpoint
580 void rxrpc_reject_packets(struct rxrpc_local *local)
582 struct sockaddr_rxrpc srx;
583 struct rxrpc_skb_priv *sp;
584 struct rxrpc_wire_header whdr;
592 _enter("%d", local->debug_id);
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);
599 msg.msg_name = &srx.transport;
600 msg.msg_control = NULL;
601 msg.msg_controllen = 0;
604 memset(&whdr, 0, sizeof(whdr));
606 while ((skb = skb_dequeue(&local->reject_queue))) {
607 rxrpc_see_skb(skb, rxrpc_skb_seen);
611 case RXRPC_SKB_MARK_REJECT_BUSY:
612 whdr.type = RXRPC_PACKET_TYPE_BUSY;
616 case RXRPC_SKB_MARK_REJECT_ABORT:
617 whdr.type = RXRPC_PACKET_TYPE_ABORT;
618 code = htonl(skb->priority);
619 size = sizeof(whdr) + sizeof(code);
623 rxrpc_free_skb(skb, rxrpc_skb_freed);
627 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
628 msg.msg_namelen = srx.transport_len;
630 whdr.epoch = htonl(sp->hdr.epoch);
631 whdr.cid = htonl(sp->hdr.cid);
632 whdr.callNumber = htonl(sp->hdr.callNumber);
633 whdr.serviceId = htons(sp->hdr.serviceId);
634 whdr.flags = sp->hdr.flags;
635 whdr.flags ^= RXRPC_CLIENT_INITIATED;
636 whdr.flags &= RXRPC_CLIENT_INITIATED;
638 iov_iter_kvec(&msg.msg_iter, WRITE, iov, ioc, size);
639 ret = do_udp_sendmsg(local->socket, &msg, size);
641 trace_rxrpc_tx_fail(local->debug_id, 0, ret,
642 rxrpc_tx_point_reject);
644 trace_rxrpc_tx_packet(local->debug_id, &whdr,
645 rxrpc_tx_point_reject);
648 rxrpc_free_skb(skb, rxrpc_skb_freed);
655 * Send a VERSION reply to a peer as a keepalive.
657 void rxrpc_send_keepalive(struct rxrpc_peer *peer)
659 struct rxrpc_wire_header whdr;
667 msg.msg_name = &peer->srx.transport;
668 msg.msg_namelen = peer->srx.transport_len;
669 msg.msg_control = NULL;
670 msg.msg_controllen = 0;
673 whdr.epoch = htonl(peer->local->rxnet->epoch);
678 whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
679 whdr.flags = RXRPC_LAST_PACKET;
681 whdr.securityIndex = 0;
685 iov[0].iov_base = &whdr;
686 iov[0].iov_len = sizeof(whdr);
687 iov[1].iov_base = (char *)rxrpc_keepalive_string;
688 iov[1].iov_len = sizeof(rxrpc_keepalive_string);
690 len = iov[0].iov_len + iov[1].iov_len;
692 _proto("Tx VERSION (keepalive)");
694 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
695 ret = do_udp_sendmsg(peer->local->socket, &msg, len);
697 trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
698 rxrpc_tx_point_version_keepalive);
700 trace_rxrpc_tx_packet(peer->debug_id, &whdr,
701 rxrpc_tx_point_version_keepalive);
703 peer->last_tx_at = ktime_get_seconds();