Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch introduces PD object which represents ibv_pd. This commit also introduces the usage of weak references. In Python, a weak reference is a reference to an object not strong enough to keep it from being garbage-collected. In Pyverbs, it is used to allow an object to keep track of the objects created using it, so if an object is being destroyed (e.g. explicitly by the user), it can iterate over the dependent objects and destroy the underlying C objects. E.g.: A user opens a device and creates a PD. If the Context object is being destroyed before the PD object, the kernel will return EBUSY. To avoid that, the Context object will hold a weakref to the PD and destroy the C PD before closing itself. This renders the Python PD object useless, but it is a user's choice that we are not blocking. In order to provide a clean teardown, Pyverbs Context is also a context manager, which means itshouldbe used within a 'with' block. This way, when the block is over, teardown will be executed properly. The same applies for all other relevant pyverbs' classes. A PyverbsCM class was added as an alternative base objects to inherit from. It inherits from PyverbsObject and adds the __enter__ and __exit__ functions needed for a Python context manager. Signed-off-by: Noa Osherovich <noaos@mellanox.com>
- Loading branch information
Showing
8 changed files
with
157 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) | ||
# Copyright (c) 2018, Mellanox Technologies. All rights reserved. | ||
# Copyright (c) 2019, Mellanox Technologies. All rights reserved. | ||
|
||
cdef class PyverbsObject(object): | ||
cdef object __weakref__ | ||
cdef object logger | ||
|
||
cdef class PyverbsCM(PyverbsObject): | ||
cpdef close(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) | ||
# Copyright (c) 2018, Mellanox Technologies. All rights reserved. See COPYING file | ||
|
||
from .base cimport PyverbsObject | ||
from .base cimport PyverbsObject, PyverbsCM | ||
cimport pyverbs.libibverbs as v | ||
|
||
|
||
cdef class Context(PyverbsObject): | ||
cdef class Context(PyverbsCM): | ||
cdef v.ibv_context *context | ||
cdef object name | ||
cpdef close(self) | ||
cdef add_ref(self, obj) | ||
cdef object pds | ||
|
||
cdef class DeviceAttr(PyverbsObject): | ||
cdef v.ibv_device_attr dev_attr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) | ||
# Copyright (c) 2019, Mellanox Technologies. All rights reserved. | ||
from pyverbs.device cimport Context | ||
cimport pyverbs.libibverbs as v | ||
from .base cimport PyverbsCM | ||
|
||
|
||
cdef class PD(PyverbsCM): | ||
cdef v.ibv_pd *pd | ||
cdef Context ctx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) | ||
# Copyright (c) 2019, Mellanox Technologies. All rights reserved. | ||
from pyverbs.pyverbs_error import PyverbsRDMAError | ||
from pyverbs.base import PyverbsRDMAErrno | ||
|
||
cdef extern from 'errno.h': | ||
int errno | ||
|
||
|
||
cdef class PD(PyverbsCM): | ||
def __cinit__(self, Context context not None): | ||
""" | ||
Initializes a PD object. A reference for the creating Context is kept | ||
so that Python's GC will destroy the objects in the right order. | ||
:param context: The Context object creating the PD | ||
:return: The newly created PD on success | ||
""" | ||
self.pd = v.ibv_alloc_pd(<v.ibv_context*>context.context) | ||
if self.pd == NULL: | ||
raise PyverbsRDMAErrno('Failed to allocate PD', errno) | ||
self.ctx = context | ||
context.add_ref(self) | ||
self.logger.debug('PD: Allocated ibv_pd') | ||
|
||
def __dealloc__(self): | ||
""" | ||
Closes the inner PD. | ||
:return: None | ||
""" | ||
self.close() | ||
|
||
cpdef close(self): | ||
""" | ||
Closes the underlying C object of the PD. | ||
PD may be deleted directly or indirectly by closing its context, which | ||
leaves the Python PD object without the underlying C object, so during | ||
destruction, need to check whether or not the C object exists. | ||
:return: None | ||
""" | ||
self.logger.debug('Closing PD') | ||
if self.pd != NULL: | ||
rc = v.ibv_dealloc_pd(self.pd) | ||
if rc != 0: | ||
raise PyverbsRDMAErrno('Failed to dealloc PD') | ||
self.pd = NULL | ||
self.ctx = None |