1 /* RxRPC packet transmission
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/net.h>
15 #include <linux/gfp.h>
16 #include <linux/skbuff.h>
17 #include <linux/export.h>
19 #include <net/af_rxrpc.h>
20 #include "ar-internal.h"
22 struct rxrpc_ack_buffer
{
23 struct rxrpc_wire_header whdr
;
24 struct rxrpc_ackpacket ack
;
27 struct rxrpc_ackinfo ackinfo
;
30 struct rxrpc_abort_buffer
{
31 struct rxrpc_wire_header whdr
;
36 * Arrange for a keepalive ping a certain time after we last transmitted. This
37 * lets the far side know we're still interested in this call and helps keep
38 * the route through any intervening firewall open.
40 * Receiving a response to the ping will prevent the ->expect_rx_by timer from
43 static void rxrpc_set_keepalive(struct rxrpc_call
*call
)
45 unsigned long now
= jiffies
, keepalive_at
= call
->next_rx_timo
/ 6;
48 WRITE_ONCE(call
->keepalive_at
, keepalive_at
);
49 rxrpc_reduce_call_timer(call
, keepalive_at
, now
,
50 rxrpc_timer_set_for_keepalive
);
54 * Fill out an ACK packet.
56 static size_t rxrpc_fill_out_ack(struct rxrpc_connection
*conn
,
57 struct rxrpc_call
*call
,
58 struct rxrpc_ack_buffer
*pkt
,
59 rxrpc_seq_t
*_hard_ack
,
63 rxrpc_serial_t serial
;
64 rxrpc_seq_t hard_ack
, top
, seq
;
69 /* Barrier against rxrpc_input_data(). */
70 serial
= call
->ackr_serial
;
71 hard_ack
= READ_ONCE(call
->rx_hard_ack
);
72 top
= smp_load_acquire(&call
->rx_top
);
73 *_hard_ack
= hard_ack
;
76 pkt
->ack
.bufferSpace
= htons(8);
77 pkt
->ack
.maxSkew
= htons(call
->ackr_skew
);
78 pkt
->ack
.firstPacket
= htonl(hard_ack
+ 1);
79 pkt
->ack
.previousPacket
= htonl(call
->ackr_prev_seq
);
80 pkt
->ack
.serial
= htonl(serial
);
81 pkt
->ack
.reason
= reason
;
82 pkt
->ack
.nAcks
= top
- hard_ack
;
84 if (reason
== RXRPC_ACK_PING
)
85 pkt
->whdr
.flags
|= RXRPC_REQUEST_ACK
;
87 if (after(top
, hard_ack
)) {
90 ix
= seq
& RXRPC_RXTX_BUFF_MASK
;
91 if (call
->rxtx_buffer
[ix
])
92 *ackp
++ = RXRPC_ACK_TYPE_ACK
;
94 *ackp
++ = RXRPC_ACK_TYPE_NACK
;
96 } while (before_eq(seq
, top
));
99 mtu
= conn
->params
.peer
->if_mtu
;
100 mtu
-= conn
->params
.peer
->hdrsize
;
101 jmax
= (call
->nr_jumbo_bad
> 3) ? 1 : rxrpc_rx_jumbo_max
;
102 pkt
->ackinfo
.rxMTU
= htonl(rxrpc_rx_mtu
);
103 pkt
->ackinfo
.maxMTU
= htonl(mtu
);
104 pkt
->ackinfo
.rwind
= htonl(call
->rx_winsize
);
105 pkt
->ackinfo
.jumbo_max
= htonl(jmax
);
110 return top
- hard_ack
+ 3;
114 * Send an ACK call packet.
116 int rxrpc_send_ack_packet(struct rxrpc_call
*call
, bool ping
,
117 rxrpc_serial_t
*_serial
)
119 struct rxrpc_connection
*conn
= NULL
;
120 struct rxrpc_ack_buffer
*pkt
;
123 rxrpc_serial_t serial
;
124 rxrpc_seq_t hard_ack
, top
;
129 spin_lock_bh(&call
->lock
);
131 conn
= rxrpc_get_connection_maybe(call
->conn
);
132 spin_unlock_bh(&call
->lock
);
136 pkt
= kzalloc(sizeof(*pkt
), GFP_KERNEL
);
138 rxrpc_put_connection(conn
);
142 msg
.msg_name
= &call
->peer
->srx
.transport
;
143 msg
.msg_namelen
= call
->peer
->srx
.transport_len
;
144 msg
.msg_control
= NULL
;
145 msg
.msg_controllen
= 0;
148 pkt
->whdr
.epoch
= htonl(conn
->proto
.epoch
);
149 pkt
->whdr
.cid
= htonl(call
->cid
);
150 pkt
->whdr
.callNumber
= htonl(call
->call_id
);
152 pkt
->whdr
.type
= RXRPC_PACKET_TYPE_ACK
;
153 pkt
->whdr
.flags
= RXRPC_SLOW_START_OK
| conn
->out_clientflag
;
154 pkt
->whdr
.userStatus
= 0;
155 pkt
->whdr
.securityIndex
= call
->security_ix
;
157 pkt
->whdr
.serviceId
= htons(call
->service_id
);
159 spin_lock_bh(&call
->lock
);
161 reason
= RXRPC_ACK_PING
;
163 reason
= call
->ackr_reason
;
164 if (!call
->ackr_reason
) {
165 spin_unlock_bh(&call
->lock
);
169 call
->ackr_reason
= 0;
171 n
= rxrpc_fill_out_ack(conn
, call
, pkt
, &hard_ack
, &top
, reason
);
173 spin_unlock_bh(&call
->lock
);
175 iov
[0].iov_base
= pkt
;
176 iov
[0].iov_len
= sizeof(pkt
->whdr
) + sizeof(pkt
->ack
) + n
;
177 iov
[1].iov_base
= &pkt
->ackinfo
;
178 iov
[1].iov_len
= sizeof(pkt
->ackinfo
);
179 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
181 serial
= atomic_inc_return(&conn
->serial
);
182 pkt
->whdr
.serial
= htonl(serial
);
183 trace_rxrpc_tx_ack(call
, serial
,
184 ntohl(pkt
->ack
.firstPacket
),
185 ntohl(pkt
->ack
.serial
),
186 pkt
->ack
.reason
, pkt
->ack
.nAcks
);
191 call
->ping_serial
= serial
;
193 /* We need to stick a time in before we send the packet in case
194 * the reply gets back before kernel_sendmsg() completes - but
195 * asking UDP to send the packet can take a relatively long
196 * time, so we update the time after, on the assumption that
197 * the packet transmission is more likely to happen towards the
198 * end of the kernel_sendmsg() call.
200 call
->ping_time
= ktime_get_real();
201 set_bit(RXRPC_CALL_PINGING
, &call
->flags
);
202 trace_rxrpc_rtt_tx(call
, rxrpc_rtt_tx_ping
, serial
);
205 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, 2, len
);
207 call
->ping_time
= ktime_get_real();
209 if (call
->state
< RXRPC_CALL_COMPLETE
) {
212 clear_bit(RXRPC_CALL_PINGING
, &call
->flags
);
213 rxrpc_propose_ACK(call
, pkt
->ack
.reason
,
214 ntohs(pkt
->ack
.maxSkew
),
215 ntohl(pkt
->ack
.serial
),
217 rxrpc_propose_ack_retry_tx
);
219 spin_lock_bh(&call
->lock
);
220 if (after(hard_ack
, call
->ackr_consumed
))
221 call
->ackr_consumed
= hard_ack
;
222 if (after(top
, call
->ackr_seen
))
223 call
->ackr_seen
= top
;
224 spin_unlock_bh(&call
->lock
);
227 rxrpc_set_keepalive(call
);
231 rxrpc_put_connection(conn
);
237 * Send an ABORT call packet.
239 int rxrpc_send_abort_packet(struct rxrpc_call
*call
)
241 struct rxrpc_connection
*conn
= NULL
;
242 struct rxrpc_abort_buffer pkt
;
245 rxrpc_serial_t serial
;
248 /* Don't bother sending aborts for a client call once the server has
249 * hard-ACK'd all of its request data. After that point, we're not
250 * going to stop the operation proceeding, and whilst we might limit
251 * the reply, it's not worth it if we can send a new call on the same
252 * channel instead, thereby closing off this call.
254 if (rxrpc_is_client_call(call
) &&
255 test_bit(RXRPC_CALL_TX_LAST
, &call
->flags
))
258 spin_lock_bh(&call
->lock
);
260 conn
= rxrpc_get_connection_maybe(call
->conn
);
261 spin_unlock_bh(&call
->lock
);
265 msg
.msg_name
= &call
->peer
->srx
.transport
;
266 msg
.msg_namelen
= call
->peer
->srx
.transport_len
;
267 msg
.msg_control
= NULL
;
268 msg
.msg_controllen
= 0;
271 pkt
.whdr
.epoch
= htonl(conn
->proto
.epoch
);
272 pkt
.whdr
.cid
= htonl(call
->cid
);
273 pkt
.whdr
.callNumber
= htonl(call
->call_id
);
275 pkt
.whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
276 pkt
.whdr
.flags
= conn
->out_clientflag
;
277 pkt
.whdr
.userStatus
= 0;
278 pkt
.whdr
.securityIndex
= call
->security_ix
;
280 pkt
.whdr
.serviceId
= htons(call
->service_id
);
281 pkt
.abort_code
= htonl(call
->abort_code
);
283 iov
[0].iov_base
= &pkt
;
284 iov
[0].iov_len
= sizeof(pkt
);
286 serial
= atomic_inc_return(&conn
->serial
);
287 pkt
.whdr
.serial
= htonl(serial
);
289 ret
= kernel_sendmsg(conn
->params
.local
->socket
,
290 &msg
, iov
, 1, sizeof(pkt
));
292 rxrpc_put_connection(conn
);
297 * send a packet through the transport endpoint
299 int rxrpc_send_data_packet(struct rxrpc_call
*call
, struct sk_buff
*skb
,
302 struct rxrpc_connection
*conn
= call
->conn
;
303 struct rxrpc_wire_header whdr
;
304 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
307 rxrpc_serial_t serial
;
312 _enter(",{%d}", skb
->len
);
314 /* Each transmission of a Tx packet needs a new serial number */
315 serial
= atomic_inc_return(&conn
->serial
);
317 whdr
.epoch
= htonl(conn
->proto
.epoch
);
318 whdr
.cid
= htonl(call
->cid
);
319 whdr
.callNumber
= htonl(call
->call_id
);
320 whdr
.seq
= htonl(sp
->hdr
.seq
);
321 whdr
.serial
= htonl(serial
);
322 whdr
.type
= RXRPC_PACKET_TYPE_DATA
;
323 whdr
.flags
= sp
->hdr
.flags
;
325 whdr
.securityIndex
= call
->security_ix
;
326 whdr
._rsvd
= htons(sp
->hdr
._rsvd
);
327 whdr
.serviceId
= htons(call
->service_id
);
329 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE
, &conn
->flags
) &&
331 whdr
.userStatus
= RXRPC_USERSTATUS_SERVICE_UPGRADE
;
333 iov
[0].iov_base
= &whdr
;
334 iov
[0].iov_len
= sizeof(whdr
);
335 iov
[1].iov_base
= skb
->head
;
336 iov
[1].iov_len
= skb
->len
;
337 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
339 msg
.msg_name
= &call
->peer
->srx
.transport
;
340 msg
.msg_namelen
= call
->peer
->srx
.transport_len
;
341 msg
.msg_control
= NULL
;
342 msg
.msg_controllen
= 0;
345 /* If our RTT cache needs working on, request an ACK. Also request
346 * ACKs if a DATA packet appears to have been lost.
348 if (!(sp
->hdr
.flags
& RXRPC_LAST_PACKET
) &&
349 (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST
, &call
->events
) ||
351 call
->cong_mode
== RXRPC_CALL_SLOW_START
||
352 (call
->peer
->rtt_usage
< 3 && sp
->hdr
.seq
& 1) ||
353 ktime_before(ktime_add_ms(call
->peer
->rtt_last_req
, 1000),
355 whdr
.flags
|= RXRPC_REQUEST_ACK
;
357 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS
)) {
359 if ((lose
++ & 7) == 7) {
366 _proto("Tx DATA %%%u { #%u }", serial
, sp
->hdr
.seq
);
368 /* send the packet with the don't fragment bit set if we currently
369 * think it's small enough */
370 if (iov
[1].iov_len
>= call
->peer
->maxdata
)
371 goto send_fragmentable
;
373 down_read(&conn
->params
.local
->defrag_sem
);
374 /* send the packet by UDP
375 * - returns -EMSGSIZE if UDP would have to fragment the packet
376 * to go out of the interface
377 * - in which case, we'll have processed the ICMP error
378 * message and update the peer record
380 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, 2, len
);
382 up_read(&conn
->params
.local
->defrag_sem
);
383 if (ret
== -EMSGSIZE
)
384 goto send_fragmentable
;
387 trace_rxrpc_tx_data(call
, sp
->hdr
.seq
, serial
, whdr
.flags
,
390 ktime_t now
= ktime_get_real();
393 sp
->hdr
.serial
= serial
;
394 if (whdr
.flags
& RXRPC_REQUEST_ACK
) {
395 call
->peer
->rtt_last_req
= now
;
396 trace_rxrpc_rtt_tx(call
, rxrpc_rtt_tx_data
, serial
);
397 if (call
->peer
->rtt_usage
> 1) {
398 unsigned long nowj
= jiffies
, ack_lost_at
;
400 ack_lost_at
= nsecs_to_jiffies(2 * call
->peer
->rtt
);
405 WRITE_ONCE(call
->ack_lost_at
, ack_lost_at
);
406 rxrpc_reduce_call_timer(call
, ack_lost_at
, nowj
,
407 rxrpc_timer_set_for_lost_ack
);
412 rxrpc_set_keepalive(call
);
414 _leave(" = %d [%u]", ret
, call
->peer
->maxdata
);
418 /* attempt to send this message with fragmentation enabled */
419 _debug("send fragment");
421 down_write(&conn
->params
.local
->defrag_sem
);
423 switch (conn
->params
.local
->srx
.transport
.family
) {
425 opt
= IP_PMTUDISC_DONT
;
426 ret
= kernel_setsockopt(conn
->params
.local
->socket
,
427 SOL_IP
, IP_MTU_DISCOVER
,
428 (char *)&opt
, sizeof(opt
));
430 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
,
433 opt
= IP_PMTUDISC_DO
;
434 kernel_setsockopt(conn
->params
.local
->socket
, SOL_IP
,
436 (char *)&opt
, sizeof(opt
));
440 #ifdef CONFIG_AF_RXRPC_IPV6
442 opt
= IPV6_PMTUDISC_DONT
;
443 ret
= kernel_setsockopt(conn
->params
.local
->socket
,
444 SOL_IPV6
, IPV6_MTU_DISCOVER
,
445 (char *)&opt
, sizeof(opt
));
447 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
,
450 opt
= IPV6_PMTUDISC_DO
;
451 kernel_setsockopt(conn
->params
.local
->socket
,
452 SOL_IPV6
, IPV6_MTU_DISCOVER
,
453 (char *)&opt
, sizeof(opt
));
459 up_write(&conn
->params
.local
->defrag_sem
);
464 * reject packets through the local endpoint
466 void rxrpc_reject_packets(struct rxrpc_local
*local
)
468 struct sockaddr_rxrpc srx
;
469 struct rxrpc_skb_priv
*sp
;
470 struct rxrpc_wire_header whdr
;
477 _enter("%d", local
->debug_id
);
479 iov
[0].iov_base
= &whdr
;
480 iov
[0].iov_len
= sizeof(whdr
);
481 iov
[1].iov_base
= &code
;
482 iov
[1].iov_len
= sizeof(code
);
483 size
= sizeof(whdr
) + sizeof(code
);
485 msg
.msg_name
= &srx
.transport
;
486 msg
.msg_control
= NULL
;
487 msg
.msg_controllen
= 0;
490 memset(&whdr
, 0, sizeof(whdr
));
491 whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
493 while ((skb
= skb_dequeue(&local
->reject_queue
))) {
494 rxrpc_see_skb(skb
, rxrpc_skb_rx_seen
);
497 if (rxrpc_extract_addr_from_skb(local
, &srx
, skb
) == 0) {
498 msg
.msg_namelen
= srx
.transport_len
;
500 code
= htonl(skb
->priority
);
502 whdr
.epoch
= htonl(sp
->hdr
.epoch
);
503 whdr
.cid
= htonl(sp
->hdr
.cid
);
504 whdr
.callNumber
= htonl(sp
->hdr
.callNumber
);
505 whdr
.serviceId
= htons(sp
->hdr
.serviceId
);
506 whdr
.flags
= sp
->hdr
.flags
;
507 whdr
.flags
^= RXRPC_CLIENT_INITIATED
;
508 whdr
.flags
&= RXRPC_CLIENT_INITIATED
;
510 kernel_sendmsg(local
->socket
, &msg
, iov
, 2, size
);
513 rxrpc_free_skb(skb
, rxrpc_skb_rx_freed
);