1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/memory/ib.c

100 lines
2.6 KiB
C
Raw Normal View History

2018-06-29 17:37:10 +02:00
/** Memory allocators.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <villas/nodes/infiniband.h>
2018-07-02 14:17:50 +02:00
#include <villas/memory.h>
#include <rdma/rdma_cma.h>
2018-06-29 17:37:10 +02:00
2018-07-02 14:17:50 +02:00
struct memory_ib {
struct ibv_pd *pd;
struct memory_type *parent;
};
2018-06-29 17:37:10 +02:00
struct ibv_mr * memory_ib_mr(void *ptr)
{
struct ibv_mr *mr = (struct ibv_mr *) ptr;
return (mr - 1);
}
static struct memory_allocation * memory_ib_alloc(struct memory_type *m, size_t len, size_t alignment)
2018-06-29 17:37:10 +02:00
{
struct memory_ib *mi = (struct memory_ib *) m->_vd;
2018-06-29 17:37:10 +02:00
struct memory_allocation *ma = alloc(sizeof(struct memory_allocation));
if (!ma)
return NULL;
ma->type = m;
ma->length = len;
ma->alignment = alignment;
ma->parent = mi->parent->alloc(mi->parent, len + sizeof(struct ibv_mr *), alignment);
ma->address = ma->parent->address;
2018-06-29 17:37:10 +02:00
ma->ib.mr = ibv_reg_mr(mi->pd, ma->address, ma->length, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
if(!ma->ib.mr) {
mi->parent->free(mi->parent, ma->parent);
free(ma);
2018-06-29 17:37:10 +02:00
return NULL;
}
return ma;
2018-06-29 17:37:10 +02:00
}
static int memory_ib_free(struct memory_type *m, struct memory_allocation *ma)
2018-06-29 17:37:10 +02:00
{
int ret;
struct memory_ib *mi = (struct memory_ib *) m->_vd;
2018-06-29 17:37:10 +02:00
ibv_dereg_mr(ma->ib.mr);
2018-06-29 17:37:10 +02:00
ret = mi->parent->free(mi->parent, ma->parent);
if (ret)
return ret;
2018-06-29 17:37:10 +02:00
free(ma);
2018-06-29 17:37:10 +02:00
return 0;
}
2018-07-02 14:17:50 +02:00
struct memory_type * memory_ib(struct node *n, struct memory_type *parent)
2018-06-29 17:37:10 +02:00
{
struct infiniband *i = (struct infiniband *) n->_vd;
2018-07-02 14:17:50 +02:00
struct memory_type *mt = malloc(sizeof(struct memory_type));
2018-06-29 17:37:10 +02:00
mt->name = "ib";
mt->flags = 0;
mt->alloc = memory_ib_alloc;
mt->free = memory_ib_free;
mt->alignment = 1;
mt->_vd = malloc(sizeof(struct memory_ib));
struct memory_ib *mi = (struct memory_ib *) mt->_vd;
mi->pd = i->ctx.pd;
mi->parent = parent;
return mt;
}