Skip to contentSkip to conversation

net/tls: Add support for async encryption of records for performance

PATCH Superseded Vakul Garg 2 participants 3 messages Sep 19, 2018 – Sep 21, 2018

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Vakul Garg

author
#0 lore raw

PATCH 1/1 net/tls: Add support for async encryption of records for performance

In current implementation, tls records are encrypted & transmitted serially. Till the time the previously submitted user data is encrypted, the implementation waits and on finish starts transmitting the record. This approach of encrypt-one record at a time is inefficient when asynchronous crypto accelerators are used. For each record, there are overheads of interrupts, driver softIRQ scheduling etc. Also the crypto accelerator sits idle most of time while an encrypted record's pages are handed over to tcp stack for transmission.

This patch enables encryption of multiple records in parallel when an async capable crypto accelerator is present in system. This is achieved by allowing the user space application to send more data using sendmsg() even while previously issued data is being processed by crypto accelerator. This requires returning the control back to user space application after submitting encryption request to accelerator. This also means that zero-copy mode of encryption cannot be used with async accelerator as we must be done with user space application buffer before returning from sendmsg().

There can be multiple records in flight to/from the accelerator. Each of the record is represented by 'struct tls_rec'. This is used to store the memory pages for the record.

After the records are encrypted, they are added in a linked list called tx_ready_list which contains encrypted tls records sorted as per tls sequence number. The records from tx_ready_list are transmitted using a newly introduced function called tls_tx_records(). The tx_ready_list is polled for any record ready to be transmitted in sendmsg(), sendpage() after initiating encryption of new tls records. This achieves parallel encryption and transmission of records when async accelerator is present.

There could be situation when crypto accelerator completes encryption later than polling of tx_ready_list by sendmsg()/sendpage(). Therefore we need a deferred work context to be able to transmit records from tx_ready_list. The deferred work context gets scheduled if applications are not sending much data through the socket. If the applications issue sendmsg()/sendpage() in quick succession, then the scheduling of tx_work_handler gets cancelled as the tx_ready_list would be polled from application's context itself. This saves scheduling overhead of deferred work.

The patch also brings some side benefit. We are able to get rid of the concept of CLOSED record. This is because the records once closed are either encrypted and then placed into tx_ready_list or if encryption fails, the socket error is set. This simplifies the kernel tls sendpath. However since tls_device.c is still using macros, accessory functions for CLOSED records have been retained.

---
 include/net/tls.h  |  70 +++++--
 net/tls/tls_main.c |  54 ++---
 net/tls/tls_sw.c   | 569 ++++++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 515 insertions(+), 178 deletions(-)
include/net/tls.h +58 -12
index 9f3c4ea9ad6f..84756667fc2a 100644
@@ -41,7 +41,7 @@
 #include <linux/tcp.h>
 #include <net/tcp.h>
 #include <net/strparser.h>
-
+#include <crypto/aead.h>
 #include <uapi/linux/tls.h>
 
 
@@ -93,24 +93,47 @@ enum {
 	TLS_NUM_CONFIG,
 };
 
