Commit c1318b39 authored by Boris Pismenny's avatar Boris Pismenny Committed by Paolo Abeni

tls: Add opt-in zerocopy mode of sendfile()

TLS device offload copies sendfile data to a bounce buffer before
transmitting. It allows to maintain the valid MAC on TLS records when
the file contents change and a part of TLS record has to be
retransmitted on TCP level.

In many common use cases (like serving static files over HTTPS) the file
contents are not changed on the fly. In many use cases breaking the
connection is totally acceptable if the file is changed during
transmission, because it would be received corrupted in any case.

This commit allows to optimize performance for such use cases to
providing a new optional mode of TLS sendfile(), in which the extra copy
is skipped. Removing this copy improves performance significantly, as
TLS and TCP sendfile perform the same operations, and the only overhead
is TLS header/trailer insertion.

The new mode can only be enabled with the new socket option named
TLS_TX_ZEROCOPY_SENDFILE on per-socket basis. It preserves backwards
compatibility with existing applications that rely on the copying
behavior.

The new mode is safe, meaning that unsolicited modifications of the file
being sent can't break integrity of the kernel. The worst thing that can
happen is sending a corrupted TLS record, which is in any case not
forbidden when using regular TCP sockets.

Sockets other than TLS device offload are not affected by the new socket
option. The actual status of zerocopy sendfile can be queried with
sock_diag.

Performance numbers in a single-core test with 24 HTTPS streams on
nginx, under 100% CPU load:

* non-zerocopy: 33.6 Gbit/s
* zerocopy: 79.92 Gbit/s

