Skip to content

Commit 71af6a2

Browse files
committed
Merge tag 'mlx5-updates-2023-01-30' of git://git.kernel.org/pub/scm/linux/kernel/git/saeed/linux
Saeed Mahameed says: ==================== mlx5-updates-2023-01-30 Add fast update encryption key Jianbo Liu Says: ================ Data encryption keys (DEKs) are the keys used for data encryption and decryption operations. Starting from version 22.33.0783, firmware is optimized to accelerate the update of user keys into DEK object in hardware. The support for bulk allocation and destruction of DEK objects is added, and the bulk allocated DEKs are uninitialized, as the bulk creation requires no input key. When offload encryption/decryption, user gets one object from a bulk, and updates key by a new "modify DEK" command. This command is the same as create DEK object, but requires no heavy context memory allocation in firmware, which consumes most cpu cycles of the create DEK command. DEKs are cached internally by the NIC, so invalidating internal NIC caches is required before reusing DEKs. The SYNC_CRYPTO command is added to support it. DEK object can be reused, the keys in it can be updated after this command is executed. This patchset enhances the key creation and destruction flow, to get use of this new feature. Any user, for example, ktls, ipsec and macsec, can use it to offload keys. But, only ktls uses it, as others don't need many keys, and caching two many DEKs in pool is wasteful. There are two new data struts added: a. DEK pool. One pool is created for each key type. The bulks by the type, are placed in the pool's different bulk lists, according to the number of available and in_used DEKs in the bulk. b. DEK bulk. All DEKs in one bulk allocation are store here. There are two bitmaps to indicate the state of each DEK. New APIs are then added. When user need a DEK object, a. Fetch one bulk with avail DEKs, from the partial_list or avail_list, otherwise create new one. b. Pick one DEK, and set its need_sync and in_used bits to 1. Move the bulk to full_list if no more available keys, or put it to partial_list if the bulk is newly created. c. Update DEK object's key with user key, by the "modify DEK" command. d. Return DEK struct to user, then it gets the object id and fills it into the offload commands. When user free a DEK, a. Set in_use bit to 0. If all need_sync bits are 1 and all in_use bits of this bulk are 0, move it to sync_list. b. If the number of DEKs, which are freed by users, is over the threshold (128), schedule a workqueue to do the sync process. For the sync process, the SYNC_CRYPTO command is executed first. Then, for each bulks in partial_list, full_list and sync_list, reset need_sync bits of the freed DEK objects. If all need_sync bits in one bulk are zero, move it to avail_list. We already supported TIS pool to recycle the TISes. With this series and TIS pool, TLS CPS performance is improved greatly. And we tested https on the system: CPU: dual AMD EPYC 7763 64-Core processors RAM: 512G DEV: ConnectX-6 DX, with FW ver 22.33.0838 and TLS_OPTIMISE=true TLS CPS performance numbers are: Before: 11k connections/sec After: 101 connections/sec ================ * tag 'mlx5-updates-2023-01-30' of git://git.kernel.org/pub/scm/linux/kernel/git/saeed/linux: net/mlx5e: kTLS, Improve connection rate by using fast update encryption key net/mlx5: Keep only one bulk of full available DEKs net/mlx5: Add async garbage collector for DEK bulk net/mlx5: Reuse DEKs after executing SYNC_CRYPTO command net/mlx5: Use bulk allocation for fast update encryption key net/mlx5: Add bulk allocation and modify_dek operation net/mlx5: Add support SYNC_CRYPTO command net/mlx5: Add new APIs for fast update encryption key net/mlx5: Refactor the encryption key creation net/mlx5: Add const to the key pointer of encryption key creation net/mlx5: Prepare for fast crypto key update if hardware supports it net/mlx5: Change key type to key purpose net/mlx5: Add IFC bits and enums for crypto key net/mlx5: Add IFC bits for general obj create param net/mlx5: Header file for crypto ==================== Link: https://lore.kernel.org/r/20230131031201.35336-1-saeed@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents c925ed5 + f741db1 commit 71af6a2

File tree

17 files changed

+992
-86
lines changed

17 files changed

+992
-86
lines changed

drivers/net/ethernet/mellanox/mlx5/core/cmd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static bool mlx5_cmd_is_throttle_opcode(u16 op)
104104
case MLX5_CMD_OP_DESTROY_GENERAL_OBJECT:
105105
case MLX5_CMD_OP_MODIFY_GENERAL_OBJECT:
106106
case MLX5_CMD_OP_QUERY_GENERAL_OBJECT:
107+
case MLX5_CMD_OP_SYNC_CRYPTO:
107108
return true;
108109
}
109110
return false;
@@ -523,6 +524,7 @@ static int mlx5_internal_err_ret_value(struct mlx5_core_dev *dev, u16 op,
523524
case MLX5_CMD_OP_QUERY_VHCA_MIGRATION_STATE:
524525
case MLX5_CMD_OP_SAVE_VHCA_STATE:
525526
case MLX5_CMD_OP_LOAD_VHCA_STATE:
527+
case MLX5_CMD_OP_SYNC_CRYPTO:
526528
*status = MLX5_DRIVER_STATUS_ABORTED;
527529
*synd = MLX5_DRIVER_SYND;
528530
return -ENOLINK;
@@ -725,6 +727,7 @@ const char *mlx5_command_str(int command)
725727
MLX5_COMMAND_STR_CASE(QUERY_VHCA_MIGRATION_STATE);
726728
MLX5_COMMAND_STR_CASE(SAVE_VHCA_STATE);
727729
MLX5_COMMAND_STR_CASE(LOAD_VHCA_STATE);
730+
MLX5_COMMAND_STR_CASE(SYNC_CRYPTO);
728731
default: return "unknown command opcode";
729732
}
730733
}