-struct tls_sw_context_tx {
-	struct crypto_aead *aead_send;
-	struct crypto_wait async_wait;
-
-	char aad_space[TLS_AAD_SPACE_SIZE];
-
-	unsigned int sg_plaintext_size;
-	int sg_plaintext_num_elem;
+/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
+ * allocated or mapped for each TLS record. After encryption, the records are
+ * stores in a linked list.
+ */
+struct tls_rec {
+	struct list_head list;
+	int tx_flags;
 	struct scatterlist sg_plaintext_data[MAX_SKB_FRAGS];
-
-	unsigned int sg_encrypted_size;
-	int sg_encrypted_num_elem;
 	struct scatterlist sg_encrypted_data[MAX_SKB_FRAGS];
 
 	/* AAD | sg_plaintext_data | sg_tag */
 	struct scatterlist sg_aead_in[2];
 	/* AAD | sg_encrypted_data (data contain overhead for hdr&iv&tag) */
 	struct scatterlist sg_aead_out[2];
+
+	unsigned int sg_plaintext_size;
+	unsigned int sg_encrypted_size;
+	int sg_plaintext_num_elem;
+	int sg_encrypted_num_elem;
+
+	char aad_space[TLS_AAD_SPACE_SIZE];
+	struct aead_request aead_req;
+	u8 aead_req_ctx[];
+};
+
+struct tx_work {
+	struct delayed_work work;
+	struct sock *sk;
+};
+
+struct tls_sw_context_tx {
+	struct crypto_aead *aead_send;
+	struct crypto_wait async_wait;
+	struct tx_work tx_work;
+	struct tls_rec *open_rec;
+	struct list_head tx_ready_list;
+	atomic_t encrypt_pending;
+	int async_notify;
+
+#define BIT_TX_SCHEDULED	0
+	unsigned long tx_bitmask;
 };
 
 struct tls_sw_context_rx {
@@ -197,6 +220,8 @@ struct tls_context {
 
 	struct scatterlist *partially_sent_record;
 	u16 partially_sent_offset;
+	u64 tx_seq_number;	/* Next TLS seqnum to be transmitted */
+
 	unsigned long flags;
 	bool in_tcp_sendpages;
 
@@ -261,6 +286,7 @@ int tls_device_sendpage(struct sock *sk, struct page *page,
 void tls_device_sk_destruct(struct sock *sk);
 void tls_device_init(void);
 void tls_device_cleanup(void);
+int tls_tx_records(struct sock *sk, int flags);
 
 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
 				       u32 seq, u64 *p_record_sn);
@@ -279,6 +305,9 @@ void tls_sk_destruct(struct sock *sk, struct tls_context *ctx);
 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
 		struct scatterlist *sg, u16 first_offset,
 		int flags);
+int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
+			    int flags);
+
 int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
 				   int flags, long *timeo);
 
@@ -312,6 +341,23 @@ static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
 	return tls_ctx->pending_open_record_frags;
 }
 
+static inline  bool is_tx_ready(struct tls_context *tls_ctx,
+				struct tls_sw_context_tx *ctx)
+{
+	struct tls_rec *rec;
+	u64 seq;
+
+	rec = list_first_entry(&ctx->tx_ready_list, struct tls_rec, list);
+	if (!rec)
+		return false;
+
+	seq = be64_to_cpup((const __be64 *)&rec->aad_space);
+	if (seq == tls_ctx->tx_seq_number)
+		return true;
+	else
+		return false;
+}
+
 struct sk_buff *
 tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
 		      struct sk_buff *skb);
net/tls/tls_main.c +21 -33

91 lines not on this page · raw message

net/tls/tls_sw.c +436 -133

833 lines not on this page · raw message

Signature · 1 line
2.13.6
maintainer
#1 lore raw

From: Vakul Garg <[email protected]>
Date: Wed, 19 Sep 2018 20:51:35 +0530

Quoted 2 lines

This patch enables encryption of multiple records in parallel when an async capable crypto accelerator is present in system.

This seems to be trading off zero copy with async support.

Async crypto device support is not the common case at all, and synchronous crypto via cpu crypto acceleration instructions is so much more likely.

Oh I see, the new logic is only triggered with ASYNC_CAPABLE is set?

include/net/tls.h static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx) · line 346

