| 1 |
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) |
| 2 |
# Copyright (c) 2019 Mellanox Technologies, Inc. All rights reserved. See COPYING file |
| 3 |
# Copyright (c) 2020 Intel Corporation. All rights reserved. See COPYING file |
| 4 |
""" |
| 5 |
Test module for pyverbs' mr module. |
| 6 |
""" |
| 7 |
import unittest |
| 8 |
import random |
| 9 |
import errno |
| 10 |
|
| 11 |
from tests.base import PyverbsAPITestCase, RCResources, RDMATestCase |
| 12 |
from pyverbs.pyverbs_error import PyverbsRDMAError, PyverbsError |
| 13 |
from pyverbs.mr import MR, MW, DMMR, DmaBufMR, MWBindInfo, MWBind, MREx |
| 14 |
from pyverbs.mem_alloc import posix_memalign, free |
| 15 |
from pyverbs.dmabuf import DmaBuf |
| 16 |
from pyverbs.qp import QPAttr |
| 17 |
from pyverbs.wr import SendWR |
| 18 |
import pyverbs.device as d |
| 19 |
from pyverbs.pd import PD |
| 20 |
from pyverbs.libibverbs_enums import ibv_access_flags, ibv_qp_state, ibv_qp_attr_mask, ibv_rereg_mr_flags, \ |
| 21 |
ibv_wr_opcode, ibv_send_flags, ibv_mw_type |
| 22 |
import tests.utils as u |
| 23 |
|
| 24 |
MAX_IO_LEN = 1048576 |
| 25 |
DM_INVALID_ALIGNMENT = 3 |
| 26 |
|
| 27 |
|
| 28 |
class MRRes(RCResources): |
| 29 |
def __init__(self, dev_name, ib_port, gid_index, |
| 30 |
mr_access=ibv_access_flags.IBV_ACCESS_LOCAL_WRITE): |
| 31 |
""" |
| 32 |
Initialize MR resources based on RC resources that include RC QP. |
| 33 |
:param dev_name: Device name to be used |
| 34 |
:param ib_port: IB port of the device to use |
| 35 |
:param gid_index: Which GID index to use |
| 36 |
:param mr_access: The MR access |
| 37 |
""" |
| 38 |
self.mr_access = mr_access |
| 39 |
super().__init__(dev_name=dev_name, ib_port=ib_port, gid_index=gid_index) |
| 40 |
|
| 41 |
def create_mr(self): |
| 42 |
try: |
| 43 |
self.mr = MR(self.pd, self.msg_size, self.mr_access) |
| 44 |
except PyverbsRDMAError as ex: |
| 45 |
if ex.error_code == errno.EOPNOTSUPP: |
| 46 |
raise unittest.SkipTest(f'Reg MR with access ({self.mr_access}) is not supported') |
| 47 |
raise ex |
| 48 |
|
| 49 |
def create_qp_attr(self): |
| 50 |
qp_attr = QPAttr(port_num=self.ib_port) |
| 51 |
qp_access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_WRITE | \ |
| 52 |
ibv_access_flags.IBV_ACCESS_REMOTE_ATOMIC |
| 53 |
qp_attr.qp_access_flags = qp_access |
| 54 |
return qp_attr |
| 55 |
|
| 56 |
def rereg_mr(self, flags, pd=None, addr=0, length=0, access=0): |
| 57 |
try: |
| 58 |
self.mr.rereg(flags, pd, addr, length, access) |
| 59 |
except PyverbsRDMAError as ex: |
| 60 |
if ex.error_code == errno.EOPNOTSUPP: |
| 61 |
raise unittest.SkipTest(f'Rereg MR is not supported ({str(ex)})') |
| 62 |
raise ex |
| 63 |
|
| 64 |
|
| 65 |
class MRExRes(MRRes): |
| 66 |
@u.skip_unsupported |
| 67 |
def create_mr(self): |
| 68 |
self.mr = MREx(self.pd, self.msg_size, self.mr_access) |
| 69 |
|
| 70 |
|
| 71 |
class MRTest(RDMATestCase): |
| 72 |
""" |
| 73 |
Test various functionalities of the MR class. |
| 74 |
""" |
| 75 |
def setUp(self): |
| 76 |
super().setUp() |
| 77 |
self.iters = 10 |
| 78 |
self.server = None |
| 79 |
self.client = None |
| 80 |
self.server_qp_attr = None |
| 81 |
self.client_qp_attr = None |
| 82 |
self.traffic_args = None |
| 83 |
|
| 84 |
def query_qp_attrs(self): |
| 85 |
"""Query and store QP attributes for both server and client.""" |
| 86 |
self.server_qp_attr, _ = self.server.qp.query(0x1ffffff) |
| 87 |
self.client_qp_attr, _ = self.client.qp.query(0x1ffffff) |
| 88 |
|
| 89 |
def create_players_and_query_qp(self, res_cls, **kwargs): |
| 90 |
"""Create players of a given resource class and query their QPs.""" |
| 91 |
self.create_players(res_cls, **kwargs) |
| 92 |
self.query_qp_attrs() |
| 93 |
|
| 94 |
def restate_qps(self): |
| 95 |
""" |
| 96 |
Restate the resources QPs from ERR back to RTS state. |
| 97 |
""" |
| 98 |
self.server.qp.modify(QPAttr(qp_state=ibv_qp_state.IBV_QPS_RESET), ibv_qp_attr_mask.IBV_QP_STATE) |
| 99 |
self.server.qp.to_rts(self.server_qp_attr) |
| 100 |
self.client.qp.modify(QPAttr(qp_state=ibv_qp_state.IBV_QPS_RESET), ibv_qp_attr_mask.IBV_QP_STATE) |
| 101 |
self.client.qp.to_rts(self.client_qp_attr) |
| 102 |
|
| 103 |
def validate_rereg_addr(self, res_cls): |
| 104 |
"""Common logic for address translation re-registration tests.""" |
| 105 |
self.create_players_and_query_qp(res_cls) |
| 106 |
# Post a receive WR to ensure old address is in use and will break. |
| 107 |
s_recv_wr = u.get_recv_wr(self.server) |
| 108 |
self.server.qp.post_recv(s_recv_wr) |
| 109 |
server_addr = posix_memalign(self.server.msg_size) |
| 110 |
try: |
| 111 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_TRANSLATION, |
| 112 |
addr=server_addr, |
| 113 |
length=self.server.msg_size) |
| 114 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Remote operation error'): |
| 115 |
# The server QP receive queue has WR with the old MR address, |
| 116 |
# therefore traffic should fail. |
| 117 |
u.traffic(**self.traffic_args) |
| 118 |
self.restate_qps() |
| 119 |
u.traffic(**self.traffic_args) |
| 120 |
finally: |
| 121 |
free(server_addr) |
| 122 |
|
| 123 |
def test_mr_rereg_addr(self): |
| 124 |
"""Test MR reregistration with different address""" |
| 125 |
self.validate_rereg_addr(MRRes) |
| 126 |
|
| 127 |
def test_mrex_rereg_addr(self): |
| 128 |
"""Test MREx reregistration with different address""" |
| 129 |
self.validate_rereg_addr(MRExRes) |
| 130 |
|
| 131 |
def test_mr_rereg_atomic(self): |
| 132 |
""" |
| 133 |
Test the rereg of MR's atomic access with the following flow: |
| 134 |
Create MRs with atomic access, then rereg the MRs without atomic |
| 135 |
access and verify that traffic fails with the relevant error. |
| 136 |
Rereg the MRs back to atomic access and verify that traffic now succeeds. |
| 137 |
""" |
| 138 |
atomic_mr_access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_ATOMIC |
| 139 |
self.create_players_and_query_qp(MRRes, mr_access=atomic_mr_access) |
| 140 |
access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 141 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_ACCESS, access=access) |
| 142 |
self.client.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_ACCESS, access=access) |
| 143 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Completion status is Remote access error'): |
| 144 |
u.atomic_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_ATOMIC_FETCH_AND_ADD) |
| 145 |
self.restate_qps() |
| 146 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_ACCESS, access=atomic_mr_access) |
| 147 |
self.client.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_ACCESS, access=atomic_mr_access) |
| 148 |
u.atomic_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_ATOMIC_FETCH_AND_ADD) |
| 149 |
|
| 150 |
def validate_rereg_access(self, res_cls): |
| 151 |
"""Common logic for reregistration of access flags and RDMA traffic.""" |
| 152 |
self.create_players(res_cls) |
| 153 |
access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_WRITE |
| 154 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_ACCESS, access=access) |
| 155 |
self.client.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_ACCESS, access=access) |
| 156 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 157 |
|
| 158 |
def test_mr_rereg_access(self): |
| 159 |
self.validate_rereg_access(MRRes) |
| 160 |
|
| 161 |
def test_mrex_rereg_access(self): |
| 162 |
self.validate_rereg_access(MRExRes) |
| 163 |
|
| 164 |
def test_mr_rereg_access_bad_flow(self): |
| 165 |
""" |
| 166 |
Test that cover rereg MR's access with this flow: |
| 167 |
Run remote traffic on MR with compatible access, then rereg the MR |
| 168 |
without remote access and verify that traffic fails with the relevant |
| 169 |
error. |
| 170 |
""" |
| 171 |
remote_access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_WRITE |
| 172 |
self.create_players(MRRes, mr_access=remote_access) |
| 173 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 174 |
access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 175 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_ACCESS, access=access) |
| 176 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Remote access error'): |
| 177 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 178 |
|
| 179 |
def test_mr_rereg_pd(self): |
| 180 |
""" |
| 181 |
Test that cover rereg MR's PD with this flow: |
| 182 |
Use MR with QP that was created with the same PD. Then rereg the MR's PD |
| 183 |
and use the MR with the same QP, expect the traffic to fail with "remote |
| 184 |
operation error". Restate the QP from ERR state, rereg the MR back |
| 185 |
to its previous PD and use it again with the QP, verify that it now |
| 186 |
succeeds. |
| 187 |
""" |
| 188 |
self.create_players(MRRes) |
| 189 |
self.server_qp_attr, _ = self.server.qp.query(0x1ffffff) |
| 190 |
self.client_qp_attr, _ = self.client.qp.query(0x1ffffff) |
| 191 |
u.traffic(**self.traffic_args) |
| 192 |
server_new_pd = PD(self.server.ctx) |
| 193 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_PD, pd=server_new_pd) |
| 194 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Remote operation error'): |
| 195 |
u.traffic(**self.traffic_args) |
| 196 |
self.restate_qps() |
| 197 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_PD, pd=self.server.pd) |
| 198 |
u.traffic(**self.traffic_args) |
| 199 |
self.server.rereg_mr(flags=ibv_rereg_mr_flags.IBV_REREG_MR_CHANGE_PD, pd=server_new_pd) |
| 200 |
|
| 201 |
def validate_bad_flags_registration(self, mr_cls, expected_regex): |
| 202 |
"""Validate that registering a given MR class with illegal flags fails.""" |
| 203 |
bad_flags = [ibv_access_flags.IBV_ACCESS_REMOTE_WRITE, |
| 204 |
ibv_access_flags.IBV_ACCESS_REMOTE_ATOMIC] |
| 205 |
with d.Context(name=self.dev_name) as ctx: |
| 206 |
with PD(ctx) as pd: |
| 207 |
for flag in bad_flags: |
| 208 |
with self.assertRaisesRegex(PyverbsRDMAError, expected_regex): |
| 209 |
mr_cls(pd, u.get_mr_length(), flag) |
| 210 |
|
| 211 |
def test_reg_mr_bad_flags(self): |
| 212 |
""" |
| 213 |
Verify that illegal flags combination fails as expected for classic MR |
| 214 |
""" |
| 215 |
self.validate_bad_flags_registration(MR, 'Failed to register a MR') |
| 216 |
|
| 217 |
def test_reg_mrex_bad_flags(self): |
| 218 |
""" |
| 219 |
Verify that illegal flags combination fails as expected for MREx |
| 220 |
""" |
| 221 |
self.validate_bad_flags_registration(MREx, 'Failed to register MR using extended API') |
| 222 |
|
| 223 |
def test_reg_mrex_inval_params(self): |
| 224 |
"""Verify MREx registration fails when ADDR & FD flags set.""" |
| 225 |
with d.Context(name=self.dev_name) as ctx: |
| 226 |
with PD(ctx) as pd: |
| 227 |
with self.assertRaises(PyverbsRDMAError): |
| 228 |
MREx(pd, u.get_mr_length(), ibv_access_flags.IBV_ACCESS_LOCAL_WRITE, |
| 229 |
implicit=True, fd=0) |
| 230 |
|
| 231 |
|
| 232 |
class MWRC(RCResources): |
| 233 |
def __init__(self, dev_name, ib_port, gid_index, mw_type): |
| 234 |
""" |
| 235 |
Initialize Memory Window resources based on RC resources that include RC |
| 236 |
QP. |
| 237 |
:param dev_name: Device name to be used |
| 238 |
:param ib_port: IB port of the device to use |
| 239 |
:param gid_index: Which GID index to use |
| 240 |
:param mw_type: The MW type to use |
| 241 |
""" |
| 242 |
super().__init__(dev_name=dev_name, ib_port=ib_port, |
| 243 |
gid_index=gid_index) |
| 244 |
self.mw_type = mw_type |
| 245 |
access = ibv_access_flags.IBV_ACCESS_REMOTE_WRITE | ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 246 |
self.mw_bind_info = MWBindInfo(self.mr, self.mr.buf, self.msg_size, |
| 247 |
access) |
| 248 |
self.mw_bind = MWBind(self.mw_bind_info, ibv_send_flags.IBV_SEND_SIGNALED) |
| 249 |
try: |
| 250 |
self.mw = MW(self.pd, self.mw_type) |
| 251 |
except PyverbsRDMAError as ex: |
| 252 |
if ex.error_code == errno.EOPNOTSUPP: |
| 253 |
raise unittest.SkipTest('Create MW is not supported') |
| 254 |
raise ex |
| 255 |
|
| 256 |
def create_mr(self): |
| 257 |
access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_MW_BIND |
| 258 |
try: |
| 259 |
self.mr = MR(self.pd, self.msg_size, access) |
| 260 |
except PyverbsRDMAError as ex: |
| 261 |
if ex.error_code == errno.EOPNOTSUPP: |
| 262 |
raise unittest.SkipTest('Reg MR with MW access is not supported') |
| 263 |
raise ex |
| 264 |
|
| 265 |
def create_qp_attr(self): |
| 266 |
qp_attr = QPAttr(port_num=self.ib_port) |
| 267 |
qp_access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_WRITE |
| 268 |
qp_attr.qp_access_flags = qp_access |
| 269 |
return qp_attr |
| 270 |
|
| 271 |
|
| 272 |
class MWExRC(MWRC): |
| 273 |
"""Resource class that uses MREx instead of MR for MW tests.""" |
| 274 |
|
| 275 |
@u.skip_unsupported |
| 276 |
def create_mr(self): |
| 277 |
# Register an MREx that can be bound to a memory window |
| 278 |
access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_MW_BIND |
| 279 |
self.mr = MREx(self.pd, self.msg_size, access) |
| 280 |
|
| 281 |
|
| 282 |
class MWTest(RDMATestCase): |
| 283 |
""" |
| 284 |
Test various functionalities of the MW class. |
| 285 |
""" |
| 286 |
def setUp(self): |
| 287 |
super().setUp() |
| 288 |
self.iters = 10 |
| 289 |
self.server = None |
| 290 |
self.client = None |
| 291 |
|
| 292 |
def tearDown(self): |
| 293 |
if self.server: |
| 294 |
self.server.mw.close() |
| 295 |
if self.client: |
| 296 |
self.client.mw.close() |
| 297 |
return super().tearDown() |
| 298 |
|
| 299 |
def bind_mw_type_1(self): |
| 300 |
self.server.qp.bind_mw(self.server.mw, self.server.mw_bind) |
| 301 |
self.client.qp.bind_mw(self.client.mw, self.client.mw_bind) |
| 302 |
# Poll the bind MW action completion. |
| 303 |
u.poll_cq(self.server.cq) |
| 304 |
u.poll_cq(self.client.cq) |
| 305 |
self.server.rkey = self.client.mw.rkey |
| 306 |
self.server.raddr = self.client.mr.buf |
| 307 |
self.client.rkey = self.server.mw.rkey |
| 308 |
self.client.raddr = self.server.mr.buf |
| 309 |
|
| 310 |
def bind_mw_type_2(self): |
| 311 |
client_send_wr = SendWR(opcode=ibv_wr_opcode.IBV_WR_BIND_MW) |
| 312 |
client_send_wr.set_bind_wr(self.client.mw, self.client.mw_bind_info) |
| 313 |
server_send_wr = SendWR(opcode=ibv_wr_opcode.IBV_WR_BIND_MW) |
| 314 |
server_send_wr.set_bind_wr(self.server.mw, self.server.mw_bind_info) |
| 315 |
self.server.qp.post_send(server_send_wr) |
| 316 |
self.client.qp.post_send(client_send_wr) |
| 317 |
# Poll the bind MW WR. |
| 318 |
u.poll_cq(self.server.cq) |
| 319 |
u.poll_cq(self.client.cq) |
| 320 |
self.server.rkey = client_send_wr.rkey |
| 321 |
self.server.raddr = self.client.mr.buf |
| 322 |
self.client.rkey = server_send_wr.rkey |
| 323 |
self.client.raddr = self.server.mr.buf |
| 324 |
|
| 325 |
def invalidate_mw_type1(self): |
| 326 |
""" |
| 327 |
Invalidate the MWs by rebind this MW with zero length. |
| 328 |
:return: None |
| 329 |
""" |
| 330 |
for player in [self.server, self.client]: |
| 331 |
mw_bind_info = MWBindInfo(player.mr, player.mr.buf, 0, 0) |
| 332 |
mw_bind = MWBind(mw_bind_info, ibv_send_flags.IBV_SEND_SIGNALED) |
| 333 |
player.qp.bind_mw(player.mw, mw_bind) |
| 334 |
# Poll the bound MW action request completion. |
| 335 |
u.poll_cq(player.cq) |
| 336 |
|
| 337 |
def invalidate_mw_type2_local(self): |
| 338 |
""" |
| 339 |
Invalidate the MWs by post invalidation send WR from the local QP. |
| 340 |
:return: None |
| 341 |
""" |
| 342 |
inv_send_wr = SendWR(opcode=ibv_wr_opcode.IBV_WR_LOCAL_INV) |
| 343 |
inv_send_wr.imm_data = self.server.rkey |
| 344 |
self.client.qp.post_send(inv_send_wr) |
| 345 |
inv_send_wr = SendWR(opcode=ibv_wr_opcode.IBV_WR_LOCAL_INV) |
| 346 |
inv_send_wr.imm_data = self.client.rkey |
| 347 |
self.server.qp.post_send(inv_send_wr) |
| 348 |
# Poll the invalidate MW WR. |
| 349 |
u.poll_cq(self.server.cq) |
| 350 |
u.poll_cq(self.client.cq) |
| 351 |
|
| 352 |
def invalidate_mw_type2_remote(self): |
| 353 |
""" |
| 354 |
Invalidate the MWs by sending invalidation send WR from the remote QP. |
| 355 |
:return: None |
| 356 |
""" |
| 357 |
server_recv_wr = u.get_recv_wr(self.server) |
| 358 |
client_recv_wr = u.get_recv_wr(self.client) |
| 359 |
self.server.qp.post_recv(server_recv_wr) |
| 360 |
self.client.qp.post_recv(client_recv_wr) |
| 361 |
inv_send_wr = SendWR(opcode=ibv_wr_opcode.IBV_WR_SEND_WITH_INV) |
| 362 |
inv_send_wr.imm_data = self.client.rkey |
| 363 |
self.client.qp.post_send(inv_send_wr) |
| 364 |
inv_send_wr = SendWR(opcode=ibv_wr_opcode.IBV_WR_SEND_WITH_INV) |
| 365 |
inv_send_wr.imm_data = self.server.rkey |
| 366 |
self.server.qp.post_send(inv_send_wr) |
| 367 |
# Poll the invalidate MW send WR. |
| 368 |
u.poll_cq(self.server.cq) |
| 369 |
u.poll_cq(self.client.cq) |
| 370 |
# Poll the invalidate MW recv WR. |
| 371 |
u.poll_cq(self.server.cq) |
| 372 |
u.poll_cq(self.client.cq) |
| 373 |
|
| 374 |
def test_mw_type1(self): |
| 375 |
self.create_players(MWRC, mw_type=ibv_mw_type.IBV_MW_TYPE_1) |
| 376 |
self.bind_mw_type_1() |
| 377 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 378 |
|
| 379 |
def test_mw_ex_type1(self): |
| 380 |
""" |
| 381 |
Test Memory Window type 1 with MREx |
| 382 |
""" |
| 383 |
self.create_players(MWExRC, mw_type=ibv_mw_type.IBV_MW_TYPE_1) |
| 384 |
self.bind_mw_type_1() |
| 385 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 386 |
|
| 387 |
def test_invalidate_mw_type1(self): |
| 388 |
self.test_mw_type1() |
| 389 |
self.invalidate_mw_type1() |
| 390 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Remote access error'): |
| 391 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 392 |
|
| 393 |
def test_mw_type2(self): |
| 394 |
self.create_players(MWRC, mw_type=ibv_mw_type.IBV_MW_TYPE_2) |
| 395 |
self.bind_mw_type_2() |
| 396 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 397 |
|
| 398 |
def test_mw_type2_invalidate_local(self): |
| 399 |
self.test_mw_type2() |
| 400 |
self.invalidate_mw_type2_local() |
| 401 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Remote access error'): |
| 402 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 403 |
|
| 404 |
def test_mw_type2_invalidate_remote(self): |
| 405 |
self.test_mw_type2() |
| 406 |
self.invalidate_mw_type2_remote() |
| 407 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Remote access error'): |
| 408 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 409 |
|
| 410 |
def test_mw_type2_invalidate_dealloc(self): |
| 411 |
self.test_mw_type2() |
| 412 |
# Dealloc the MW by closing the pyverbs objects. |
| 413 |
self.server.mw.close() |
| 414 |
self.client.mw.close() |
| 415 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Remote access error'): |
| 416 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 417 |
|
| 418 |
def test_reg_mw_wrong_type(self): |
| 419 |
""" |
| 420 |
Verify that trying to create a MW of a wrong type fails |
| 421 |
""" |
| 422 |
with d.Context(name=self.dev_name) as ctx: |
| 423 |
with PD(ctx) as pd: |
| 424 |
try: |
| 425 |
mw_type = 3 |
| 426 |
MW(pd, mw_type) |
| 427 |
except PyverbsRDMAError as ex: |
| 428 |
if ex.error_code == errno.EOPNOTSUPP: |
| 429 |
raise unittest.SkipTest('Create memory window of type {} is not supported'.format(mw_type)) |
| 430 |
else: |
| 431 |
raise PyverbsError('Created a MW with type {t}'.\ |
| 432 |
format(t=mw_type)) |
| 433 |
|
| 434 |
|
| 435 |
class DeviceMemoryAPITest(PyverbsAPITestCase): |
| 436 |
""" |
| 437 |
Test various API usages of the DMMR class. |
| 438 |
""" |
| 439 |
def setUp(self): |
| 440 |
super().setUp() |
| 441 |
if self.attr_ex.max_dm_size == 0: |
| 442 |
raise unittest.SkipTest('Device memory is not supported') |
| 443 |
|
| 444 |
def test_create_dm_mr(self): |
| 445 |
max_dm_size = self.attr_ex.max_dm_size |
| 446 |
dm_access = ibv_access_flags.IBV_ACCESS_ZERO_BASED | ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 447 |
for dm_size in [4, max_dm_size/4, max_dm_size/2]: |
| 448 |
dm_size = dm_size - (dm_size % u.DM_ALIGNMENT) |
| 449 |
for dmmr_factor_size in [0.1, 0.5, 1]: |
| 450 |
dmmr_size = dm_size * dmmr_factor_size |
| 451 |
dmmr_size = dmmr_size - (dmmr_size % u.DM_ALIGNMENT) |
| 452 |
with d.DM(self.ctx, d.AllocDmAttr(length=dm_size)) as dm: |
| 453 |
DMMR(PD(self.ctx), dmmr_size, dm_access, dm, 0) |
| 454 |
|
| 455 |
def test_dm_bad_access(self): |
| 456 |
""" |
| 457 |
Test multiple types of bad access to the Device Memory. Device memory |
| 458 |
access requests a 4B alignment. The test tries to access the DM |
| 459 |
with bad alignment or outside of the allocated memory. |
| 460 |
""" |
| 461 |
dm_size = 100 |
| 462 |
with d.DM(self.ctx, d.AllocDmAttr(length=dm_size)) as dm: |
| 463 |
dm_access = ibv_access_flags.IBV_ACCESS_ZERO_BASED | ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 464 |
dmmr = DMMR(PD(self.ctx), dm_size, dm_access, dm, 0) |
| 465 |
access_cases = [(DM_INVALID_ALIGNMENT, 4), # Valid length with unaligned offset |
| 466 |
(4, DM_INVALID_ALIGNMENT), # Valid offset with unaligned length |
| 467 |
(dm_size + 4, 4), # Offset out of allocated memory |
| 468 |
(0, dm_size + 4)] # Length out of allocated memory |
| 469 |
for case in access_cases: |
| 470 |
offset, length = case |
| 471 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Failed to copy from dm'): |
| 472 |
dmmr.read(offset=offset, length=length) |
| 473 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Failed to copy to dm'): |
| 474 |
dmmr.write(data='s'*length, offset=offset, length=length) |
| 475 |
|
| 476 |
def test_dm_bad_registration(self): |
| 477 |
""" |
| 478 |
Test bad Device Memory registration when trying to register bigger DMMR |
| 479 |
than the allocated DM. |
| 480 |
""" |
| 481 |
dm_size = 100 |
| 482 |
with d.DM(self.ctx, d.AllocDmAttr(length=dm_size)) as dm: |
| 483 |
dm_access = ibv_access_flags.IBV_ACCESS_ZERO_BASED | ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 484 |
with self.assertRaisesRegex(PyverbsRDMAError, 'Failed to register a device MR'): |
| 485 |
DMMR(PD(self.ctx), dm_size + 4, dm_access, dm, 0) |
| 486 |
|
| 487 |
def test_dm_export_dmabuf_fd(self): |
| 488 |
"""Test exporting a DM as a dmabuf FD""" |
| 489 |
dm_size = 100 |
| 490 |
try: |
| 491 |
with d.DM(self.ctx, d.AllocDmAttr(length=dm_size)) as dm: |
| 492 |
dmabuf_fd = dm.export_dmabuf_fd() |
| 493 |
self.assertGreater(dmabuf_fd, 0, 'Expected a valid dmabuf FD greater than 0') |
| 494 |
except PyverbsRDMAError as ex: |
| 495 |
if ex.error_code in [errno.EOPNOTSUPP, errno.EPROTONOSUPPORT]: |
| 496 |
raise unittest.SkipTest('DM dmabuf export is not supported') |
| 497 |
raise ex |
| 498 |
|
| 499 |
|
| 500 |
def check_dmabuf_support(gpu=0): |
| 501 |
""" |
| 502 |
Check if dma-buf allocation is supported by the system. |
| 503 |
Skip the test on failure. |
| 504 |
""" |
| 505 |
device_num = 128 + gpu |
| 506 |
try: |
| 507 |
DmaBuf(1, gpu=gpu) |
| 508 |
except PyverbsRDMAError as ex: |
| 509 |
if ex.error_code == errno.ENOENT: |
| 510 |
raise unittest.SkipTest(f'Device /dev/dri/renderD{device_num} is not present') |
| 511 |
if ex.error_code == errno.EACCES: |
| 512 |
raise unittest.SkipTest(f'Lack of permission to access /dev/dri/renderD{device_num}') |
| 513 |
if ex.error_code == errno.EOPNOTSUPP: |
| 514 |
raise unittest.SkipTest(f'Allocating dmabuf is not supported by /dev/dri/renderD{device_num}') |
| 515 |
|
| 516 |
|
| 517 |
def check_dmabuf_mr_support(pd, gpu=0): |
| 518 |
""" |
| 519 |
Check if dma-buf MR registration is supported by the driver. |
| 520 |
Skip the test on failure |
| 521 |
""" |
| 522 |
try: |
| 523 |
DmaBufMR(pd, 1, 0, gpu=gpu) |
| 524 |
except PyverbsRDMAError as ex: |
| 525 |
if ex.error_code == errno.EOPNOTSUPP: |
| 526 |
raise unittest.SkipTest('Reg dma-buf MR is not supported by the RDMA driver') |
| 527 |
|
| 528 |
|
| 529 |
class DmaBufMRTest(PyverbsAPITestCase): |
| 530 |
""" |
| 531 |
Test various functionalities of the DmaBufMR class. |
| 532 |
""" |
| 533 |
def setUp(self): |
| 534 |
super().setUp() |
| 535 |
self.gpu = self.config['gpu'] |
| 536 |
self.gtt = self.config['gtt'] |
| 537 |
|
| 538 |
def test_dmabuf_reg_mr(self): |
| 539 |
""" |
| 540 |
Test ibv_reg_dmabuf_mr() |
| 541 |
""" |
| 542 |
check_dmabuf_support(self.gpu) |
| 543 |
with PD(self.ctx) as pd: |
| 544 |
check_dmabuf_mr_support(pd, self.gpu) |
| 545 |
flags = u.get_dmabuf_access_flags(self.ctx) |
| 546 |
for f in flags: |
| 547 |
len = u.get_mr_length() |
| 548 |
for off in [0, len//2]: |
| 549 |
with DmaBufMR(pd, len, f, offset=off, gpu=self.gpu, |
| 550 |
gtt=self.gtt) as mr: |
| 551 |
pass |
| 552 |
|
| 553 |
def test_dmabuf_dereg_mr(self): |
| 554 |
""" |
| 555 |
Test ibv_dereg_mr() with DmaBufMR |
| 556 |
""" |
| 557 |
check_dmabuf_support(self.gpu) |
| 558 |
with PD(self.ctx) as pd: |
| 559 |
check_dmabuf_mr_support(pd, self.gpu) |
| 560 |
flags = u.get_dmabuf_access_flags(self.ctx) |
| 561 |
for f in flags: |
| 562 |
len = u.get_mr_length() |
| 563 |
for off in [0, len//2]: |
| 564 |
with DmaBufMR(pd, len, f, offset=off, gpu=self.gpu, |
| 565 |
gtt=self.gtt) as mr: |
| 566 |
mr.close() |
| 567 |
|
| 568 |
def test_dmabuf_dereg_mr_twice(self): |
| 569 |
""" |
| 570 |
Verify that explicit call to DmaBufMR's close() doesn't fail |
| 571 |
""" |
| 572 |
check_dmabuf_support(self.gpu) |
| 573 |
with PD(self.ctx) as pd: |
| 574 |
check_dmabuf_mr_support(pd, self.gpu) |
| 575 |
flags = u.get_dmabuf_access_flags(self.ctx) |
| 576 |
for f in flags: |
| 577 |
len = u.get_mr_length() |
| 578 |
for off in [0, len//2]: |
| 579 |
with DmaBufMR(pd, len, f, offset=off, gpu=self.gpu, |
| 580 |
gtt=self.gtt) as mr: |
| 581 |
# Pyverbs supports multiple destruction of objects, |
| 582 |
# we are not expecting an exception here. |
| 583 |
mr.close() |
| 584 |
mr.close() |
| 585 |
|
| 586 |
def test_dmabuf_reg_mr_bad_flags(self): |
| 587 |
""" |
| 588 |
Verify that DmaBufMR with illegal flags combination fails as expected |
| 589 |
""" |
| 590 |
check_dmabuf_support(self.gpu) |
| 591 |
with PD(self.ctx) as pd: |
| 592 |
check_dmabuf_mr_support(pd, self.gpu) |
| 593 |
for i in range(5): |
| 594 |
flags = random.sample([ibv_access_flags.IBV_ACCESS_REMOTE_WRITE, |
| 595 |
ibv_access_flags.IBV_ACCESS_REMOTE_ATOMIC], |
| 596 |
random.randint(1, 2)) |
| 597 |
mr_flags = 0 |
| 598 |
for i in flags: |
| 599 |
mr_flags += i.value |
| 600 |
try: |
| 601 |
DmaBufMR(pd, u.get_mr_length(), mr_flags, |
| 602 |
gpu=self.gpu, gtt=self.gtt) |
| 603 |
except PyverbsRDMAError as err: |
| 604 |
assert 'Failed to register a dma-buf MR' in err.args[0] |
| 605 |
else: |
| 606 |
raise PyverbsRDMAError('Registered a dma-buf MR with illegal falgs') |
| 607 |
|
| 608 |
def test_dmabuf_write(self): |
| 609 |
""" |
| 610 |
Test writing to DmaBufMR's buffer |
| 611 |
""" |
| 612 |
check_dmabuf_support(self.gpu) |
| 613 |
with PD(self.ctx) as pd: |
| 614 |
check_dmabuf_mr_support(pd, self.gpu) |
| 615 |
for i in range(10): |
| 616 |
mr_len = u.get_mr_length() |
| 617 |
flags = u.get_dmabuf_access_flags(self.ctx) |
| 618 |
for f in flags: |
| 619 |
for mr_off in [0, mr_len//2]: |
| 620 |
with DmaBufMR(pd, mr_len, f, offset=mr_off, |
| 621 |
gpu=self.gpu, gtt=self.gtt) as mr: |
| 622 |
write_len = min(random.randint(1, MAX_IO_LEN), |
| 623 |
mr_len) |
| 624 |
mr.write('a' * write_len, write_len) |
| 625 |
|
| 626 |
def test_dmabuf_read(self): |
| 627 |
""" |
| 628 |
Test reading from DmaBufMR's buffer |
| 629 |
""" |
| 630 |
check_dmabuf_support(self.gpu) |
| 631 |
with PD(self.ctx) as pd: |
| 632 |
check_dmabuf_mr_support(pd, self.gpu) |
| 633 |
for i in range(10): |
| 634 |
mr_len = u.get_mr_length() |
| 635 |
flags = u.get_dmabuf_access_flags(self.ctx) |
| 636 |
for f in flags: |
| 637 |
for mr_off in [0, mr_len//2]: |
| 638 |
with DmaBufMR(pd, mr_len, f, offset=mr_off, |
| 639 |
gpu=self.gpu, gtt=self.gtt) as mr: |
| 640 |
write_len = min(random.randint(1, MAX_IO_LEN), |
| 641 |
mr_len) |
| 642 |
write_str = 'a' * write_len |
| 643 |
mr.write(write_str, write_len) |
| 644 |
read_len = random.randint(1, write_len) |
| 645 |
offset = random.randint(0, write_len-read_len) |
| 646 |
read_str = mr.read(read_len, offset).decode() |
| 647 |
assert read_str in write_str |
| 648 |
|
| 649 |
def test_dmabuf_lkey(self): |
| 650 |
""" |
| 651 |
Test reading lkey property |
| 652 |
""" |
| 653 |
check_dmabuf_support(self.gpu) |
| 654 |
with PD(self.ctx) as pd: |
| 655 |
check_dmabuf_mr_support(pd, self.gpu) |
| 656 |
length = u.get_mr_length() |
| 657 |
flags = u.get_dmabuf_access_flags(self.ctx) |
| 658 |
for f in flags: |
| 659 |
with DmaBufMR(pd, length, f, gpu=self.gpu, |
| 660 |
gtt=self.gtt) as mr: |
| 661 |
mr.lkey |
| 662 |
|
| 663 |
def test_dmabuf_rkey(self): |
| 664 |
""" |
| 665 |
Test reading rkey property |
| 666 |
""" |
| 667 |
check_dmabuf_support(self.gpu) |
| 668 |
with PD(self.ctx) as pd: |
| 669 |
check_dmabuf_mr_support(pd, self.gpu) |
| 670 |
length = u.get_mr_length() |
| 671 |
flags = u.get_dmabuf_access_flags(self.ctx) |
| 672 |
for f in flags: |
| 673 |
with DmaBufMR(pd, length, f, gpu=self.gpu, |
| 674 |
gtt=self.gtt) as mr: |
| 675 |
mr.rkey |
| 676 |
|
| 677 |
|
| 678 |
class DmaBufRC(RCResources): |
| 679 |
def __init__(self, dev_name, ib_port, gid_index, gpu, gtt): |
| 680 |
""" |
| 681 |
Initialize an DmaBufRC object. |
| 682 |
:param dev_name: Device name to be used |
| 683 |
:param ib_port: IB port of the device to use |
| 684 |
:param gid_index: Which GID index to use |
| 685 |
:param gpu: GPU unit to allocate dmabuf from |
| 686 |
:gtt: Allocate dmabuf from GTT instead og VRAM |
| 687 |
""" |
| 688 |
self.gpu = gpu |
| 689 |
self.gtt = gtt |
| 690 |
super(DmaBufRC, self).__init__(dev_name=dev_name, ib_port=ib_port, |
| 691 |
gid_index=gid_index) |
| 692 |
|
| 693 |
def create_mr(self): |
| 694 |
check_dmabuf_support(self.gpu) |
| 695 |
check_dmabuf_mr_support(self.pd, self.gpu) |
| 696 |
access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_WRITE |
| 697 |
mr = DmaBufMR(self.pd, self.msg_size, access, gpu=self.gpu, |
| 698 |
gtt=self.gtt) |
| 699 |
self.mr = mr |
| 700 |
|
| 701 |
def create_qp_attr(self): |
| 702 |
qp_attr = QPAttr(port_num=self.ib_port) |
| 703 |
qp_access = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_WRITE |
| 704 |
qp_attr.qp_access_flags = qp_access |
| 705 |
return qp_attr |
| 706 |
|
| 707 |
|
| 708 |
class DmaBufTestCase(RDMATestCase): |
| 709 |
def setUp(self): |
| 710 |
super(DmaBufTestCase, self).setUp() |
| 711 |
self.iters = 100 |
| 712 |
self.gpu = self.config['gpu'] |
| 713 |
self.gtt = self.config['gtt'] |
| 714 |
|
| 715 |
def test_dmabuf_rc_traffic(self): |
| 716 |
""" |
| 717 |
Test send/recv using dma-buf MR over RC |
| 718 |
""" |
| 719 |
self.create_players(DmaBufRC, gpu=self.gpu, gtt=self.gtt) |
| 720 |
u.traffic(**self.traffic_args) |
| 721 |
|
| 722 |
def test_dmabuf_rdma_traffic(self): |
| 723 |
""" |
| 724 |
Test rdma write using dma-buf MR |
| 725 |
""" |
| 726 |
self.create_players(DmaBufRC, gpu=self.gpu, gtt=self.gtt) |
| 727 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 728 |
|
| 729 |
|
| 730 |
class DeviceMemoryRes(RCResources): |
| 731 |
def __init__(self, dev_name, ib_port, gid_index, remote_access=False, msg_size=1024): |
| 732 |
""" |
| 733 |
Initialize DM resources based on RC resources that include RC |
| 734 |
QP. |
| 735 |
:param dev_name: Device name to be used. |
| 736 |
:param ib_port: IB port of the device to use. |
| 737 |
:param gid_index: Which GID index to use. |
| 738 |
:param remote_access: If True, enable remote access. |
| 739 |
:param msg_size: Message size (default: 1024). |
| 740 |
""" |
| 741 |
self.remote_access = remote_access |
| 742 |
super().__init__(dev_name=dev_name, ib_port=ib_port, |
| 743 |
gid_index=gid_index, msg_size=msg_size) |
| 744 |
|
| 745 |
def create_mr(self): |
| 746 |
try: |
| 747 |
self.dm = d.DM(self.ctx, d.AllocDmAttr(length=self.msg_size)) |
| 748 |
access = ibv_access_flags.IBV_ACCESS_ZERO_BASED | ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 749 |
if self.remote_access: |
| 750 |
access |= ibv_access_flags.IBV_ACCESS_REMOTE_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_READ | \ |
| 751 |
ibv_access_flags.IBV_ACCESS_REMOTE_ATOMIC |
| 752 |
self.mr = DMMR(self.pd, self.msg_size, access, self.dm, 0) |
| 753 |
except PyverbsRDMAError as ex: |
| 754 |
if ex.error_code == errno.EOPNOTSUPP: |
| 755 |
raise unittest.SkipTest(f'Reg DMMR with access={access} is not supported') |
| 756 |
raise ex |
| 757 |
|
| 758 |
def create_qp_attr(self): |
| 759 |
qp_attr = QPAttr(port_num=self.ib_port) |
| 760 |
qp_attr.qp_access_flags = ibv_access_flags.IBV_ACCESS_LOCAL_WRITE |
| 761 |
if self.remote_access: |
| 762 |
qp_attr.qp_access_flags |= ibv_access_flags.IBV_ACCESS_REMOTE_WRITE | ibv_access_flags.IBV_ACCESS_REMOTE_READ | \ |
| 763 |
ibv_access_flags.IBV_ACCESS_REMOTE_ATOMIC |
| 764 |
return qp_attr |
| 765 |
|
| 766 |
|
| 767 |
class DeviceMemoryTest(RDMATestCase): |
| 768 |
""" |
| 769 |
Test various functionalities of the DM class. |
| 770 |
""" |
| 771 |
def setUp(self): |
| 772 |
super().setUp() |
| 773 |
self.iters = 10 |
| 774 |
self.server = None |
| 775 |
self.client = None |
| 776 |
self.traffic_args = None |
| 777 |
ctx = d.Context(name=self.dev_name) |
| 778 |
if ctx.query_device_ex().max_dm_size == 0: |
| 779 |
raise unittest.SkipTest('Device memory is not supported') |
| 780 |
# Device memory can not work in scatter to cqe mode in MLX5 devices, |
| 781 |
# therefore disable it and restore the default value at the end of the |
| 782 |
# test. |
| 783 |
self.set_env_variable('MLX5_SCATTER_TO_CQE', '0') |
| 784 |
|
| 785 |
def test_dm_traffic(self): |
| 786 |
self.create_players(DeviceMemoryRes) |
| 787 |
u.traffic(**self.traffic_args) |
| 788 |
|
| 789 |
def test_dm_remote_traffic(self): |
| 790 |
self.create_players(DeviceMemoryRes, remote_access=True) |
| 791 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE) |
| 792 |
|
| 793 |
def test_dm_remote_write_traffic_imm(self): |
| 794 |
self.create_players(DeviceMemoryRes, remote_access=True) |
| 795 |
u.traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_WRITE_WITH_IMM) |
| 796 |
|
| 797 |
def test_dm_remote_read_traffic(self): |
| 798 |
self.create_players(DeviceMemoryRes, remote_access=True) |
| 799 |
u.rdma_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_RDMA_READ) |
| 800 |
|
| 801 |
def test_dm_atomic_fetch_add(self): |
| 802 |
self.create_players(DeviceMemoryRes, remote_access=True, msg_size=8) |
| 803 |
u.atomic_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_ATOMIC_FETCH_AND_ADD) |
| 804 |
|
| 805 |
def test_dm_atomic_cmp_swp(self): |
| 806 |
self.create_players(DeviceMemoryRes, remote_access=True, msg_size=8) |
| 807 |
u.atomic_traffic(**self.traffic_args, send_op=ibv_wr_opcode.IBV_WR_ATOMIC_CMP_AND_SWP) |