Skip to content

Commit ed9b764

Browse files
Boris PismennySaeed Mahameed
authored andcommitted
net/tls: Add asynchronous resync
This patch adds support for asynchronous resynchronization in tls_device. Async resync follows two distinct stages: 1. The NIC driver indicates that it would like to resync on some TLS record within the received packet (P), but the driver does not know (yet) which of the TLS records within the packet. At this stage, the NIC driver will query the device to find the exact TCP sequence for resync (tcpsn), however, the driver does not wait for the device to provide the response. 2. Eventually, the device responds, and the driver provides the tcpsn within the resync packet to KTLS. Now, KTLS can check the tcpsn against any processed TLS records within packet P, and also against any record that is processed in the future within packet P. The asynchronous resync path simplifies the device driver, as it can save bits on the packet completion (32-bit TCP sequence), and pass this information on an asynchronous command instead. Signed-off-by: Boris Pismenny <[email protected]> Signed-off-by: Tariq Toukan <[email protected]> Signed-off-by: Saeed Mahameed <[email protected]>
1 parent acb5a07 commit ed9b764

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

include/net/tls.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,19 @@ struct tlsdev_ops {
291291
enum tls_offload_sync_type {
292292
TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ = 0,
293293
TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT = 1,
294+
TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC = 2,
294295
};
295296

296297
#define TLS_DEVICE_RESYNC_NH_START_IVAL 2
297298
#define TLS_DEVICE_RESYNC_NH_MAX_IVAL 128
298299

300+
#define TLS_DEVICE_RESYNC_ASYNC_LOGMAX 13
301+
struct tls_offload_resync_async {
302+
atomic64_t req;
303+
u32 loglen;
304+
u32 log[TLS_DEVICE_RESYNC_ASYNC_LOGMAX];
305+
};
306+
299307
struct tls_offload_context_rx {
300308
/* sw must be the first member of tls_offload_context_rx */
301309
struct tls_sw_context_rx sw;
@@ -314,6 +322,10 @@ struct tls_offload_context_rx {
314322
u32 decrypted_failed;
315323
u32 decrypted_tgt;
316324
} resync_nh;
325+
/* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC */
326+
struct {
327+
struct tls_offload_resync_async *resync_async;
328+
};
317329
};
318330
u8 driver_state[] __aligned(8);
319331
/* The TLS layer reserves room for driver specific state
@@ -606,13 +618,37 @@ tls_driver_ctx(const struct sock *sk, enum tls_offload_ctx_dir direction)
606618
}
607619
#endif
608620

621+
#define RESYNC_REQ BIT(0)
622+
#define RESYNC_REQ_ASYNC BIT(1)
609623
/* The TLS context is valid until sk_destruct is called */
610624
static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
611625
{
612626
struct tls_context *tls_ctx = tls_get_ctx(sk);
613627
struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
614628

615-
atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | 1);
629+
atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | RESYNC_REQ);
630+
}
631+
632+
/* Log all TLS record header TCP sequences in [seq, seq+len] */
633+
static inline void
634+
tls_offload_rx_resync_async_request_start(struct sock *sk, __be32 seq, u16 len)
635+
{
636+
struct tls_context *tls_ctx = tls_get_ctx(sk);
637+
struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
638+
639+
atomic64_set(&rx_ctx->resync_async->req, ((u64)ntohl(seq) << 32) |
640+
(len << 16) | RESYNC_REQ | RESYNC_REQ_ASYNC);
641+
rx_ctx->resync_async->loglen = 0;
642+
}
643+
644+
static inline void
645+
tls_offload_rx_resync_async_request_end(struct sock *sk, __be32 seq)
646+
{
647+
struct tls_context *tls_ctx = tls_get_ctx(sk);
648+
struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
649+
650+
atomic64_set(&rx_ctx->resync_async->req,
651+
((u64)ntohl(seq) << 32) | RESYNC_REQ);
616652
}
617653

618654
static inline void

net/tls/tls_device.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,47 @@ static void tls_device_resync_rx(struct tls_context *tls_ctx,
690690
TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC);
691691
}
692692

693+
static bool
694+
tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async,
695+
s64 resync_req, u32 *seq)
696+
{
697+
u32 is_async = resync_req & RESYNC_REQ_ASYNC;
698+
u32 req_seq = resync_req >> 32;
699+
u32 req_end = req_seq + ((resync_req >> 16) & 0xffff);
700+
701+
if (is_async) {
702+
/* asynchronous stage: log all headers seq such that
703+
* req_seq <= seq <= end_seq, and wait for real resync request
704+
*/
705+
if (between(*seq, req_seq, req_end) &&
706+
resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX)
707+
resync_async->log[resync_async->loglen++] = *seq;
708+
709+
return false;
710+
}
711+
712+
/* synchronous stage: check against the logged entries and
713+
* proceed to check the next entries if no match was found
714+
*/
715+
while (resync_async->loglen) {
716+
if (req_seq == resync_async->log[resync_async->loglen - 1] &&
717+
atomic64_try_cmpxchg(&resync_async->req,
718+
&resync_req, 0)) {
719+
resync_async->loglen = 0;
720+
*seq = req_seq;
721+
return true;
722+
}
723+
resync_async->loglen--;
724+
}
725+
726+
if (req_seq == *seq &&
727+
atomic64_try_cmpxchg(&resync_async->req,
728+
&resync_req, 0))
729+
return true;
730+
731+
return false;
732+
}
733+
693734
void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
694735
{
695736
struct tls_context *tls_ctx = tls_get_ctx(sk);
@@ -736,6 +777,16 @@ void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
736777
seq += rcd_len;
737778
tls_bigint_increment(rcd_sn, prot->rec_seq_size);
738779
break;
780+
case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC:
781+
resync_req = atomic64_read(&rx_ctx->resync_async->req);
782+
is_req_pending = resync_req;
783+
if (likely(!is_req_pending))
784+
return;
785+
786+
if (!tls_device_rx_resync_async(rx_ctx->resync_async,
787+
resync_req, &seq))
788+
return;
789+
break;
739790
}
740791

741792
tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn);

0 commit comments

Comments
 (0)