CPU: Intel(R) Xeon(R) Platinum 8380 CPU @ 2.30GHz
Signed-off-by: default avatarBoris Pismenny <borisp@nvidia.com>
Signed-off-by: default avatarTariq Toukan <tariqt@nvidia.com>
Signed-off-by: default avatarMaxim Mikityanskiy <maximmi@nvidia.com>
Reviewed-by: default avatarJakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/r/20220518092731.1243494-1-maximmi@nvidia.comSigned-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parent e43d940f
...@@ -238,6 +238,7 @@ struct tls_context { ...@@ -238,6 +238,7 @@ struct tls_context {
u8 tx_conf:3; u8 tx_conf:3;
u8 rx_conf:3; u8 rx_conf:3;
u8 zerocopy_sendfile:1;
int (*push_pending_record)(struct sock *sk, int flags); int (*push_pending_record)(struct sock *sk, int flags);
void (*sk_write_space)(struct sock *sk); void (*sk_write_space)(struct sock *sk);
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
/* TLS socket options */ /* TLS socket options */
#define TLS_TX 1 /* Set transmit parameters */ #define TLS_TX 1 /* Set transmit parameters */
#define TLS_RX 2 /* Set receive parameters */ #define TLS_RX 2 /* Set receive parameters */
#define TLS_TX_ZEROCOPY_SENDFILE 3 /* transmit zerocopy sendfile */
/* Supported versions */ /* Supported versions */
#define TLS_VERSION_MINOR(ver) ((ver) & 0xFF) #define TLS_VERSION_MINOR(ver) ((ver) & 0xFF)
...@@ -160,6 +161,7 @@ enum { ...@@ -160,6 +161,7 @@ enum {
TLS_INFO_CIPHER, TLS_INFO_CIPHER,
TLS_INFO_TXCONF, TLS_INFO_TXCONF,
TLS_INFO_RXCONF, TLS_INFO_RXCONF,
TLS_INFO_ZC_SENDFILE,
__TLS_INFO_MAX, __TLS_INFO_MAX,
}; };
#define TLS_INFO_MAX (__TLS_INFO_MAX - 1) #define TLS_INFO_MAX (__TLS_INFO_MAX - 1)
......
...@@ -411,10 +411,16 @@ static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i) ...@@ -411,10 +411,16 @@ static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i)
return 0; return 0;
} }
union tls_iter_offset {
struct iov_iter *msg_iter;
int offset;
};
static int tls_push_data(struct sock *sk, static int tls_push_data(struct sock *sk,
struct iov_iter *msg_iter, union tls_iter_offset iter_offset,
size_t size, int flags, size_t size, int flags,
unsigned char record_type) unsigned char record_type,
struct page *zc_page)
{ {
struct tls_context *tls_ctx = tls_get_ctx(sk); struct tls_context *tls_ctx = tls_get_ctx(sk);
struct tls_prot_info *prot = &tls_ctx->prot_info; struct tls_prot_info *prot = &tls_ctx->prot_info;
...@@ -480,12 +486,21 @@ static int tls_push_data(struct sock *sk, ...@@ -480,12 +486,21 @@ static int tls_push_data(struct sock *sk,
} }
record = ctx->open_record; record = ctx->open_record;
copy = min_t(size_t, size, (pfrag->size - pfrag->offset));
copy = min_t(size_t, copy, (max_open_record_len - record->len));
if (copy) { copy = min_t(size_t, size, max_open_record_len - record->len);
if (copy && zc_page) {
struct page_frag zc_pfrag;
zc_pfrag.page = zc_page;
zc_pfrag.offset = iter_offset.offset;
zc_pfrag.size = copy;
tls_append_frag(record, &zc_pfrag, copy);
} else if (copy) {
copy = min_t(size_t, copy, pfrag->size - pfrag->offset);
rc = tls_device_copy_data(page_address(pfrag->page) + rc = tls_device_copy_data(page_address(pfrag->page) +
pfrag->offset, copy, msg_iter); pfrag->offset, copy,
iter_offset.msg_iter);
if (rc) if (rc)
goto handle_error; goto handle_error;
tls_append_frag(record, pfrag, copy); tls_append_frag(record, pfrag, copy);
...@@ -540,6 +555,7 @@ int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) ...@@ -540,6 +555,7 @@ int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
{ {
unsigned char record_type = TLS_RECORD_TYPE_DATA; unsigned char record_type = TLS_RECORD_TYPE_DATA;
struct tls_context *tls_ctx = tls_get_ctx(sk); struct tls_context *tls_ctx = tls_get_ctx(sk);
union tls_iter_offset iter;
int rc; int rc;
mutex_lock(&tls_ctx->tx_lock); mutex_lock(&tls_ctx->tx_lock);
...@@ -551,8 +567,8 @@ int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) ...@@ -551,8 +567,8 @@ int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
goto out; goto out;
} }
rc = tls_push_data(sk, &msg->msg_iter, size, iter.msg_iter = &msg->msg_iter;
msg->msg_flags, record_type); rc = tls_push_data(sk, iter, size, msg->msg_flags, record_type, NULL);
out: out:
release_sock(sk); release_sock(sk);
...@@ -564,7 +580,8 @@ int tls_device_sendpage(struct sock *sk, struct page *page, ...@@ -564,7 +580,8 @@ int tls_device_sendpage(struct sock *sk, struct page *page,
int offset, size_t size, int flags) int offset, size_t size, int flags)
{ {
struct tls_context *tls_ctx = tls_get_ctx(sk); struct tls_context *tls_ctx = tls_get_ctx(sk);
struct iov_iter msg_iter; union tls_iter_offset iter_offset;
struct iov_iter msg_iter;
char *kaddr; char *kaddr;
struct kvec iov; struct kvec iov;
int rc; int rc;
...@@ -580,12 +597,20 @@ int tls_device_sendpage(struct sock *sk, struct page *page, ...@@ -580,12 +597,20 @@ int tls_device_sendpage(struct sock *sk, struct page *page,
goto out; goto out;
} }
if (tls_ctx->zerocopy_sendfile) {
iter_offset.offset = offset;
rc = tls_push_data(sk, iter_offset, size,
flags, TLS_RECORD_TYPE_DATA, page);
goto out;
}
kaddr = kmap(page); kaddr = kmap(page);
iov.iov_base = kaddr + offset; iov.iov_base = kaddr + offset;
iov.iov_len = size; iov.iov_len = size;
iov_iter_kvec(&msg_iter, WRITE, &iov, 1, size); iov_iter_kvec(&msg_iter, WRITE, &iov, 1, size);
rc = tls_push_data(sk, &msg_iter, size, iter_offset.msg_iter = &msg_iter;
flags, TLS_RECORD_TYPE_DATA); rc = tls_push_data(sk, iter_offset, size, flags, TLS_RECORD_TYPE_DATA,
NULL);
kunmap(page); kunmap(page);
out: out:
...@@ -656,10 +681,12 @@ EXPORT_SYMBOL(tls_get_record); ...@@ -656,10 +681,12 @@ EXPORT_SYMBOL(tls_get_record);
static int tls_device_push_pending_record(struct sock *sk, int flags) static int tls_device_push_pending_record(struct sock *sk, int flags)
{ {
struct iov_iter msg_iter; union tls_iter_offset iter;
struct iov_iter msg_iter;
iov_iter_kvec(&msg_iter, WRITE, NULL, 0, 0); iov_iter_kvec(&msg_iter, WRITE, NULL, 0, 0);
return tls_push_data(sk, &msg_iter, 0, flags, TLS_RECORD_TYPE_DATA); iter.msg_iter = &msg_iter;
return tls_push_data(sk, iter, 0, flags, TLS_RECORD_TYPE_DATA, NULL);
} }
void tls_device_write_space(struct sock *sk, struct tls_context *ctx) void tls_device_write_space(struct sock *sk, struct tls_context *ctx)
......
...@@ -513,6 +513,26 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval, ...@@ -513,6 +513,26 @@ static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
return rc; return rc;
} }
static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
int __user *optlen)
{
struct tls_context *ctx = tls_get_ctx(sk);
unsigned int value;
int len;
if (get_user(len, optlen))
return -EFAULT;
if (len != sizeof(value))
return -EINVAL;
value = ctx->zerocopy_sendfile;
if (copy_to_user(optval, &value, sizeof(value)))
return -EFAULT;
return 0;
}
static int do_tls_getsockopt(struct sock *sk, int optname, static int do_tls_getsockopt(struct sock *sk, int optname,
char __user *optval, int __user *optlen) char __user *optval, int __user *optlen)
{ {
...@@ -524,6 +544,9 @@ static int do_tls_getsockopt(struct sock *sk, int optname, ...@@ -524,6 +544,9 @@ static int do_tls_getsockopt(struct sock *sk, int optname,
rc = do_tls_getsockopt_conf(sk, optval, optlen, rc = do_tls_getsockopt_conf(sk, optval, optlen,
optname == TLS_TX); optname == TLS_TX);
break; break;
case TLS_TX_ZEROCOPY_SENDFILE:
rc = do_tls_getsockopt_tx_zc(sk, optval, optlen);
break;
default: default:
rc = -ENOPROTOOPT; rc = -ENOPROTOOPT;
break; break;
...@@ -675,6 +698,26 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval, ...@@ -675,6 +698,26 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
return rc; return rc;
} }
static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,
unsigned int optlen)
{
struct tls_context *ctx = tls_get_ctx(sk);
unsigned int value;
if (sockptr_is_null(optval) || optlen != sizeof(value))
return -EINVAL;
if (copy_from_sockptr(&value, optval, sizeof(value)))
return -EFAULT;
if (value > 1)
return -EINVAL;
ctx->zerocopy_sendfile = value;
return 0;
}
static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval, static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
unsigned int optlen) unsigned int optlen)
{ {
...@@ -688,6 +731,11 @@ static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval, ...@@ -688,6 +731,11 @@ static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
optname == TLS_TX); optname == TLS_TX);
release_sock(sk); release_sock(sk);
break; break;
case TLS_TX_ZEROCOPY_SENDFILE:
lock_sock(sk);
rc = do_tls_setsockopt_tx_zc(sk, optval, optlen);
release_sock(sk);
break;
default: default:
rc = -ENOPROTOOPT; rc = -ENOPROTOOPT;
break; break;
...@@ -921,6 +969,12 @@ static int tls_get_info(const struct sock *sk, struct sk_buff *skb) ...@@ -921,6 +969,12 @@ static int tls_get_info(const struct sock *sk, struct sk_buff *skb)
if (err) if (err)
goto nla_failure; goto nla_failure;
if (ctx->tx_conf == TLS_HW && ctx->zerocopy_sendfile) {
err = nla_put_flag(skb, TLS_INFO_ZC_SENDFILE);
if (err)
goto nla_failure;
}
rcu_read_unlock(); rcu_read_unlock();
nla_nest_end(skb, start); nla_nest_end(skb, start);
return 0; return 0;
...@@ -940,6 +994,7 @@ static size_t tls_get_info_size(const struct sock *sk) ...@@ -940,6 +994,7 @@ static size_t tls_get_info_size(const struct sock *sk)
nla_total_size(sizeof(u16)) + /* TLS_INFO_CIPHER */ nla_total_size(sizeof(u16)) + /* TLS_INFO_CIPHER */
nla_total_size(sizeof(u16)) + /* TLS_INFO_RXCONF */ nla_total_size(sizeof(u16)) + /* TLS_INFO_RXCONF */
nla_total_size(sizeof(u16)) + /* TLS_INFO_TXCONF */ nla_total_size(sizeof(u16)) + /* TLS_INFO_TXCONF */
nla_total_size(0) + /* TLS_INFO_ZC_SENDFILE */
0; 0;
return size; return size;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment