| 1 |
import unittest |
| 2 |
import struct |
| 3 |
import errno |
| 4 |
import json |
| 5 |
import os |
| 6 |
|
| 7 |
from pyverbs.providers.mlx5.mlx5dv_mkey import Mlx5Mkey, Mlx5MrInterleaved, \ |
| 8 |
Mlx5MkeyConfAttr, Mlx5SigCrc, Mlx5SigBlockDomain, Mlx5SigBlockAttr |
| 9 |
from pyverbs.providers.mlx5.mlx5dv_crypto import Mlx5CryptoLoginAttr, Mlx5DEK, \ |
| 10 |
Mlx5DEKInitAttr, Mlx5CryptoAttr, Mlx5CryptoLogin, Mlx5CryptoExtLoginAttr |
| 11 |
from pyverbs.providers.mlx5.mlx5dv import Mlx5Context, Mlx5DVContextAttr, \ |
| 12 |
Mlx5DVQPInitAttr, Mlx5QP |
| 13 |
from pyverbs.pyverbs_error import PyverbsRDMAError, PyverbsUserError |
| 14 |
from tests.mlx5_base import Mlx5RDMATestCase, Mlx5PyverbsAPITestCase |
| 15 |
from pyverbs.providers.mlx5.mlx5_enums import mlx5dv_crypto_engines_caps, mlx5dv_crypto_wrapped_import_method_caps, \ |
| 16 |
mlx5dv_crypto_caps_flags, mlx5dv_mkey_init_attr_flags, mlx5dv_qp_init_attr_mask, mlx5dv_crypto_key_size, \ |
| 17 |
mlx5dv_crypto_key_purpose, mlx5dv_crypto_login_state, mlx5dv_dek_init_attr_mask, mlx5dv_signature_crypto_order, \ |
| 18 |
mlx5dv_crypto_standard, mlx5dv_block_size, mlx5dv_sig_crc_type, mlx5dv_sig_type, mlx5dv_sig_mask, \ |
| 19 |
mlx5dv_qp_create_send_ops_flags |
| 20 |
from pyverbs.wr import SGE, SendWR, RecvWR |
| 21 |
from pyverbs.qp import QPInitAttrEx, QPCap |
| 22 |
from tests.base import RCResources |
| 23 |
from pyverbs.pd import PD |
| 24 |
from pyverbs.libibverbs_enums import ibv_qp_create_send_ops_flags, ibv_qp_init_attr_mask, ibv_qp_type, \ |
| 25 |
ibv_send_flags, ibv_access_flags, ibv_wr_opcode |
| 26 |
import tests.utils as u |
| 27 |
|
| 28 |
DEK_OPAQUE = b'dek' |
| 29 |
|
| 30 |
""" |
| 31 |
Crytpo operation requires specific input from the user, e.g. the wrapped credential that the |
| 32 |
device is configured with. The input should be provided in JSON format in a file in this path: |
| 33 |
"/tmp/mlx5_crypto_test.txt". |
| 34 |
User can also set this environment variable with his file path: MLX5_CRYPTO_TEST_INFO |
| 35 |
|
| 36 |
This doc describes the input option with examples to the format: |
| 37 |
Mandatory: |
| 38 |
Wrapped credential: The credential that was configured in the device wrapped. |
| 39 |
Wrapped key: Wrapped key for the DEK creation. The key should be encrypted using the KEK |
| 40 |
(Key Encrypted Key). |
| 41 |
Optional: |
| 42 |
Wrapped 256 bits key: Wrapped key for the DEK creation when the key size of 256 bits is required. |
| 43 |
If not provided, that test will only use the key in size of 128 bits. |
| 44 |
Encrypted data for 512 of 'c': If a user wants to have data validation, he needs to provide |
| 45 |
expected encrypted data for a plain text of 512 bytes of the |
| 46 |
character 'c'. If not provided, data validation will be skipped. |
| 47 |
|
| 48 |
Example of content of such file: |
| 49 |
[{"credential": [8704278040424473809, 4403447855848063568, 13892768337045135232, |
| 50 |
5942481448427925932, 171338997253969038, 5703425261028721211], |
| 51 |
"wrapped_key": [contains 5 integers of 64bits each], |
| 52 |
"plaintext_key": [contains 8 integers of 32bits each], |
| 53 |
"plaintext_256_bits_key": [contains 16 integers of 32bits each], |
| 54 |
"encrypted_data_for_512_c": [contains 64 integers of 64bits each], |
| 55 |
"wrapped_256_bits_key": [contains 9 integers of 64bits each]}] |
| 56 |
""" |
| 57 |
|
| 58 |
|
| 59 |
def check_crypto_caps(dev_name, is_wrapped_dek_mode, multi_block_support=False): |
| 60 |
""" |
| 61 |
Check that this device support crypto actions. |
| 62 |
:param dev_name: The device name. |
| 63 |
:param is_wrapped_dek_mode: True when wrapped_dek and False when plaintext. |
| 64 |
:param multi_block_support: If True, check for multi-block support. |
| 65 |
""" |
| 66 |
mlx5dv_attr = Mlx5DVContextAttr() |
| 67 |
with Mlx5Context(mlx5dv_attr, name=dev_name) as ctx: |
| 68 |
crypto_caps = ctx.query_mlx5_device().crypto_caps |
| 69 |
failed_selftests = crypto_caps['failed_selftests'] |
| 70 |
if failed_selftests: |
| 71 |
raise unittest.SkipTest(f'The device crypto selftest failed ({failed_selftests})') |
| 72 |
single_block_cap = mlx5dv_crypto_engines_caps.MLX5DV_CRYPTO_ENGINES_CAP_AES_XTS_SINGLE_BLOCK \ |
| 73 |
& crypto_caps['crypto_engines'] |
| 74 |
multi_block_cap = mlx5dv_crypto_engines_caps.MLX5DV_CRYPTO_ENGINES_CAP_AES_XTS_MULTI_BLOCK \ |
| 75 |
& crypto_caps['crypto_engines'] |
| 76 |
if not single_block_cap and not multi_block_cap: |
| 77 |
raise unittest.SkipTest('The device crypto engines does not support AES') |
| 78 |
elif multi_block_support and not multi_block_cap: |
| 79 |
raise unittest.SkipTest('The device crypto engines does not support multi blocks') |
| 80 |
elif multi_block_cap: |
| 81 |
assert single_block_cap, \ |
| 82 |
'The device crypto engines do not support single block but support multi blocks' |
| 83 |
dev_wrapped_import_method = crypto_caps['wrapped_import_method'] |
| 84 |
if is_wrapped_dek_mode: |
| 85 |
if not mlx5dv_crypto_wrapped_import_method_caps.MLX5DV_CRYPTO_WRAPPED_IMPORT_METHOD_CAP_AES_XTS & dev_wrapped_import_method or \ |
| 86 |
not mlx5dv_crypto_caps_flags.MLX5DV_CRYPTO_CAPS_WRAPPED_CRYPTO_OPERATIONAL & crypto_caps['flags']: |
| 87 |
raise unittest.SkipTest('The device does not support wrapped DEK') |
| 88 |
elif mlx5dv_crypto_wrapped_import_method_caps.MLX5DV_CRYPTO_WRAPPED_IMPORT_METHOD_CAP_AES_XTS & dev_wrapped_import_method and \ |
| 89 |
mlx5dv_crypto_caps_flags.MLX5DV_CRYPTO_CAPS_WRAPPED_CRYPTO_OPERATIONAL & crypto_caps['flags']: |
| 90 |
raise unittest.SkipTest('The device does not support plaintext DEK') |
| 91 |
|
| 92 |
|
| 93 |
def require_crypto_login_details(instance): |
| 94 |
""" |
| 95 |
Parse the crypto login session details from this file: |
| 96 |
'/tmp/mlx5_crypto_test.txt' |
| 97 |
If the file doesn't exists or the content is not in Json format, skip the test. |
| 98 |
:param instance: The test instance. |
| 99 |
""" |
| 100 |
crypto_file = '/tmp/mlx5_crypto_test.txt' |
| 101 |
if 'MLX5_CRYPTO_TEST_INFO' in os.environ: |
| 102 |
crypto_file = os.environ['MLX5_CRYPTO_TEST_INFO'] |
| 103 |
try: |
| 104 |
with open(crypto_file, 'r') as f: |
| 105 |
test_details = f.read() |
| 106 |
setattr(instance, 'crypto_details', test_details) |
| 107 |
json.loads(test_details) |
| 108 |
instance.crypto_details = json.loads(test_details)[0] |
| 109 |
except json.JSONDecodeError: |
| 110 |
raise unittest.SkipTest(f'The crypto data in {crypto_file} must be in Json format') |
| 111 |
except FileNotFoundError: |
| 112 |
raise unittest.SkipTest(f'Crypto login details must be supplied in {crypto_file}') |
| 113 |
|
| 114 |
|
| 115 |
def requires_crypto_support(is_wrapped_dek_mode, multi_block_support=False): |
| 116 |
""" |
| 117 |
:param is_wrapped_dek_mode: True when wapped_dek and False when plaintext. |
| 118 |
:param multi_block_support: If True, check for multi-block support. |
| 119 |
""" |
| 120 |
def outer(func): |
| 121 |
def inner(instance): |
| 122 |
require_crypto_login_details(instance) |
| 123 |
check_crypto_caps(instance.dev_name, is_wrapped_dek_mode, multi_block_support) |
| 124 |
return func(instance) |
| 125 |
return inner |
| 126 |
return outer |
| 127 |
|
| 128 |
|
| 129 |
class Mlx5CryptoResources(RCResources): |
| 130 |
def __init__(self, dev_name, ib_port, gid_index, dv_send_ops_flags=0, |
| 131 |
mkey_create_flags=mlx5dv_mkey_init_attr_flags.MLX5DV_MKEY_INIT_ATTR_FLAGS_INDIRECT, |
| 132 |
msg_size=1024): |
| 133 |
self.dv_send_ops_flags = dv_send_ops_flags |
| 134 |
self.mkey_create_flags = mkey_create_flags |
| 135 |
self.max_inline_data = 512 |
| 136 |
self.send_ops_flags = ibv_qp_create_send_ops_flags.IBV_QP_EX_WITH_SEND |
| 137 |
super().__init__(dev_name, ib_port, gid_index, msg_size=msg_size) |
| 138 |
self.create_mkeys() |
| 139 |
|
| 140 |
def create_mkeys(self): |
| 141 |
try: |
| 142 |
self.wire_enc_mkey = Mlx5Mkey(self.pd, self.mkey_create_flags, max_entries=1) |
| 143 |
self.mem_enc_mkey = Mlx5Mkey(self.pd, self.mkey_create_flags, max_entries=1) |
| 144 |
except PyverbsRDMAError as ex: |
| 145 |
if ex.error_code == errno.EOPNOTSUPP: |
| 146 |
raise unittest.SkipTest('Create Mkey is not supported') |
| 147 |
raise ex |
| 148 |
|
| 149 |
def create_qp_cap(self): |
| 150 |
return QPCap(max_send_wr=self.num_msgs, max_recv_wr=self.num_msgs, |
| 151 |
max_inline_data=self.max_inline_data) |
| 152 |
|
| 153 |
def create_qp_init_attr(self): |
| 154 |
comp_mask = ibv_qp_init_attr_mask.IBV_QP_INIT_ATTR_PD | ibv_qp_init_attr_mask.IBV_QP_INIT_ATTR_SEND_OPS_FLAGS |
| 155 |
return QPInitAttrEx(cap=self.create_qp_cap(), pd=self.pd, scq=self.cq, |
| 156 |
rcq=self.cq, qp_type=ibv_qp_type.IBV_QPT_RC, |
| 157 |
send_ops_flags=self.send_ops_flags, |
| 158 |
comp_mask=comp_mask) |
| 159 |
|
| 160 |
def create_qps(self): |
| 161 |
try: |
| 162 |
qp_init_attr = self.create_qp_init_attr() |
| 163 |
comp_mask = mlx5dv_qp_init_attr_mask.MLX5DV_QP_INIT_ATTR_MASK_SEND_OPS_FLAGS |
| 164 |
attr = Mlx5DVQPInitAttr(comp_mask=comp_mask, |
| 165 |
send_ops_flags=self.dv_send_ops_flags) |
| 166 |
qp = Mlx5QP(self.ctx, qp_init_attr, attr) |
| 167 |
self.qps.append(qp) |
| 168 |
self.qps_num.append(qp.qp_num) |
| 169 |
self.psns.append(0) |
| 170 |
except PyverbsRDMAError as ex: |
| 171 |
if ex.error_code == errno.EOPNOTSUPP: |
| 172 |
raise unittest.SkipTest('Create Mlx5DV QP is not supported') |
| 173 |
raise ex |
| 174 |
|
| 175 |
|
| 176 |
class Mlx5CryptoAPITest(Mlx5PyverbsAPITestCase): |
| 177 |
def __init__(self, methodName='runTest'): |
| 178 |
super().__init__(methodName) |
| 179 |
self.crypto_details = None |
| 180 |
|
| 181 |
def verify_create_dek_out_of_login_session(self): |
| 182 |
""" |
| 183 |
Verify that create DEK out of crypto login session is not permited. |
| 184 |
""" |
| 185 |
with self.assertRaises(PyverbsRDMAError) as ex: |
| 186 |
Mlx5DEK(self.ctx, self.dek_init_attr) |
| 187 |
self.assertEqual(ex.exception.error_code, errno.EINVAL) |
| 188 |
|
| 189 |
def verify_login_state(self, expected_state): |
| 190 |
""" |
| 191 |
Query the session login state and verify that it's as expected. |
| 192 |
""" |
| 193 |
state = Mlx5Context.query_login_state(self.ctx) |
| 194 |
self.assertEqual(state, expected_state) |
| 195 |
|
| 196 |
def verify_login_twice(self): |
| 197 |
""" |
| 198 |
Verify that when there is already a login session alive the second login |
| 199 |
fails. |
| 200 |
""" |
| 201 |
with self.assertRaises(PyverbsRDMAError) as ex: |
| 202 |
Mlx5Context.crypto_login(self.ctx, self.login_attr) |
| 203 |
self.assertEqual(ex.exception.error_code, errno.EEXIST) |
| 204 |
|
| 205 |
def verify_dek_opaque(self): |
| 206 |
""" |
| 207 |
Query the DEK and verify that its opaque is as expected. |
| 208 |
""" |
| 209 |
dek_attr = self.dek.query() |
| 210 |
self.assertEqual(dek_attr.opaque, DEK_OPAQUE) |
| 211 |
|
| 212 |
@requires_crypto_support(is_wrapped_dek_mode=True) |
| 213 |
def test_mlx5_dek_management(self): |
| 214 |
""" |
| 215 |
Test crypto login and DEK management APIs. |
| 216 |
The test checks also that invalid actions are not permited, e.g, create |
| 217 |
DEK not in login session on wrapped DEK mode. |
| 218 |
""" |
| 219 |
try: |
| 220 |
self.pd = PD(self.ctx) |
| 221 |
cred_bytes = struct.pack('!6Q', *self.crypto_details['credential']) |
| 222 |
key = struct.pack('!5Q', *self.crypto_details['wrapped_key']) |
| 223 |
self.dek_init_attr = \ |
| 224 |
Mlx5DEKInitAttr(self.pd, key=key, |
| 225 |
key_size=mlx5dv_crypto_key_size.MLX5DV_CRYPTO_KEY_SIZE_128, |
| 226 |
key_purpose=mlx5dv_crypto_key_purpose.MLX5DV_CRYPTO_KEY_PURPOSE_AES_XTS, |
| 227 |
opaque=DEK_OPAQUE) |
| 228 |
self.verify_create_dek_out_of_login_session() |
| 229 |
self.verify_login_state(mlx5dv_crypto_login_state.MLX5DV_CRYPTO_LOGIN_STATE_NO_LOGIN) |
| 230 |
|
| 231 |
# Login to crypto session |
| 232 |
self.login_attr = Mlx5CryptoLoginAttr(cred_bytes) |
| 233 |
Mlx5Context.crypto_login(self.ctx, self.login_attr) |
| 234 |
self.verify_login_state(mlx5dv_crypto_login_state.MLX5DV_CRYPTO_LOGIN_STATE_VALID) |
| 235 |
self.verify_login_twice() |
| 236 |
self.dek = Mlx5DEK(self.ctx, self.dek_init_attr) |
| 237 |
self.verify_dek_opaque() |
| 238 |
self.dek.close() |
| 239 |
|
| 240 |
# Logout from crypto session |
| 241 |
Mlx5Context.crypto_logout(self.ctx) |
| 242 |
self.verify_login_state(mlx5dv_crypto_login_state.MLX5DV_CRYPTO_LOGIN_STATE_NO_LOGIN) |
| 243 |
except PyverbsRDMAError as ex: |
| 244 |
print(ex) |
| 245 |
if ex.error_code == errno.EOPNOTSUPP: |
| 246 |
raise unittest.SkipTest('Create crypto elements is not supported') |
| 247 |
raise ex |
| 248 |
|
| 249 |
|
| 250 |
class Mlx5CryptoTrafficTest(Mlx5RDMATestCase): |
| 251 |
""" |
| 252 |
Test the mlx5 cryto APIs. |
| 253 |
""" |
| 254 |
def setUp(self): |
| 255 |
super().setUp() |
| 256 |
self.iters = 10 |
| 257 |
self.crypto_details = None |
| 258 |
self.validate_data = False |
| 259 |
self.is_multi_block = False |
| 260 |
self.msg_size = 1024 |
| 261 |
self.key_size = mlx5dv_crypto_key_size.MLX5DV_CRYPTO_KEY_SIZE_128 |
| 262 |
|
| 263 |
def create_client_dek(self): |
| 264 |
""" |
| 265 |
Create DEK using the client resources. |
| 266 |
""" |
| 267 |
cred_bytes = struct.pack('!6Q', *self.crypto_details['credential']) |
| 268 |
log_attr = Mlx5CryptoLoginAttr(cred_bytes) |
| 269 |
Mlx5Context.crypto_login(self.client.ctx, log_attr) |
| 270 |
self._create_wrapped_dek() |
| 271 |
|
| 272 |
def create_client_wrapped_dek_login_obj(self): |
| 273 |
""" |
| 274 |
Create CryptoExtLoginAttr and crypto login object |
| 275 |
""" |
| 276 |
cred_bytes = struct.pack('!6Q', *self.crypto_details['credential']) |
| 277 |
log_attr = Mlx5CryptoExtLoginAttr(cred_bytes, 48) |
| 278 |
crypto_login_obj = Mlx5CryptoLogin(self.client.ctx, log_attr) |
| 279 |
comp_mask=mlx5dv_dek_init_attr_mask.MLX5DV_DEK_INIT_ATTR_CRYPTO_LOGIN |
| 280 |
self._create_wrapped_dek(comp_mask, crypto_login_obj) |
| 281 |
|
| 282 |
def _create_wrapped_dek(self, comp_mask=0, crypto_login_obj=None): |
| 283 |
""" |
| 284 |
Create wrapped DEK using the client resources. |
| 285 |
""" |
| 286 |
key = struct.pack('!5Q', *self.crypto_details['wrapped_key']) |
| 287 |
if self.key_size == mlx5dv_crypto_key_size.MLX5DV_CRYPTO_KEY_SIZE_256: |
| 288 |
key = struct.pack('!9Q', *self.crypto_details['wrapped_256_bits_key']) |
| 289 |
|
| 290 |
if comp_mask == mlx5dv_dek_init_attr_mask.MLX5DV_DEK_INIT_ATTR_CRYPTO_LOGIN: |
| 291 |
self.dek_attr = Mlx5DEKInitAttr(self.client.pd, key=key, |
| 292 |
key_size=self.key_size, |
| 293 |
key_purpose=mlx5dv_crypto_key_purpose.MLX5DV_CRYPTO_KEY_PURPOSE_AES_XTS, |
| 294 |
comp_mask=comp_mask, crypto_login=crypto_login_obj) |
| 295 |
else: |
| 296 |
self.dek_attr = Mlx5DEKInitAttr(self.client.pd, key=key, |
| 297 |
key_size=self.key_size, |
| 298 |
key_purpose=mlx5dv_crypto_key_purpose.MLX5DV_CRYPTO_KEY_PURPOSE_AES_XTS) |
| 299 |
|
| 300 |
self.dek = Mlx5DEK(self.client.ctx, self.dek_attr) |
| 301 |
|
| 302 |
def create_client_plaintext_dek(self): |
| 303 |
""" |
| 304 |
Create DEK using the client resources. |
| 305 |
""" |
| 306 |
key = struct.pack('8I',*self.crypto_details['plaintext_key']) |
| 307 |
if self.key_size == mlx5dv_crypto_key_size.MLX5DV_CRYPTO_KEY_SIZE_256: |
| 308 |
key = struct.pack('16I', *self.crypto_details['plaintext_256_bits_key']) |
| 309 |
self.dek_attr = Mlx5DEKInitAttr(self.client.pd,key=key, |
| 310 |
key_size=self.key_size, |
| 311 |
comp_mask=mlx5dv_dek_init_attr_mask.MLX5DV_DEK_INIT_ATTR_CRYPTO_LOGIN, |
| 312 |
crypto_login=None) |
| 313 |
self.dek = Mlx5DEK(self.client.ctx, self.dek_attr) |
| 314 |
|
| 315 |
def reg_client_mkey(self, signature=False): |
| 316 |
""" |
| 317 |
Configure an mkey with crypto attributes. |
| 318 |
:param signature: True if signature configuration requested. |
| 319 |
""" |
| 320 |
num_of_configuration = 4 if signature else 3 |
| 321 |
for mkey in [self.client.wire_enc_mkey, self.client.mem_enc_mkey]: |
| 322 |
self.client.qp.wr_start() |
| 323 |
self.client.qp.wr_flags = ibv_send_flags.IBV_SEND_SIGNALED | ibv_send_flags.IBV_SEND_INLINE |
| 324 |
offset = 0 if mkey == self.client.wire_enc_mkey else self.client.msg_size/2 |
| 325 |
sge = SGE(self.client.mr.buf + offset, self.client.msg_size/2, self.client.mr.lkey) |
| 326 |
self.client.qp.wr_mkey_configure(mkey, num_of_configuration, Mlx5MkeyConfAttr()) |
| 327 |
self.client.qp.wr_set_mkey_access_flags(ibv_access_flags.IBV_ACCESS_LOCAL_WRITE) |
| 328 |
self.client.qp.wr_set_mkey_layout_list([sge]) |
| 329 |
if signature: |
| 330 |
self.configure_mkey_signature() |
| 331 |
initial_tweak = struct.pack('!2Q', int(0), int(0)) |
| 332 |
encrypt_on_tx = mkey == self.client.wire_enc_mkey |
| 333 |
sign_crypto_order = mlx5dv_signature_crypto_order.MLX5DV_SIGNATURE_CRYPTO_ORDER_SIGNATURE_BEFORE_CRYPTO_ON_TX |
| 334 |
crypto_attr = Mlx5CryptoAttr(crypto_standard=mlx5dv_crypto_standard.MLX5DV_CRYPTO_STANDARD_AES_XTS, |
| 335 |
encrypt_on_tx=encrypt_on_tx, |
| 336 |
signature_crypto_order=sign_crypto_order, |
| 337 |
data_unit_size=mlx5dv_block_size.MLX5DV_BLOCK_SIZE_512, |
| 338 |
dek=self.dek, initial_tweak=initial_tweak) |
| 339 |
self.client.qp.wr_set_mkey_crypto(crypto_attr) |
| 340 |
self.client.qp.wr_complete() |
| 341 |
u.poll_cq(self.client.cq) |
| 342 |
|
| 343 |
def configure_mkey_signature(self): |
| 344 |
""" |
| 345 |
Configure an mkey with signature attributes. |
| 346 |
""" |
| 347 |
sig_crc = Mlx5SigCrc(crc_type=mlx5dv_sig_crc_type.MLX5DV_SIG_CRC_TYPE_CRC32, seed=0xFFFFFFFF) |
| 348 |
sig_block_domain = Mlx5SigBlockDomain(sig_type=mlx5dv_sig_type.MLX5DV_SIG_TYPE_CRC, crc=sig_crc, |
| 349 |
block_size=mlx5dv_block_size.MLX5DV_BLOCK_SIZE_512) |
| 350 |
sig_attr = Mlx5SigBlockAttr(wire=sig_block_domain, |
| 351 |
check_mask=mlx5dv_sig_mask.MLX5DV_SIG_MASK_CRC32) |
| 352 |
self.client.qp.wr_set_mkey_sig_block(sig_attr) |
| 353 |
|
| 354 |
def get_send_wr(self, player, wire_encryption): |
| 355 |
mkey = player.wire_enc_mkey if wire_encryption else player.mem_enc_mkey |
| 356 |
sge = SGE(0, player.msg_size/2, mkey.lkey) |
| 357 |
return SendWR(opcode=ibv_wr_opcode.IBV_WR_SEND, num_sge=1, sg=[sge]) |
| 358 |
|
| 359 |
def get_recv_wr(self, player, wire_encryption): |
| 360 |
offset = 0 if wire_encryption else player.msg_size/2 |
| 361 |
sge = SGE(player.mr.buf + offset, player.msg_size/2, player.mr.lkey) |
| 362 |
return RecvWR(sg=[sge], num_sge=1) |
| 363 |
|
| 364 |
def prepare_validate_data(self): |
| 365 |
data_size = int(self.msg_size / 2) |
| 366 |
self.client.mr.write('c' * data_size, data_size) |
| 367 |
if self.is_multi_block: |
| 368 |
encrypted_data = struct.pack('!128Q', *self.crypto_details['encrypted_data_for_1024_c']) |
| 369 |
else: |
| 370 |
encrypted_data = struct.pack('!64Q', *self.crypto_details['encrypted_data_for_512_c']) |
| 371 |
self.client.mr.write(encrypted_data, data_size, offset=data_size) |
| 372 |
|
| 373 |
def validate_crypto_data(self): |
| 374 |
""" |
| 375 |
Validate the server MR data. Verify that the encryption/decryption works well. |
| 376 |
""" |
| 377 |
data_size= int(self.msg_size / 2) |
| 378 |
send_msg = self.client.mr.read(self.msg_size, 0) |
| 379 |
recv_msg = self.server.mr.read(self.msg_size, 0) |
| 380 |
self.assertEqual(send_msg[0:data_size], recv_msg[data_size:self.msg_size]) |
| 381 |
self.assertEqual(send_msg[data_size:self.msg_size], recv_msg[0:data_size]) |
| 382 |
|
| 383 |
def init_data_validation(self): |
| 384 |
if 'encrypted_data_for_512_c' in self.crypto_details and not self.is_multi_block: |
| 385 |
self.validate_data = True |
| 386 |
elif 'encrypted_data_for_1024_c' in self.crypto_details and self.is_multi_block: |
| 387 |
self.validate_data = True |
| 388 |
|
| 389 |
def traffic(self): |
| 390 |
""" |
| 391 |
Perform RC traffic using the configured mkeys. |
| 392 |
""" |
| 393 |
if self.validate_data: |
| 394 |
self.prepare_validate_data() |
| 395 |
for _ in range(self.iters): |
| 396 |
self.server.qp.post_recv(self.get_recv_wr(self.server, wire_encryption=True)) |
| 397 |
self.server.qp.post_recv(self.get_recv_wr(self.server, wire_encryption=False)) |
| 398 |
self.client.qp.post_send(self.get_send_wr(self.client, wire_encryption=True)) |
| 399 |
self.client.qp.post_send(self.get_send_wr(self.client, wire_encryption=False)) |
| 400 |
u.poll_cq(self.client.cq, count=2) |
| 401 |
u.poll_cq(self.server.cq, count=2) |
| 402 |
if self.validate_data: |
| 403 |
self.validate_crypto_data() |
| 404 |
|
| 405 |
def run_crypto_dek_test(self, create_dek_func): |
| 406 |
""" |
| 407 |
Creates player and DEK, using the give function, for different test |
| 408 |
cases, and runs traffic. |
| 409 |
:param create_dek_func: Function that creates a DEK. |
| 410 |
""" |
| 411 |
self.init_data_validation() |
| 412 |
mkey_flags = mlx5dv_mkey_init_attr_flags.MLX5DV_MKEY_INIT_ATTR_FLAGS_CRYPTO | \ |
| 413 |
mlx5dv_mkey_init_attr_flags.MLX5DV_MKEY_INIT_ATTR_FLAGS_INDIRECT |
| 414 |
self.create_players(Mlx5CryptoResources, |
| 415 |
dv_send_ops_flags=mlx5dv_qp_create_send_ops_flags.MLX5DV_QP_EX_WITH_MKEY_CONFIGURE, |
| 416 |
mkey_create_flags=mkey_flags, msg_size=self.msg_size) |
| 417 |
create_dek_func() |
| 418 |
self.reg_client_mkey() |
| 419 |
self.traffic() |
| 420 |
|
| 421 |
@requires_crypto_support(is_wrapped_dek_mode=True) |
| 422 |
def test_mlx5_crypto_mkey_old_api(self): |
| 423 |
""" |
| 424 |
Create Mkeys, register a memory layout using the mkeys, configure |
| 425 |
crypto attributes on it and then run traffic. |
| 426 |
""" |
| 427 |
self.run_crypto_dek_test(self.create_client_dek) |
| 428 |
|
| 429 |
@requires_crypto_support(is_wrapped_dek_mode=True) |
| 430 |
def test_mlx5_crypto_wrapped_dek(self): |
| 431 |
""" |
| 432 |
Create Mkeys, register a memory layout using the mkeys, |
| 433 |
configure crypto attributes on it using login object API and then run |
| 434 |
traffic. |
| 435 |
Use wrapped DEK with new API. |
| 436 |
""" |
| 437 |
self.run_crypto_dek_test(self.create_client_wrapped_dek_login_obj) |
| 438 |
|
| 439 |
@requires_crypto_support(is_wrapped_dek_mode=False) |
| 440 |
def test_mlx5_crypto_plaintext_dek(self): |
| 441 |
""" |
| 442 |
Create Mkeys, register a memory layout using the mkeys, |
| 443 |
configure crypto attributes on it using login object API and then run |
| 444 |
traffic. |
| 445 |
Use plaintext DEK. |
| 446 |
""" |
| 447 |
self.run_crypto_dek_test(self.create_client_plaintext_dek) |
| 448 |
|
| 449 |
@requires_crypto_support(is_wrapped_dek_mode=True) |
| 450 |
def test_mlx5_crypto_signature_mkey(self): |
| 451 |
""" |
| 452 |
Create Mkeys, register a memory layout using this mkey, configure |
| 453 |
crypto and signature attributes on it and then perform traffic using |
| 454 |
this mkey. |
| 455 |
""" |
| 456 |
if 'wrapped_256_bits_key' in self.crypto_details: |
| 457 |
self.key_size = mlx5dv_crypto_key_size.MLX5DV_CRYPTO_KEY_SIZE_256 |
| 458 |
mkey_flags = mlx5dv_mkey_init_attr_flags.MLX5DV_MKEY_INIT_ATTR_FLAGS_CRYPTO | \ |
| 459 |
mlx5dv_mkey_init_attr_flags.MLX5DV_MKEY_INIT_ATTR_FLAGS_INDIRECT | \ |
| 460 |
mlx5dv_mkey_init_attr_flags.MLX5DV_MKEY_INIT_ATTR_FLAGS_BLOCK_SIGNATURE |
| 461 |
self.create_players(Mlx5CryptoResources, |
| 462 |
dv_send_ops_flags=mlx5dv_qp_create_send_ops_flags.MLX5DV_QP_EX_WITH_MKEY_CONFIGURE, |
| 463 |
mkey_create_flags=mkey_flags) |
| 464 |
self.create_client_dek() |
| 465 |
self.reg_client_mkey(signature=True) |
| 466 |
self.traffic() |
| 467 |
|
| 468 |
@requires_crypto_support(is_wrapped_dek_mode=False, multi_block_support=True) |
| 469 |
def test_mlx5_plaintext_dek_multi_block(self): |
| 470 |
self.is_multi_block = True |
| 471 |
self.msg_size = 2048 |
| 472 |
self.run_crypto_dek_test(self.create_client_plaintext_dek) |
| 473 |
|
| 474 |
@requires_crypto_support(is_wrapped_dek_mode=True, multi_block_support=True) |
| 475 |
def test_mlx5_wrapped_dek_multi_block(self): |
| 476 |
self.is_multi_block = True |
| 477 |
self.msg_size = 2048 |
| 478 |
self.run_crypto_dek_test(self.create_client_wrapped_dek_login_obj) |