Quoted 3 lines
include/net/tls.h +3 -0
+static inline  bool is_tx_ready(struct tls_context *tls_ctx,
+				struct tls_sw_context_tx *ctx)
+{

Two space between "inline" and "bool", please make it one.

net/tls/tls_main.c int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx, · line 224

Quoted 4 lines

static void tls_write_space(struct sock *sk)
{

net/tls/tls_main.c +1 -0
 	struct tls_context *ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);

Longest to shortest line (reverse christmas tree) ordering for local variable declarations please.

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 344

Quoted 4 lines
net/tls/tls_sw.c +3 -0
+	list_for_each_prev(pos, &ctx->tx_ready_list) {
+		struct tls_rec *rec = (struct tls_rec *)pos;
+		u64 seq = be64_to_cpup((const __be64 *)&rec->aad_space);

Likewise.

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 361

Quoted 8 lines
net/tls/tls_sw.c +7 -1
-static int tls_do_encryption(struct tls_context *tls_ctx,
+int tls_tx_records(struct sock *sk, int flags)
+{
+	struct tls_rec *rec, *tmp;
+	struct tls_context *tls_ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+	int rc = 0;
+	int tx_flags;

Likewise.

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 425

Quoted 9 lines
net/tls/tls_sw.c +9 -0
+static void tls_encrypt_done(struct crypto_async_request *req, int err)
+{
+	struct aead_request *aead_req = (struct aead_request *)req;
+	struct sock *sk = req->data;
+	struct tls_context *tls_ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+	struct tls_rec *rec;
+	int pending;
+	bool ready = false;

Likewise.

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 476

Quoted 8 lines
net/tls/tls_sw.c +3 -0
+static int tls_do_encryption(struct sock *sk,
+			     struct tls_context *tls_ctx,
 			     struct tls_sw_context_tx *ctx,
 			     struct aead_request *aead_req,
 			     size_t data_len)
 {
 	int rc;
+	struct tls_rec *rec = ctx->open_rec;

Likewise.

net/tls/tls_sw.c static int memcopy_from_iter(struct sock *sk, struct iov_iter *from, · line 635

Quoted 8 lines
net/tls/tls_sw.c +2 -1
@@ -473,11 +630,12 @@ static int memcopy_from_iter(struct sock *sk, struct iov_iter *from,
 {
 	struct tls_context *tls_ctx = tls_get_ctx(sk);
 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
-	struct scatterlist *sg = ctx->sg_plaintext_data;
+	struct tls_rec *rec = ctx->open_rec;
+	struct scatterlist *sg = rec->sg_plaintext_data;
 	int copy, i, rc = 0;

Likewise.

net/tls/tls_sw.c static int memcopy_from_iter(struct sock *sk, struct iov_iter *from, · line 663

Quoted 6 lines
net/tls/tls_sw.c +6 -0
+struct tls_rec *get_rec(struct sock *sk)
+{
+	int mem_size;
+	struct tls_context *tls_ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+	struct tls_rec *rec;

Likewise.

net/tls/tls_sw.c int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) · line 715

Quoted 10 lines
net/tls/tls_sw.c +5 -0
@@ -510,21 +707,33 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 	int record_room;
 	bool full_record;
 	int orig_size;
+	struct tls_rec *rec;
 	bool is_kvec = msg->msg_iter.type & ITER_KVEC;
+	struct crypto_tfm *tfm = crypto_aead_tfm(ctx->aead_send);
+	bool async_capable = tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
+	int num_async = 0;
+	int num_zc = 0;

Likewise.

net/tls/tls_sw.c int tls_sw_sendpage(struct sock *sk, struct page *page, · line 908

Quoted 6 lines
net/tls/tls_sw.c +2 -0
@@ -661,6 +904,8 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,
 	struct scatterlist *sg;
 	bool full_record;
 	int record_room;
+	struct tls_rec *rec;
+	int num_async = 0;

Likewise.

author
#2 lore raw
Quoted 22 lines

-----Original Message-----
From: David Miller <[email protected]>
Sent: Thursday, September 20, 2018 11:49 PM
To: Vakul Garg <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net-next] net/tls: Add support for async encryption of records for performance

From: Vakul Garg <[email protected]>
Date: Wed, 19 Sep 2018 20:51:35 +0530

Quoted 2 lines

This patch enables encryption of multiple records in parallel when an async capable crypto accelerator is present in system.

This seems to be trading off zero copy with async support.

Async crypto device support is not the common case at all, and synchronous crypto via cpu crypto acceleration instructions is so much more likely.

Quoted 7 lines

Oh I see, the new logic is only triggered with ASYNC_CAPABLE is set?

include/net/tls.h static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx) · line 346

Quoted 3 lines
include/net/tls.h +3 -0
+static inline  bool is_tx_ready(struct tls_context *tls_ctx,
+				struct tls_sw_context_tx *ctx)
+{

Two space between "inline" and "bool", please make it one.

Fixed.
Seems checkpatch misses it.

Quoted 7 lines

net/tls/tls_main.c int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx, · line 224

Quoted 4 lines

static void tls_write_space(struct sock *sk) {

net/tls/tls_main.c +1 -0
 	struct tls_context *ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);

Longest to shortest line (reverse christmas tree) ordering for local variable declarations please.

Can't do this. The second variable assignment is dependent upon previous one.

Quoted 7 lines

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 344

Quoted 4 lines
net/tls/tls_sw.c +3 -0
+	list_for_each_prev(pos, &ctx->tx_ready_list) {
+		struct tls_rec *rec = (struct tls_rec *)pos;
+		u64 seq = be64_to_cpup((const __be64 *)&rec->aad_space);

Likewise.

I can split variable declaration 'seq' and its assignment into two separate lines. But I am not sure if increasing number of lines in order to comply reverse Christmas tree is a good thing for this case.

Quoted 9 lines

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 361

Quoted 7 lines
net/tls/tls_sw.c +6 -1
-static int tls_do_encryption(struct tls_context *tls_ctx,
+int tls_tx_records(struct sock *sk, int flags) {
+	struct tls_rec *rec, *tmp;
+	struct tls_context *tls_ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+	int rc = 0;
+	int tx_flags;

Likewise.

Could partially address since ctx assignment depends upon tls_ctx assignment.

Quoted 12 lines

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 425

Quoted 10 lines
net/tls/tls_sw.c +9 -0
+static void tls_encrypt_done(struct crypto_async_request *req, int
+err) {
+	struct aead_request *aead_req = (struct aead_request *)req;
+	struct sock *sk = req->data;
+	struct tls_context *tls_ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+	struct tls_rec *rec;
+	int pending;
+	bool ready = false;

Likewise.

Placed 'ready' above pending 'pending'. Rest unchanged because of dependencies.

Quoted 12 lines

net/tls/tls_sw.c static void tls_free_both_sg(struct sock *sk) · line 476

Quoted 9 lines
net/tls/tls_sw.c +3 -0
+static int tls_do_encryption(struct sock *sk,
+			     struct tls_context *tls_ctx,
 			     struct tls_sw_context_tx *ctx,
 			     struct aead_request *aead_req,
 			     size_t data_len)
 {
 	int rc;
+	struct tls_rec *rec = ctx->open_rec;

Likewise.

Quoted 10 lines

net/tls/tls_sw.c static int memcopy_from_iter(struct sock *sk, · line 635

Quoted 8 lines
net/tls/tls_sw.c +2 -1
@@ -473,11 +630,12 @@ static int memcopy_from_iter(struct sock *sk,
 struct iov_iter *from,  {
 	struct tls_context *tls_ctx = tls_get_ctx(sk);
 	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
-	struct scatterlist *sg = ctx->sg_plaintext_data;
+	struct tls_rec *rec = ctx->open_rec;
+	struct scatterlist *sg = rec->sg_plaintext_data;
 	int copy, i, rc = 0;

Likewise.

Can't change because of dependencies.

Quoted 9 lines

net/tls/tls_sw.c static int memcopy_from_iter(struct sock *sk, struct iov_iter *from, · line 663

Quoted 6 lines
net/tls/tls_sw.c +5 -0
+struct tls_rec *get_rec(struct sock *sk) {
+	int mem_size;
+	struct tls_context *tls_ctx = tls_get_ctx(sk);
+	struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
+	struct tls_rec *rec;

Likewise.

Declared 'mem_size' below 'rec'.

Quoted 10 lines

Review int tls_sw_sendmsg(struct sock *sk, struct

Quoted 1 line
(quoted diff) +0 -0
@@ -510,21 +707,33 @@ int tls_sw_sendmsg(struct sock *sk, struct

msghdr *msg, size_t size)

net/tls/tls_sw.c int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)

Quoted 7 lines
net/tls/tls_sw.c +3 -0
 	int record_room;
 	bool full_record;
 	int orig_size;
+	struct tls_rec *rec;
 	bool is_kvec = msg->msg_iter.type & ITER_KVEC;
+	struct crypto_tfm *tfm = crypto_aead_tfm(ctx->aead_send);
+	bool async_capable = tfm->__crt_alg->cra_flags &

CRYPTO_ALG_ASYNC;

Quoted 4 lines

net/tls/tls_sw.c int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) · line 715

Quoted 2 lines
net/tls/tls_sw.c +2 -0
+	int num_async = 0;
+	int num_zc = 0;

Likewise.

Fixed

Quoted 9 lines

Review int tls_sw_sendpage(struct sock *sk, struct page

Quoted 1 line
(quoted diff) +0 -0
@@ -661,6 +904,8 @@ int tls_sw_sendpage(struct sock *sk, struct page

*page,

net/tls/tls_sw.c int tls_sw_sendpage(struct sock *sk, struct page *page, · line 908

Quoted 5 lines
net/tls/tls_sw.c +2 -0
 	struct scatterlist *sg;
 	bool full_record;
 	int record_room;
+	struct tls_rec *rec;
+	int num_async = 0;

Likewise.

Fixed.

Sending v2.