2018-07-04 17:27:26 +02:00
|
|
|
/** Infiniband memory allocator.
|
2018-06-29 17:37:10 +02:00
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2018-06-29 17:37:10 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <infiniband/verbs.h>
|
2018-07-04 17:27:26 +02:00
|
|
|
|
2019-04-23 00:12:31 +02:00
|
|
|
#include <villas/nodes/infiniband.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-07-04 15:26:22 +02:00
|
|
|
#include <villas/memory/ib.h>
|
2021-09-13 14:59:38 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node_compat.hpp>
|
2018-06-29 17:37:10 +02:00
|
|
|
|
2021-09-13 14:59:38 +02:00
|
|
|
using namespace villas;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
|
|
|
using namespace villas::node::memory;
|
2019-06-04 16:55:38 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct ibv_mr * villas::node::memory::ib_get_mr(void *ptr)
|
2018-06-29 17:37:10 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *ma = get_allocation(ptr);
|
2018-06-29 17:37:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
return ma->ib.mr;
|
2018-06-29 17:37:10 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
struct Allocation * ib_alloc(size_t len, size_t alignment, struct Type *m)
|
2018-06-29 17:37:10 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *mi = (struct IB *) m->_vd;
|
2018-06-29 17:37:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *ma = new struct Allocation;
|
2018-07-02 19:00:55 +02:00
|
|
|
if (!ma)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2018-07-02 19:00:55 +02:00
|
|
|
|
|
|
|
ma->type = m;
|
|
|
|
ma->length = len;
|
|
|
|
ma->alignment = alignment;
|
|
|
|
|
2019-10-26 13:07:02 +02:00
|
|
|
ma->parent = mi->parent->alloc(len + sizeof(struct ibv_mr *), alignment, mi->parent);
|
2018-07-02 19:00:55 +02:00
|
|
|
ma->address = ma->parent->address;
|
2018-06-29 17:37:10 +02:00
|
|
|
|
2021-09-13 14:59:38 +02:00
|
|
|
if (!mi->pd) {
|
|
|
|
auto logger = logging.get("memory:ib");
|
|
|
|
logger->error("Protection domain is not registered!");
|
|
|
|
}
|
2018-07-07 12:56:08 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ma->ib.mr = ibv_reg_mr(mi->pd, ma->address, ma->length, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
|
2018-08-13 15:26:24 +02:00
|
|
|
if (!ma->ib.mr) {
|
2019-10-26 13:07:02 +02:00
|
|
|
mi->parent->free(ma->parent, mi->parent);
|
2020-01-21 16:26:51 +01:00
|
|
|
delete ma;
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2018-06-29 17:37:10 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
return ma;
|
2018-06-29 17:37:10 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
int ib_free(struct Allocation *ma, struct Type *m)
|
2018-06-29 17:37:10 +02:00
|
|
|
{
|
2018-07-02 19:00:55 +02:00
|
|
|
int ret;
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *mi = (struct IB *) m->_vd;
|
2018-06-29 17:37:10 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ibv_dereg_mr(ma->ib.mr);
|
2018-06-29 17:37:10 +02:00
|
|
|
|
2019-10-26 13:07:02 +02:00
|
|
|
ret = mi->parent->free(ma->parent, mi->parent);
|
2018-07-02 19:00:55 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-06-29 17:37:10 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Type * villas::node::memory::ib(NodeCompat *n, struct Type *parent)
|
2018-06-29 17:37:10 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *i = n->getData<struct infiniband>();
|
|
|
|
auto *mt = (struct Type *) malloc(sizeof(struct Type));
|
2018-06-29 17:37:10 +02:00
|
|
|
|
|
|
|
mt->name = "ib";
|
|
|
|
mt->flags = 0;
|
2021-08-10 10:12:48 -04:00
|
|
|
mt->alloc = ib_alloc;
|
|
|
|
mt->free = ib_free;
|
2018-06-29 17:37:10 +02:00
|
|
|
mt->alignment = 1;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
mt->_vd = malloc(sizeof(struct IB));
|
2018-06-29 17:37:10 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *mi = (struct memory::IB *) mt->_vd;
|
2018-06-29 17:37:10 +02:00
|
|
|
|
|
|
|
mi->pd = i->ctx.pd;
|
|
|
|
mi->parent = parent;
|
|
|
|
|
|
|
|
return mt;
|
|
|
|
}
|