Skip to content

Commit

Permalink
pyverbs: Add rdma_get_device API
Browse files Browse the repository at this point in the history
Add rdma_get_devices method. This method calls rdma_get_devices verb
and builds a list of devices.
Add "index" attribute to Device object that is initiated using
ibv_get_device_index.

Signed-off-by: Ido Kalir <idok@mellanox.com>
Signed-off-by: Edward Srouji <edwards@mellanox.com>
  • Loading branch information
idokalir1990 authored and EdwardSro committed Jul 20, 2020
1 parent 7b13174 commit d1a87a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
38 changes: 34 additions & 4 deletions pyverbs/device.pyx
Expand Up @@ -15,6 +15,7 @@ from pyverbs.base import PyverbsRDMAErrno
from pyverbs.base cimport close_weakrefs
cimport pyverbs.libibverbs_enums as e
cimport pyverbs.libibverbs as v
cimport pyverbs.librdmacm as cm
from pyverbs.cmid cimport CMID
from pyverbs.xrcd cimport XRCD
from pyverbs.addr cimport GID
Expand All @@ -35,11 +36,12 @@ class Device(PyverbsObject):
It is not a part of objects creation order - there's no need for the user
to create it for such purposes.
"""
def __init__(self, name, guid, node_type, transport_type):
def __init__(self, name, guid, node_type, transport_type, index):
self._node_type = node_type
self._transport_type = transport_type
self._name = name
self._guid = guid
self._index = index

@property
def name(self):
Expand All @@ -57,12 +59,16 @@ class Device(PyverbsObject):
def guid(self):
return self._guid

@property
def index(self):
return self._index

def __str__(self):
return 'Device {dev}, node type {ntype}, transport type {ttype},' \
' guid {guid}'.format(dev=self.name.decode(),
' guid {guid}, index {index}'.format(dev=self.name.decode(),
ntype=translate_node_type(self.node_type),
ttype=translate_transport_type(self.transport_type),
guid=guid_to_hex(self.guid))
guid=guid_to_hex(self.guid), index=self._index)


cdef class Context(PyverbsCM):
Expand Down Expand Up @@ -970,6 +976,7 @@ def get_device_list():
device node type
device transport type
device guid
device index
"""
cdef int count = 0;
cdef v.ibv_device **dev_list;
Expand All @@ -983,7 +990,30 @@ def get_device_list():
node = dev_list[i].node_type
transport = dev_list[i].transport_type
guid = be64toh(v.ibv_get_device_guid(dev_list[i]))
devices.append(Device(name, guid, node, transport))
index = v.ibv_get_device_index(dev_list[i])
devices.append(Device(name, guid, node, transport, index))
finally:
v.ibv_free_device_list(dev_list)
return devices


def rdma_get_devices():
"""
Get the RDMA devices.
:return: list of Device objects.
"""
cdef int count
cdef v.ibv_context **ctx_list
ctx_list = cm.rdma_get_devices(&count)
if ctx_list == NULL:
raise PyverbsRDMAErrno('Failed to get device list')
devices = []
for i in range(count):
name = ctx_list[i].device.name
node = ctx_list[i].device.node_type
transport = ctx_list[i].device.transport_type
guid = be64toh(v.ibv_get_device_guid(ctx_list[i].device))
index = v.ibv_get_device_index(ctx_list[i].device)
devices.append(Device(name, guid, node, transport, index))
cm.rdma_free_devices(ctx_list)
return devices
1 change: 1 addition & 0 deletions pyverbs/libibverbs.pxd
Expand Up @@ -481,6 +481,7 @@ cdef extern from 'infiniband/verbs.h':
uint32_t comp_mask

ibv_device **ibv_get_device_list(int *n)
int ibv_get_device_index(ibv_device *device);
void ibv_free_device_list(ibv_device **list)
ibv_context *ibv_open_device(ibv_device *device)
int ibv_close_device(ibv_context *context)
Expand Down
2 changes: 2 additions & 0 deletions pyverbs/librdmacm.pxd
Expand Up @@ -93,6 +93,8 @@ cdef extern from '<rdma/rdma_cma.h>':

rdma_event_channel *rdma_create_event_channel()
void rdma_destroy_event_channel(rdma_event_channel *channel)
ibv_context **rdma_get_devices(int *num_devices)
void rdma_free_devices (ibv_context **list);
int rdma_get_cm_event(rdma_event_channel *channel, rdma_cm_event **event)
int rdma_ack_cm_event(rdma_cm_event *event)
char *rdma_event_str(rdma_cm_event_type event)
Expand Down

0 comments on commit d1a87a9

Please sign in to comment.