drivers/net/ethernet/mellanox/mlx5/core/en/tc/meter.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,15 @@ mlx5e_flow_meter_create_aso_obj(struct mlx5e_flow_meters *flow_meters, int *obj_
204204
u32 in[MLX5_ST_SZ_DW(create_flow_meter_aso_obj_in)] = {};
205205
u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
206206
struct mlx5_core_dev *mdev = flow_meters->mdev;
207-
void *obj;
207+
void *obj, *param;
208208
int err;
209209

210210
MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
211211
MLX5_SET(general_obj_in_cmd_hdr, in, obj_type,
212212
MLX5_GENERAL_OBJECT_TYPES_FLOW_METER_ASO);
213-
MLX5_SET(general_obj_in_cmd_hdr, in, log_obj_range, flow_meters->log_granularity);
213+
param = MLX5_ADDR_OF(general_obj_in_cmd_hdr, in, op_param);
214+
MLX5_SET(general_obj_create_param, param, log_obj_range,
215+
flow_meters->log_granularity);
214216

215217
obj = MLX5_ADDR_OF(create_flow_meter_aso_obj_in, in, flow_meter_aso_obj);
216218
MLX5_SET(flow_meter_aso_obj, obj, meter_aso_access_pd, flow_meters->pdn);

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "mlx5_core.h"
55
#include "en.h"
66
#include "ipsec.h"
7-
#include "lib/mlx5.h"
7+
#include "lib/crypto.h"
88

99
enum {
1010
MLX5_IPSEC_ASO_REMOVE_FLOW_PKT_CNT_OFFSET,

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
#include <linux/debugfs.h>
55
#include "en.h"
66
#include "lib/mlx5.h"
7+
#include "lib/crypto.h"
78
#include "en_accel/ktls.h"
89
#include "en_accel/ktls_utils.h"
910
#include "en_accel/fs_tcp.h"
1011

11-
int mlx5_ktls_create_key(struct mlx5_core_dev *mdev,
12-
struct tls_crypto_info *crypto_info,
13-
u32 *p_key_id)
12+
struct mlx5_crypto_dek *mlx5_ktls_create_key(struct mlx5_crypto_dek_pool *dek_pool,
13+
struct tls_crypto_info *crypto_info)
1414
{
15+
const void *key;
1516
u32 sz_bytes;
16-
void *key;
1717

1818
switch (crypto_info->cipher_type) {
1919
case TLS_CIPHER_AES_GCM_128: {
@@ -33,17 +33,16 @@ int mlx5_ktls_create_key(struct mlx5_core_dev *mdev,
3333
break;
3434
}
3535
default:
36-
return -EINVAL;
36+
return ERR_PTR(-EINVAL);
3737
}
3838

39-
return mlx5_create_encryption_key(mdev, key, sz_bytes,
40-
MLX5_ACCEL_OBJ_TLS_KEY,
41-
p_key_id);
39+
return mlx5_crypto_dek_create(dek_pool, key, sz_bytes);
4240
}
4341

44-
void mlx5_ktls_destroy_key(struct mlx5_core_dev *mdev, u32 key_id)
42+
void mlx5_ktls_destroy_key(struct mlx5_crypto_dek_pool *dek_pool,
43+
struct mlx5_crypto_dek *dek)
4544
{
46-
mlx5_destroy_encryption_key(mdev, key_id);
45+
mlx5_crypto_dek_destroy(dek_pool, dek);
4746
}
4847

4948
static int mlx5e_ktls_add(struct net_device *netdev, struct sock *sk,
@@ -189,6 +188,7 @@ static void mlx5e_tls_debugfs_init(struct mlx5e_tls *tls,
189188

190189
int mlx5e_ktls_init(struct mlx5e_priv *priv)
191190
{
191+
struct mlx5_crypto_dek_pool *dek_pool;
192192
struct mlx5e_tls *tls;
193193

194194
if (!mlx5e_is_ktls_device(priv->mdev))
@@ -197,9 +197,15 @@ int mlx5e_ktls_init(struct mlx5e_priv *priv)
197197
tls = kzalloc(sizeof(*tls), GFP_KERNEL);
198198
if (!tls)
199199
return -ENOMEM;
200+
tls->mdev = priv->mdev;
200201

202+
dek_pool = mlx5_crypto_dek_pool_create(priv->mdev, MLX5_ACCEL_OBJ_TLS_KEY);
203+
if (IS_ERR(dek_pool)) {
204+
kfree(tls);
205+
return PTR_ERR(dek_pool);
206+
}
207+
tls->dek_pool = dek_pool;
201208
priv->tls = tls;
202-
priv->tls->mdev = priv->mdev;
203209

204210
mlx5e_tls_debugfs_init(tls, priv->dfs_root);
205211

@@ -216,6 +222,7 @@ void mlx5e_ktls_cleanup(struct mlx5e_priv *priv)
216222
debugfs_remove_recursive(tls->debugfs.dfs);
217223
tls->debugfs.dfs = NULL;
218224

225+
mlx5_crypto_dek_pool_destroy(tls->dek_pool);
219226
kfree(priv->tls);
220227
priv->tls = NULL;
221228
}

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
#include "en.h"
1111

1212
#ifdef CONFIG_MLX5_EN_TLS
13-
int mlx5_ktls_create_key(struct mlx5_core_dev *mdev,
14-
struct tls_crypto_info *crypto_info,
15-
u32 *p_key_id);
16-
void mlx5_ktls_destroy_key(struct mlx5_core_dev *mdev, u32 key_id);
13+
#include "lib/crypto.h"
14+
15+
struct mlx5_crypto_dek *mlx5_ktls_create_key(struct mlx5_crypto_dek_pool *dek_pool,
16+
struct tls_crypto_info *crypto_info);
17+
void mlx5_ktls_destroy_key(struct mlx5_crypto_dek_pool *dek_pool,
18+
struct mlx5_crypto_dek *dek);
1719

1820
static inline bool mlx5e_is_ktls_device(struct mlx5_core_dev *mdev)
1921
{
@@ -83,6 +85,7 @@ struct mlx5e_tls {
8385
struct mlx5e_tls_sw_stats sw_stats;
8486
struct workqueue_struct *rx_wq;
8587
struct mlx5e_tls_tx_pool *tx_pool;
88+
struct mlx5_crypto_dek_pool *dek_pool;
8689
struct mlx5e_tls_debugfs debugfs;
8790
};
8891

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_rx.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct mlx5e_ktls_offload_context_rx {
5050
struct mlx5e_tls_sw_stats *sw_stats;
5151
struct completion add_ctx;
5252
struct mlx5e_tir tir;
53-
u32 key_id;
53+
struct mlx5_crypto_dek *dek;
5454
u32 rxq;
5555
DECLARE_BITMAP(flags, MLX5E_NUM_PRIV_RX_FLAGS);
5656

@@ -148,7 +148,8 @@ post_static_params(struct mlx5e_icosq *sq,
148148
wqe = MLX5E_TLS_FETCH_SET_STATIC_PARAMS_WQE(sq, pi);
149149
mlx5e_ktls_build_static_params(wqe, sq->pc, sq->sqn, &priv_rx->crypto_info,
150150
mlx5e_tir_get_tirn(&priv_rx->tir),
151-
priv_rx->key_id, priv_rx->resync.seq, false,
151+
mlx5_crypto_dek_get_id(priv_rx->dek),
152+
priv_rx->resync.seq, false,
152153
TLS_OFFLOAD_CTX_DIR_RX);
153154
wi = (struct mlx5e_icosq_wqe_info) {
154155
.wqe_type = MLX5E_ICOSQ_WQE_UMR_TLS,
@@ -610,20 +611,22 @@ int mlx5e_ktls_add_rx(struct net_device *netdev, struct sock *sk,
610611
struct mlx5e_ktls_offload_context_rx *priv_rx;
611612
struct mlx5e_ktls_rx_resync_ctx *resync;
612613
struct tls_context *tls_ctx;
613-
struct mlx5_core_dev *mdev;
614+
struct mlx5_crypto_dek *dek;
614615
struct mlx5e_priv *priv;
615616
int rxq, err;
616617

617618
tls_ctx = tls_get_ctx(sk);
618619
priv = netdev_priv(netdev);
619-
mdev = priv->mdev;
620620
priv_rx = kzalloc(sizeof(*priv_rx), GFP_KERNEL);
621621
if (unlikely(!priv_rx))
622622
return -ENOMEM;
623623

624-
err = mlx5_ktls_create_key(mdev, crypto_info, &priv_rx->key_id);
625-
if (err)
624+
dek = mlx5_ktls_create_key(priv->tls->dek_pool, crypto_info);
625+
if (IS_ERR(dek)) {
626+
err = PTR_ERR(dek);
626627
goto err_create_key;
628+
}
629+
priv_rx->dek = dek;
627630

628631
INIT_LIST_HEAD(&priv_rx->list);
629632
spin_lock_init(&priv_rx->lock);
@@ -673,7 +676,7 @@ int mlx5e_ktls_add_rx(struct net_device *netdev, struct sock *sk,
673676
err_post_wqes:
674677
mlx5e_tir_destroy(&priv_rx->tir);
675678
err_create_tir:
676-
mlx5_ktls_destroy_key(mdev, priv_rx->key_id);
679+
mlx5_ktls_destroy_key(priv->tls->dek_pool, priv_rx->dek);
677680
err_create_key:
678681
kfree(priv_rx);
679682
return err;
@@ -683,11 +686,9 @@ void mlx5e_ktls_del_rx(struct net_device *netdev, struct tls_context *tls_ctx)
683686
{
684687
struct mlx5e_ktls_offload_context_rx *priv_rx;
685688
struct mlx5e_ktls_rx_resync_ctx *resync;
686-
struct mlx5_core_dev *mdev;
687689
struct mlx5e_priv *priv;
688690

689691
priv = netdev_priv(netdev);
690-
mdev = priv->mdev;
691692

692693
priv_rx = mlx5e_get_ktls_rx_priv_ctx(tls_ctx);
693694
set_bit(MLX5E_PRIV_RX_FLAG_DELETING, priv_rx->flags);
@@ -707,7 +708,7 @@ void mlx5e_ktls_del_rx(struct net_device *netdev, struct tls_context *tls_ctx)
707708
mlx5e_accel_fs_del_sk(priv_rx->rule.rule);
708709

709710
mlx5e_tir_destroy(&priv_rx->tir);
710-
mlx5_ktls_destroy_key(mdev, priv_rx->key_id);
711+
mlx5_ktls_destroy_key(priv->tls->dek_pool, priv_rx->dek);
711712
/* priv_rx should normally be freed here, but if there is an outstanding
712713
* GET_PSV, deallocation will be delayed until the CQE for GET_PSV is
713714
* processed.

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct mlx5e_ktls_offload_context_tx {
9898
struct tls_offload_context_tx *tx_ctx;
9999
struct mlx5_core_dev *mdev;
100100
struct mlx5e_tls_sw_stats *sw_stats;
101-
u32 key_id;
101+
struct mlx5_crypto_dek *dek;
102102
u8 create_err : 1;
103103
};
104104

@@ -457,6 +457,7 @@ int mlx5e_ktls_add_tx(struct net_device *netdev, struct sock *sk,
457457
struct mlx5e_ktls_offload_context_tx *priv_tx;
458458
struct mlx5e_tls_tx_pool *pool;
459459
struct tls_context *tls_ctx;
460+
struct mlx5_crypto_dek *dek;
460461
struct mlx5e_priv *priv;
461462
int err;
462463

@@ -468,9 +469,12 @@ int mlx5e_ktls_add_tx(struct net_device *netdev, struct sock *sk,
468469
if (IS_ERR(priv_tx))
469470
return PTR_ERR(priv_tx);
470471

471-
err = mlx5_ktls_create_key(pool->mdev, crypto_info, &priv_tx->key_id);
472-
if (err)
472+
dek = mlx5_ktls_create_key(priv->tls->dek_pool, crypto_info);
473+
if (IS_ERR(dek)) {
474+
err = PTR_ERR(dek);
473475
goto err_create_key;
476+
}
477+
priv_tx->dek = dek;
474478

475479
priv_tx->expected_seq = start_offload_tcp_sn;
476480
switch (crypto_info->cipher_type) {
@@ -512,7 +516,7 @@ void mlx5e_ktls_del_tx(struct net_device *netdev, struct tls_context *tls_ctx)
512516
pool = priv->tls->tx_pool;
513517

514518
atomic64_inc(&priv_tx->sw_stats->tx_tls_del);
515-
mlx5_ktls_destroy_key(priv_tx->mdev, priv_tx->key_id);
519+
mlx5_ktls_destroy_key(priv->tls->dek_pool, priv_tx->dek);
516520
pool_push(pool, priv_tx);
517521
}
518522

@@ -551,8 +555,9 @@ post_static_params(struct mlx5e_txqsq *sq,
551555
pi = mlx5e_txqsq_get_next_pi(sq, num_wqebbs);
552556
wqe = MLX5E_TLS_FETCH_SET_STATIC_PARAMS_WQE(sq, pi);
553557
mlx5e_ktls_build_static_params(wqe, sq->pc, sq->sqn, &priv_tx->crypto_info,
554-
priv_tx->tisn, priv_tx->key_id, 0, fence,
555-
TLS_OFFLOAD_CTX_DIR_TX);
558+
priv_tx->tisn,
559+
mlx5_crypto_dek_get_id(priv_tx->dek),
560+
0, fence, TLS_OFFLOAD_CTX_DIR_TX);
556561
tx_fill_wi(sq, pi, num_wqebbs, 0, NULL);
557562
sq->pc += num_wqebbs;
558563
}

drivers/net/ethernet/mellanox/mlx5/core/en_accel/macsec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "en.h"
99
#include "lib/aso.h"
10-
#include "lib/mlx5.h"
10+
#include "lib/crypto.h"
1111
#include "en_accel/macsec.h"
1212
#include "en_accel/macsec_fs.h"
1313

drivers/net/ethernet/mellanox/mlx5/core/en_common.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*/
3232

3333
#include "en.h"
34+
#include "lib/crypto.h"
3435

3536
/* mlx5e global resources should be placed in this file.
3637
* Global resources are common to all the netdevices created on the same nic.
@@ -104,6 +105,13 @@ int mlx5e_create_mdev_resources(struct mlx5_core_dev *mdev)
104105
INIT_LIST_HEAD(&res->td.tirs_list);
105106
mutex_init(&res->td.list_lock);
106107

108+
mdev->mlx5e_res.dek_priv = mlx5_crypto_dek_init(mdev);
109+
if (IS_ERR(mdev->mlx5e_res.dek_priv)) {
110+
mlx5_core_err(mdev, "crypto dek init failed, %ld\n",
111+
PTR_ERR(mdev->mlx5e_res.dek_priv));
112+
mdev->mlx5e_res.dek_priv = NULL;
113+
}
114+
107115
return 0;
108116

109117
err_destroy_mkey:
@@ -119,6 +127,8 @@ void mlx5e_destroy_mdev_resources(struct mlx5_core_dev *mdev)
119127
{
120128
struct mlx5e_hw_objs *res = &mdev->mlx5e_res.hw_objs;
121129

130+
mlx5_crypto_dek_cleanup(mdev->mlx5e_res.dek_priv);
131+
mdev->mlx5e_res.dek_priv = NULL;
122132
mlx5_free_bfreg(mdev, &res->bfreg);
123133
mlx5_core_destroy_mkey(mdev, res->mkey);
124134
mlx5_core_dealloc_transport_domain(mdev, res->td.tdn);

drivers/net/ethernet/mellanox/mlx5/core/fw.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ int mlx5_query_hca_caps(struct mlx5_core_dev *dev)
267267
return err;
268268
}
269269

270+
if (MLX5_CAP_GEN(dev, crypto)) {
271+
err = mlx5_core_get_caps(dev, MLX5_CAP_CRYPTO);
272+
if (err)
273+
return err;
274+
}
275+
270276
if (MLX5_CAP_GEN(dev, shampo)) {
271277
err = mlx5_core_get_caps(dev, MLX5_CAP_DEV_SHAMPO);
272278
if (err)

0 commit comments

Comments
 (0)