Memory windows allow user applications a more flexible control over remote access to its memory. Add needed support create them using pyverbs. Signed-off-by: Noa Osherovich <noaos@xxxxxxxxxxxx> --- pyverbs/libibverbs.pxd | 9 +++++++++ pyverbs/mr.pxd | 4 ++++ pyverbs/mr.pyx | 45 ++++++++++++++++++++++++++++++++++++++++++ pyverbs/pd.pxd | 1 + pyverbs/pd.pyx | 7 +++++-- 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/pyverbs/libibverbs.pxd b/pyverbs/libibverbs.pxd index ed179383af9a..7eb1df2407c9 100644 --- a/pyverbs/libibverbs.pxd +++ b/pyverbs/libibverbs.pxd @@ -132,6 +132,13 @@ cdef extern from 'infiniband/verbs.h': ibv_cq_moderation_caps cq_mod_caps unsigned long max_dm_size + cdef struct ibv_mw: + ibv_context *context + ibv_pd *pd + unsigned int rkey + unsigned int handle + ibv_mw_type mw_type + ibv_device **ibv_get_device_list(int *n) void ibv_free_device_list(ibv_device **list) ibv_context *ibv_open_device(ibv_device *device) @@ -147,3 +154,5 @@ cdef extern from 'infiniband/verbs.h': int ibv_dealloc_pd(ibv_pd *pd) ibv_mr *ibv_reg_mr(ibv_pd *pd, void *addr, size_t length, int access) int ibv_dereg_mr(ibv_mr *mr) + ibv_mw *ibv_alloc_mw(ibv_pd *pd, ibv_mw_type type) + int ibv_dealloc_mw(ibv_mw *mw) diff --git a/pyverbs/mr.pxd b/pyverbs/mr.pxd index 92578021c3b3..91d662070135 100644 --- a/pyverbs/mr.pxd +++ b/pyverbs/mr.pxd @@ -10,3 +10,7 @@ cdef class MR(PyverbsCM): cdef v.ibv_mr *mr cdef void *buf cpdef read(self, length, offset) + +cdef class MW(PyverbsCM): + cdef object pd + cdef v.ibv_mw *mw diff --git a/pyverbs/mr.pyx b/pyverbs/mr.pyx index 31949e739376..1ab4bd809dee 100644 --- a/pyverbs/mr.pyx +++ b/pyverbs/mr.pyx @@ -107,3 +107,48 @@ cdef class MR(PyverbsCM): @property def rkey(self): return self.mr.rkey + + +cdef class MW(PyverbsCM): + def __cinit__(self, PD pd not None, v.ibv_mw_type mw_type): + """ + Initializes a memory window object of the given type + :param pd: A PD object + :param mw_type: Type of of the memory window, see ibv_mw_type enum + :return: + """ + self.mw = NULL + self.mw = v.ibv_alloc_mw(pd.pd, mw_type) + if self.mw == NULL: + raise PyverbsRDMAErrno('Failed to allocate MW') + self.pd = pd + pd.add_ref(self) + self.logger.debug('Allocated memory window of type {t}'. + format(t=mwtype2str(mw_type))) + + def __dealloc__(self): + self.close() + + cpdef close(self): + """ + Closes the underlaying C MW object. + MW may be deleted directly or by deleting its PD, which leaves the + Python object without the underlaying MW. + Need to check that the underlaying MW wasn't dealloced before. + :return: None + """ + self.logger.debug('Closing MW') + if self.mw is not NULL: + rc = v.ibv_dealloc_mw(self.mw) + if rc != 0: + raise PyverbsRDMAErrno('Failed to dealloc MW') + self.mw = NULL + self.pd = None + + +def mwtype2str(mw_type): + mw_types = {1:'IBV_MW_TYPE_1', 2:'IBV_MW_TYPE_2'} + try: + return mw_types[mw_type] + except KeyError: + return 'Unknown MW type ({t})'.format(t=mw_type) diff --git a/pyverbs/pd.pxd b/pyverbs/pd.pxd index 8381662b21be..fc2e405600d0 100644 --- a/pyverbs/pd.pxd +++ b/pyverbs/pd.pxd @@ -10,3 +10,4 @@ cdef class PD(PyverbsCM): cdef Context ctx cdef add_ref(self, obj) cdef object mrs + cdef object mws diff --git a/pyverbs/pd.pyx b/pyverbs/pd.pyx index a5fc063751e1..da580423cd81 100644 --- a/pyverbs/pd.pyx +++ b/pyverbs/pd.pyx @@ -4,7 +4,7 @@ import weakref from pyverbs.pyverbs_error import PyverbsRDMAError, PyverbsError from pyverbs.base import PyverbsRDMAErrno -from .mr cimport MR +from .mr cimport MR, MW cdef extern from 'errno.h': int errno @@ -25,6 +25,7 @@ cdef class PD(PyverbsCM): context.add_ref(self) self.logger.debug('PD: Allocated ibv_pd') self.mrs = weakref.WeakSet() + self.mws = weakref.WeakSet() def __dealloc__(self): """ @@ -42,7 +43,7 @@ cdef class PD(PyverbsCM): :return: None """ self.logger.debug('Closing PD') - self.close_weakrefs([self.mrs]) + self.close_weakrefs([self.mws, self.mrs]) if self.pd != NULL: rc = v.ibv_dealloc_pd(self.pd) if rc != 0: @@ -53,5 +54,7 @@ cdef class PD(PyverbsCM): cdef add_ref(self, obj): if isinstance(obj, MR): self.mrs.add(obj) + elif isinstance(obj, MW): + self.mws.add(obj) else: raise PyverbsError('Unrecognized object type') -- 2.17.2