From patchwork Sun Feb 24 13:06:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noa Osherovich X-Patchwork-Id: 10827953 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 081071575 for ; Sun, 24 Feb 2019 13:09:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E821F296ED for ; Sun, 24 Feb 2019 13:09:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DA8372AA01; Sun, 24 Feb 2019 13:09:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2C623296ED for ; Sun, 24 Feb 2019 13:09:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728318AbfBXNI5 (ORCPT ); Sun, 24 Feb 2019 08:08:57 -0500 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:50556 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728307AbfBXNI4 (ORCPT ); Sun, 24 Feb 2019 08:08:56 -0500 Received: from Internal Mail-Server by MTLPINE1 (envelope-from noaos@mellanox.com) with ESMTPS (AES256-SHA encrypted); 24 Feb 2019 15:06:49 +0200 Received: from reg-l-vrt-059-009.mtl.labs.mlnx (reg-l-vrt-059-009.mtl.labs.mlnx [10.135.59.9]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x1OD6mfg026210; Sun, 24 Feb 2019 15:06:49 +0200 From: Noa Osherovich To: leonro@mellanox.com, jgg@mellanox.com, dledford@redhat.com Cc: linux-rdma@vger.kernel.org, Noa Osherovich Subject: [PATCH rdma-core 12/19] pyverbs: Add direct memory related unittests Date: Sun, 24 Feb 2019 15:06:31 +0200 Message-Id: <20190224130638.31848-13-noaos@mellanox.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190224130638.31848-1-noaos@mellanox.com> References: <20190224130638.31848-1-noaos@mellanox.com> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add tests for direct memory creation as well as DMMR. Signed-off-by: Noa Osherovich --- pyverbs/tests/device.py | 147 +++++++++++++++++++++++++++++++++++++++- pyverbs/tests/mr.py | 48 ++++++++++++- pyverbs/tests/utils.py | 16 +++++ 3 files changed, 209 insertions(+), 2 deletions(-) diff --git a/pyverbs/tests/device.py b/pyverbs/tests/device.py index bdfa81f0dc06..4a5e0a794069 100644 --- a/pyverbs/tests/device.py +++ b/pyverbs/tests/device.py @@ -2,10 +2,12 @@ # Copyright (c) 2018, Mellanox Technologies. All rights reserved. See COPYING file import unittest import resource +import random +from pyverbs.pyverbs_error import PyverbsError, PyverbsRDMAError +import pyverbs.tests.utils as u import pyverbs.device as d - PAGE_SIZE = resource.getpagesize() @@ -73,3 +75,146 @@ class device_test(unittest.TestCase): with d.Context(name=dev.name.decode()) as ctx: attr_ex = ctx.query_device_ex() self.verify_device_attr(attr_ex.orig_attr) + + +class dm_test(unittest.TestCase): + """ + Test various functionalities of the DM class. + """ + def test_create_dm(self): + """ + test ibv_alloc_dm() + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, + u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + with d.DM(ctx, dm_attrs): + pass + + def test_destroy_dm(self): + """ + test ibv_free_dm() + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, + u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + dm = d.DM(ctx, dm_attrs) + dm.close() + + def test_create_dm_bad_flow(self): + """ + test ibv_alloc_dm() with an illegal size and comp mask + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + dm_len = attr.max_dm_size + 1 + dm_attrs = u.get_dm_attrs(dm_len) + try: + dm = d.DM(ctx, dm_attrs) + except PyverbsRDMAError as e: + assert 'Failed to allocate device memory of size' in e.args[0] + assert 'Max available size' in e.args[0] + else: + raise PyverbsError('Created a DM with size larger than max reported') + dm_attrs.comp_mask = random.randint(1, 100) + try: + dm = d.DM(ctx, dm_attrs) + except PyverbsRDMAError as e: + assert 'Failed to allocate device memory of size' in e.args[0] + else: + raise PyverbsError('Created a DM with illegal comp mask {c}'.\ + format(c=dm_attrs.comp_mask)) + + def test_destroy_dm_bad_flow(self): + """ + test calling ibv_free_dm() twice + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + dm = d.DM(ctx, dm_attrs) + dm.close() + dm.close() + + def test_dm_write(self): + """ + Test writing to the device memory + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + with d.DM(ctx, dm_attrs) as dm: + data_length = random.randrange(4, dm_len, u.DM_ALIGNMENT) + data_offset = random.randrange(0, dm_len - data_length, u.DM_ALIGNMENT) + data = u.get_data(data_length) + dm.copy_to_dm(data_offset, data.encode(), data_length) + + def test_dm_write_bad_flow(self): + """ + Test writing to the device memory with bad offset and length + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + with d.DM(ctx, dm_attrs) as dm: + data_length = random.randrange(4, dm_len, u.DM_ALIGNMENT) + data_offset = random.randrange(0, dm_len - data_length, u.DM_ALIGNMENT) + data_offset += 1 # offset needs to be a multiple of 4 + data = u.get_data(data_length) + try: + dm.copy_to_dm(data_offset, data.encode(), data_length) + except PyverbsRDMAError as e: + assert 'Failed to copy to dm' in e.args[0] + else: + raise PyverbsError('Wrote to device memory with a bad offset') + + def test_dm_read(self): + """ + Test reading from the device memory + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + with d.DM(ctx, dm_attrs) as dm: + data_length = random.randrange(4, dm_len, u.DM_ALIGNMENT) + data_offset = random.randrange(0, dm_len - data_length, u.DM_ALIGNMENT) + data = u.get_data(data_length) + dm.copy_to_dm(data_offset, data.encode(), data_length) + read_str = dm.copy_from_dm(data_offset, data_length) + assert read_str.decode() == data diff --git a/pyverbs/tests/mr.py b/pyverbs/tests/mr.py index c97c6ac38d6d..229da54240cd 100644 --- a/pyverbs/tests/mr.py +++ b/pyverbs/tests/mr.py @@ -5,8 +5,8 @@ import random from pyverbs.pyverbs_error import PyverbsRDMAError, PyverbsError from pyverbs.base import PyverbsRDMAErrno +from pyverbs.mr import MR, MW, DMMR import pyverbs.tests.utils as u -from pyverbs.mr import MR, MW import pyverbs.device as d from pyverbs.pd import PD import pyverbs.enums as e @@ -181,3 +181,49 @@ class mw_test(unittest.TestCase): else: raise PyverbsError('Created a MW with type {t}'.\ format(t=mw_type)) + + +class dm_mr_test(unittest.TestCase): + """ + Test various functionalities of the DMMR class. + """ + def test_create_dm_mr(self): + """ + Test ibv_reg_dm_mr + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + with PD(ctx) as pd: + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, + u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + with d.DM(ctx, dm_attrs) as dm: + dm_mr_len = random.randint(1, dm_len) + dm_mr_offset = random.randint(0, (dm_len - dm_mr_len)) + dm_mr = DMMR(pd, dm_mr_len, e.IBV_ACCESS_ZERO_BASED, dm=dm, + offset=dm_mr_offset) + + def test_destroy_dm_mr(self): + """ + Test freeing of dm_mr + """ + lst = d.get_device_list() + for dev in lst: + with d.Context(name=dev.name.decode()) as ctx: + attr = ctx.query_device_ex() + if attr.max_dm_size == 0: + return + with PD(ctx) as pd: + dm_len = random.randrange(u.MIN_DM_SIZE, attr.max_dm_size, + u.DM_ALIGNMENT) + dm_attrs = u.get_dm_attrs(dm_len) + with d.DM(ctx, dm_attrs) as dm: + dm_mr_len = random.randint(1, dm_len) + dm_mr_offset = random.randint(0, (dm_len - dm_mr_len)) + dm_mr = DMMR(pd, dm_mr_len, e.IBV_ACCESS_ZERO_BASED, dm=dm, + offset=dm_mr_offset) + dm_mr.close() diff --git a/pyverbs/tests/utils.py b/pyverbs/tests/utils.py index 3281a326b324..c8b46686b0c1 100644 --- a/pyverbs/tests/utils.py +++ b/pyverbs/tests/utils.py @@ -2,9 +2,19 @@ # Copyright (c) 2019, Mellanox Technologies. All rights reserved. See COPYING file from string import ascii_lowercase as al import random + +import pyverbs.device as d import pyverbs.enums as e MAX_MR_SIZE = 4194304 +# Some HWs limit DM address and length alignment to 4 for read and write +# operations. Use a minimal length and alignment that respect that. +# For creation purposes use random alignments. As this is log2 of address +# alignment, no need for large numbers. +MIN_DM_SIZE = 4 +DM_ALIGNMENT = 4 +MIN_DM_LOG_ALIGN = 0 +MAX_DM_LOG_ALIGN = 6 def get_mr_length(): # Allocating large buffers typically fails @@ -26,3 +36,9 @@ def get_access_flags(): def get_data(length): return ''.join(random.choice(al) for i in range(length)) + + +def get_dm_attrs(dm_len): + align = random.randint(MIN_DM_LOG_ALIGN, MAX_DM_LOG_ALIGN) + # Comp mask != 0 is not supported + return d.AllocDmAttr(dm_len, align, 0)