bake-server.c 26.4 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7 8
#include "bake-config.h"

Philip Carns's avatar
Philip Carns committed
9
#include <assert.h>
10
#include <libpmemobj.h>
11
#include <bake-server.h>
12
#include "uthash.h"
13
#include "bake-rpc.h"
Philip Carns's avatar
Philip Carns committed
14

15
/* definition of BAKE root data structure (just a uuid for now) */
16
typedef struct
17
{   
18
    bake_target_id_t pool_id;
19
} bake_root_t;
20

21
/* definition of internal BAKE region_id_t identifier for libpmemobj back end */
22 23
typedef struct
{
24 25 26
    PMEMoid oid;
    uint64_t size;
} pmemobj_region_id_t;
27

28 29 30 31 32 33 34 35
typedef struct
{
    PMEMobjpool* pmem_pool;
    bake_root_t* pmem_root;
    bake_target_id_t target_id;
    UT_hash_handle hh;
} bake_pmem_entry_t;

36
typedef struct bake_server_context_t
37
{
38 39
    uint64_t num_targets;
    bake_pmem_entry_t* targets;
40
} bake_server_context_t;
41

42
static void bake_server_finalize_cb(void *data);
43

44
int bake_makepool(
45 46 47
        const char *pool_name,
        size_t pool_size,
        mode_t pool_mode)
48
{
49
    PMEMobjpool *pool;
50
    PMEMoid root_oid;
51
    bake_root_t *root;
52

53 54
    pool = pmemobj_create(pool_name, NULL, pool_size, pool_mode);
    if(!pool)
55
    {
56 57
        fprintf(stderr, "pmemobj_create: %s\n", pmemobj_errormsg());
        return(-1);
58 59 60
    }

    /* find root */
61
    root_oid = pmemobj_root(pool, sizeof(bake_root_t));
62 63 64
    root = pmemobj_direct(root_oid);

    /* store the target id for this bake pool at the root */
65
    uuid_generate(root->pool_id.id);
66
    pmemobj_persist(pool, root, sizeof(bake_root_t));
67
#if 0
68
    char target_string[64];
69
    uuid_unparse(root->id, target_string);
70
    fprintf(stderr, "created BAKE target ID: %s\n", target_string);
71
#endif
72 73 74 75 76 77

    pmemobj_close(pool);

    return(0);
}

78
int bake_provider_register(
79
        margo_instance_id mid,
80
        uint16_t provider_id,
81 82
        ABT_pool abt_pool,
        bake_provider_t* provider)
83
{
84
    bake_server_context_t *tmp_svr_ctx;
85

86
    /* check if a provider with the same multiplex id already exists */
87
    {
88 89
        hg_id_t id;
        hg_bool_t flag;
90
        margo_provider_registered_name(mid, "bake_probe_rpc", provider_id, &id, &flag);
91
        if(flag == HG_TRUE) {
92
            fprintf(stderr, "bake_provider_register(): a provider with the same id (%d) already exists\n", provider_id);
93 94
            return -1;
        }
95
    }
96

97 98 99
    /* allocate the resulting structure */    
    tmp_svr_ctx = calloc(1,sizeof(*tmp_svr_ctx));
    if(!tmp_svr_ctx)
100
        return(-1);
Philip Carns's avatar
Philip Carns committed
101

102
    /* register RPCs */
103
    hg_id_t rpc_id;
104
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_create_rpc",
105
            bake_create_in_t, bake_create_out_t, 
106 107 108
            bake_create_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_write_rpc",
109
            bake_write_in_t, bake_write_out_t, 
110 111 112
            bake_write_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_eager_write_rpc",
113
            bake_eager_write_in_t, bake_eager_write_out_t, 
114 115 116
            bake_eager_write_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_eager_read_rpc",
117
            bake_eager_read_in_t, bake_eager_read_out_t, 
118 119 120
            bake_eager_read_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_persist_rpc",
121
            bake_persist_in_t, bake_persist_out_t, 
122 123 124
            bake_persist_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_create_write_persist_rpc",
125
            bake_create_write_persist_in_t, bake_create_write_persist_out_t,
126 127 128
            bake_create_write_persist_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_get_size_rpc",
129
            bake_get_size_in_t, bake_get_size_out_t, 
130 131 132
            bake_get_size_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_read_rpc",
133
            bake_read_in_t, bake_read_out_t, 
134 135 136
            bake_read_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_probe_rpc",
137
            bake_probe_in_t, bake_probe_out_t, bake_probe_ult, 
138 139 140 141 142
            provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_noop_rpc",
            void, void, bake_noop_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Shane Snyder's avatar
Shane Snyder committed
143 144 145 146 147
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_remove_rpc",
            bake_remove_in_t, bake_remove_out_t, bake_remove_ult,
            provider_id, abt_pool);
    margo_register_data(mid, rpc_id, mplex_id, (void*)tmp_svr_ctx, NULL);

148

149 150 151
    /* install the bake server finalize callback */
    margo_push_finalize_callback(mid, &bake_server_finalize_cb, tmp_svr_ctx);

152 153
    if(provider != BAKE_PROVIDER_IGNORE)
        *provider = tmp_svr_ctx;
154

155
    return(0);
156 157
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
int bake_provider_add_storage_target(
        bake_provider_t provider,
        const char *target_name,
        bake_target_id_t* target_id)
{
    bake_pmem_entry_t* new_entry = calloc(1, sizeof(*new_entry));

    new_entry->pmem_pool = pmemobj_open(target_name, NULL);
    if(!(new_entry->pmem_pool)) {
        fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
        free(new_entry);
        return -1;
    }

    /* check to make sure the root is properly set */
    PMEMoid root_oid = pmemobj_root(new_entry->pmem_pool, sizeof(bake_root_t));
    new_entry->pmem_root = pmemobj_direct(root_oid);
    bake_target_id_t key = new_entry->pmem_root->pool_id;
    new_entry->target_id = key;

    if(uuid_is_null(key.id))
    {
        fprintf(stderr, "Error: BAKE pool %s is not properly initialized\n", target_name);
        pmemobj_close(new_entry->pmem_pool);
        free(new_entry);
        return(-1);
    }

    /* insert in the provider's hash */
    HASH_ADD(hh, provider->targets, target_id, sizeof(bake_target_id_t), new_entry);
    /* check that it was inserted */
    bake_pmem_entry_t* check_entry = NULL;
    HASH_FIND(hh, provider->targets, &key, sizeof(bake_target_id_t), check_entry);
    if(check_entry != new_entry) {
        fprintf(stderr, "Error: BAKE could not insert new pmem pool into the hash\n");
        pmemobj_close(new_entry->pmem_pool);
        free(new_entry);
        return -1;
    }

    provider->num_targets += 1;
    *target_id = key;
    return 0;
}

static bake_pmem_entry_t* find_pmem_entry(
            bake_provider_t provider,
            bake_target_id_t target_id)
{
    bake_pmem_entry_t* entry = NULL;
    HASH_FIND(hh, provider->targets, &target_id, sizeof(bake_target_id_t), entry);
    return entry;
}

int bake_provider_remove_storage_target(
        bake_provider_t provider,
        bake_target_id_t target_id)
{
    bake_pmem_entry_t* entry = NULL;
    HASH_FIND(hh, provider->targets, &target_id, sizeof(bake_target_id_t), entry);
    if(!entry) return -1;
    pmemobj_close(entry->pmem_pool);
    HASH_DEL(provider->targets, entry);
    free(entry);
    return 0;
}

int bake_provider_remove_all_storage_targets(
        bake_provider_t provider)
{
    bake_pmem_entry_t *p, *tmp;
    HASH_ITER(hh, provider->targets, p, tmp) {
        HASH_DEL(provider->targets, p);
        pmemobj_close(p->pmem_pool);
        free(p);
    }
234
    provider->num_targets = 0;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    return 0;
}

int bake_provider_count_storage_targets(
        bake_provider_t provider,
        uint64_t* num_targets)
{
    *num_targets = provider->num_targets;
    return 0;
}

int bake_provider_list_storage_targets(
        bake_provider_t provider,
        bake_target_id_t* targets)
{
    bake_pmem_entry_t *p, *tmp;
    uint64_t i = 0;
    HASH_ITER(hh, provider->targets, p, tmp) {
        targets[i] = p->target_id;
        i += 1;
    }
    return 0;
}

259
/* service a remote RPC that creates a BAKE region */
260
static void bake_create_ult(hg_handle_t handle)
261
{
262 263
    bake_create_out_t out;
    bake_create_in_t in;
264 265 266
    hg_return_t hret;
    pmemobj_region_id_t* prid;

267 268 269 270
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    bake_provider_t svr_ctx = 
271
        margo_registered_data(mid, info->id);
272 273
    if(!svr_ctx) {
        fprintf(stderr, "Error: BAKE create could not find provider\n"); 
274
        goto respond_with_error;
275 276
    }

277 278 279 280
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
        goto respond_with_error;

281 282 283 284 285 286 287
    /* find the pmem pool */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, in.bti);
    if(entry == NULL) {
        fprintf(stderr, "Error: BAKE create could not find storage target\n");
        goto respond_with_error;
    }

288
    /* TODO: this check needs to be somewhere else */
289
    assert(sizeof(pmemobj_region_id_t) <= BAKE_REGION_ID_DATA_SIZE);
290

291 292 293 294
    memset(&out, 0, sizeof(out));

    prid = (pmemobj_region_id_t*)out.rid.data;
    prid->size = in.region_size;
295 296 297
    /* find the pmem pool */
    out.ret = pmemobj_alloc(entry->pmem_pool, &prid->oid,
            in.region_size, 0, NULL, NULL);
298

299 300
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
301 302

finish:
303
    margo_destroy(handle);
304
    return;
305 306 307 308 309

respond_with_error:
    out.ret = -1;
    margo_respond(handle, &out);
    goto finish;
310
}
311
DEFINE_MARGO_RPC_HANDLER(bake_create_ult)
312

313
    /* service a remote RPC that writes to a BAKE region */
314
static void bake_write_ult(hg_handle_t handle)
315
{
316 317
    bake_write_out_t out;
    bake_write_in_t in;
318
    hg_return_t hret;
319
    hg_addr_t src_addr;
320 321
    char* buffer;
    hg_bulk_t bulk_handle;
322
    const struct hg_info *hgi;
323 324 325 326 327
    margo_instance_id mid;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

328 329
    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
330
    hgi = margo_get_info(handle);
331
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
332 333 334 335 336 337
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
338

339
    hret = margo_get_input(handle, &in);
340 341 342
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
343 344
        margo_respond(handle, &out);
        margo_destroy(handle);
345 346 347 348 349 350 351 352 353 354
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
355 356 357
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
358 359 360 361
        return;
    }

    /* create bulk handle for local side of transfer */
362
    hret = margo_bulk_create(mid, 1, (void**)(&buffer), &in.bulk_size, 
363
            HG_BULK_WRITE_ONLY, &bulk_handle);
364 365 366
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
367 368 369
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
370 371 372
        return;
    }

373 374
    if(in.remote_addr_str)
    {
375
        /* a proxy address was provided to pull write data from */
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
            out.ret = -1;
            margo_bulk_free(bulk_handle);
            margo_free_input(handle, &in);
            margo_respond(handle, &out);
            margo_destroy(handle);
            return;
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PULL, src_addr, in.bulk_handle,
394
            in.bulk_offset, bulk_handle, 0, in.bulk_size);
395
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
396
    {
397
        out.ret = -1;
398 399
        if(in.remote_addr_str)
            margo_addr_free(mid, src_addr);
400 401 402 403
        margo_bulk_free(bulk_handle);
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
404
        return;
Philip Carns's avatar
Philip Carns committed
405 406
    }

407 408
    out.ret = 0;

409 410
    if(in.remote_addr_str)
        margo_addr_free(mid, src_addr);
411 412 413 414
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
415 416
    return;
}
417
DEFINE_MARGO_RPC_HANDLER(bake_write_ult)
418

419
    /* service a remote RPC that writes to a BAKE region in eager mode */
420
static void bake_eager_write_ult(hg_handle_t handle)
421
{
422 423
    bake_eager_write_out_t out;
    bake_eager_write_in_t in;
424 425 426 427 428 429 430
    hg_return_t hret;
    char* buffer;
    hg_bulk_t bulk_handle;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

431 432 433
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
434
    bake_provider_t svr_ctx = margo_registered_data(mid, info->id);
435 436 437 438 439 440 441
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

442
    hret = margo_get_input(handle, &in);
443 444 445
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
446 447
        margo_respond(handle, &out);
        margo_destroy(handle);
448 449 450 451 452 453 454 455
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
456
    {
457
        out.ret = -1;
458 459 460
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
461
        return;
462
    }
463 464 465 466 467

    memcpy(buffer, in.buffer, in.size);

    out.ret = 0;

468 469 470
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
471 472
    return;
}
473
DEFINE_MARGO_RPC_HANDLER(bake_eager_write_ult)
474

475
    /* service a remote RPC that persists to a BAKE region */
476
static void bake_persist_ult(hg_handle_t handle)
477
{
478 479
    bake_persist_out_t out;
    bake_persist_in_t in;
480 481 482
    hg_return_t hret;
    char* buffer;
    pmemobj_region_id_t* prid;
483

484 485
    memset(&out, 0, sizeof(out));

486 487 488
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
489
    bake_provider_t svr_ctx = margo_registered_data(mid, info->id);
490 491 492 493 494 495 496
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

497
    hret = margo_get_input(handle, &in);
498
    if(hret != HG_SUCCESS)
499
    {
500
        out.ret = -1;
501 502
        margo_respond(handle, &out);
        margo_destroy(handle);
503
        return;
504
    }
505

506 507 508 509 510
    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
Philip Carns's avatar
Philip Carns committed
511
    {
512
        out.ret = -1;
513 514 515
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
516
        return;
Philip Carns's avatar
Philip Carns committed
517
    }
518 519

    /* TODO: should this have an abt shim in case it blocks? */
520 521
    PMEMobjpool* pmem_pool = pmemobj_pool_by_oid(prid->oid);
    pmemobj_persist(pmem_pool, buffer, prid->size);
522 523 524

    out.ret = 0;

525 526 527
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
528 529
    return;
}
530
DEFINE_MARGO_RPC_HANDLER(bake_persist_ult)
531

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
static void bake_create_write_persist_ult(hg_handle_t handle)
{
    bake_create_write_persist_out_t out;
    bake_create_write_persist_in_t in;
    hg_addr_t src_addr;
    char* buffer;
    hg_bulk_t bulk_handle;
    const struct hg_info *hgi;
    margo_instance_id mid;
    hg_return_t hret;
    int ret;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

547 548
    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
549
    hgi = margo_get_info(handle);
550
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
551
    if(!svr_ctx) {
552
        fprintf(stderr, "Error: BAKE create_write_persist could not find provider\n");
553 554 555 556 557 558 559 560
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* TODO: this check needs to be somewhere else */
    assert(sizeof(pmemobj_region_id_t) <= BAKE_REGION_ID_DATA_SIZE);
561 562 563 564 565 566 567 568 569 570

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

571 572 573 574 575 576 577 578 579 580 581
    /* find the pmem pool */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, in.bti);
    if(entry == NULL) {
        fprintf(stderr, "Error: BAKE create_write_persist could not find storage target\n");
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

582 583
    prid = (pmemobj_region_id_t*)out.rid.data;
    prid->size = in.region_size;
584 585
    ret = pmemobj_alloc(entry->pmem_pool, &prid->oid,
            in.region_size, 0, NULL, NULL);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    if(ret != 0)
    {
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* create bulk handle for local side of transfer */
    hret = margo_bulk_create(mid, 1, (void**)(&buffer), &in.bulk_size,
608
            HG_BULK_WRITE_ONLY, &bulk_handle);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    if(in.remote_addr_str)
    {
        /* a proxy address was provided to pull write data from */
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
            out.ret = -1;
            margo_bulk_free(bulk_handle);
            margo_free_input(handle, &in);
            margo_respond(handle, &out);
            margo_destroy(handle);
            return;
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PULL, src_addr, in.bulk_handle,
639
            in.bulk_offset, bulk_handle, 0, in.bulk_size);
640 641 642 643 644 645 646 647 648 649 650 651 652
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        if(in.remote_addr_str)
            margo_addr_free(mid, src_addr);
        margo_bulk_free(bulk_handle);
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* TODO: should this have an abt shim in case it blocks? */
653
    pmemobj_persist(entry->pmem_pool, buffer, prid->size);
654 655 656 657 658 659 660 661 662 663 664 665 666

    out.ret = 0;

    if(in.remote_addr_str)
        margo_addr_free(mid, src_addr);
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_create_write_persist_ult)

667
    /* service a remote RPC that retrieves the size of a BAKE region */
668
static void bake_get_size_ult(hg_handle_t handle)
669
{
670 671
    bake_get_size_out_t out;
    bake_get_size_in_t in;
672 673 674 675 676
    hg_return_t hret;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

677 678 679
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
680
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
681 682 683 684 685 686
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
687

688
    hret = margo_get_input(handle, &in);
689
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
690
    {
691
        out.ret = -1;
692 693
        margo_respond(handle, &out);
        margo_destroy(handle);
694
        return;
Philip Carns's avatar
Philip Carns committed
695 696
    }

697 698 699 700 701 702
    prid = (pmemobj_region_id_t*)in.rid.data;

    /* kind of cheating here; the size is encoded in the RID */
    out.size = prid->size;
    out.ret = 0;

703 704 705
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
706 707
    return;
}
708
DEFINE_MARGO_RPC_HANDLER(bake_get_size_ult)
709

710
    /* service a remote RPC for a BAKE no-op */
711
static void bake_noop_ult(hg_handle_t handle)
712
{
713 714 715
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
716
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
717

718 719
    margo_respond(handle, NULL);
    margo_destroy(handle);
720 721
    return;
}
722
DEFINE_MARGO_RPC_HANDLER(bake_noop_ult)
723

724 725
    /* TODO consolidate with write handler; read and write are nearly identical */
    /* service a remote RPC that reads from a BAKE region */
726
static void bake_read_ult(hg_handle_t handle)
727
{
728 729
    bake_read_out_t out;
    bake_read_in_t in;
730
    hg_return_t hret;
731
    hg_addr_t src_addr;
732 733
    char* buffer;
    hg_bulk_t bulk_handle;
734
    const struct hg_info *hgi;
735 736 737 738 739
    margo_instance_id mid;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

740 741
    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
742
    hgi = margo_get_info(handle);
743
    bake_provider_t svr_ctx = 
744
        margo_registered_data(mid, hgi->id);
745 746 747 748 749 750
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
751

752
    hret = margo_get_input(handle, &in);
753
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
754
    {
755
        out.ret = -1;
756 757
        margo_respond(handle, &out);
        margo_destroy(handle);
758
        return;
Philip Carns's avatar
Philip Carns committed
759 760
    }

761 762 763 764 765
    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
Philip Carns's avatar
Philip Carns committed
766
    {
767
        out.ret = -1;
768 769 770
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
771
        return;
Philip Carns's avatar
Philip Carns committed
772 773
    }

774
    /* create bulk handle for local side of transfer */
775
    hret = margo_bulk_create(mid, 1, (void**)(&buffer), &in.bulk_size, 
776
            HG_BULK_READ_ONLY, &bulk_handle);
777
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
778
    {
779
        out.ret = -1;
780 781 782
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
783
        return;
Philip Carns's avatar
Philip Carns committed
784
    }
785

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    if(in.remote_addr_str)
    {
        /* a proxy address was provided to push read data to */
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
            out.ret = -1;
            margo_bulk_free(bulk_handle);
            margo_free_input(handle, &in);
            margo_respond(handle, &out);
            margo_destroy(handle);
            return;
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PUSH, src_addr, in.bulk_handle,
807
            in.bulk_offset, bulk_handle, 0, in.bulk_size);
808
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
809
    {
810
        out.ret = -1;
811 812
        if(in.remote_addr_str)
            margo_addr_free(mid, src_addr);
813 814 815 816
        margo_bulk_free(bulk_handle);
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
817
        return;
Philip Carns's avatar
Philip Carns committed
818 819
    }

820
    out.ret = 0;
Philip Carns's avatar
Philip Carns committed
821

822 823
    if(in.remote_addr_str)
        margo_addr_free(mid, src_addr);
824 825 826 827
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
828 829
    return;
}
830
DEFINE_MARGO_RPC_HANDLER(bake_read_ult)
Philip Carns's avatar
Philip Carns committed
831

832 833
    /* service a remote RPC that reads from a BAKE region and eagerly sends
     * response */
834
static void bake_eager_read_ult(hg_handle_t handle)
835
{
836 837
    bake_eager_read_out_t out;
    bake_eager_read_in_t in;
838 839 840 841 842 843 844
    hg_return_t hret;
    char* buffer;
    hg_size_t size;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

845 846 847 848
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
    bake_provider_t svr_ctx = 
849
        margo_registered_data(mid, hgi->id);
850 851 852 853 854 855 856
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

857
    hret = margo_get_input(handle, &in);
858 859 860
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
861 862
        margo_respond(handle, &out);
        margo_destroy(handle);
863 864 865 866
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;
Philip Carns's avatar
Philip Carns committed
867

868 869 870 871 872
    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
873 874 875
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
876 877
        return;
    }
878

879 880 881 882
    out.ret = 0;
    out.buffer = buffer;
    out.size = in.size;

883 884 885
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
886
    return;
Philip Carns's avatar
Philip Carns committed
887
}
888
DEFINE_MARGO_RPC_HANDLER(bake_eager_read_ult)
889

890
    /* service a remote RPC that probes for a BAKE target id */
891
static void bake_probe_ult(hg_handle_t handle)
892
{
893
    bake_probe_out_t out;
894

895 896
    memset(&out, 0, sizeof(out));

897 898 899 900
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
    bake_provider_t svr_ctx = 
901
        margo_registered_data(mid, hgi->id);
902 903 904 905 906 907 908
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

909
    out.ret = 0;
910
    // XXX this is where we should handle multiple targets
911 912 913 914 915
    uint64_t targets_count;
    bake_provider_count_storage_targets(svr_ctx, &targets_count);
    bake_target_id_t targets[targets_count];
    bake_provider_list_storage_targets(svr_ctx, targets);

916
    out.targets = targets;
917
    out.num_targets = targets_count;
918

919 920
    margo_respond(handle, &out);
    margo_destroy(handle);
921 922
    return;
}
923
DEFINE_MARGO_RPC_HANDLER(bake_probe_ult)
924

Shane Snyder's avatar
Shane Snyder committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
static void bake_remove_ult(hg_handle_t handle)
{
    bake_remove_in_t in;
    bake_remove_out_t out;
    hg_return_t hret;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
    bake_provider_t svr_ctx = 
        margo_registered_data_mplex(mid, hgi->id, hgi->target_id);
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    pmemobj_free(&prid->oid);

    out.ret = 0;

    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_remove_ult)

968
static void bake_server_finalize_cb(void *data)
969
{
970 971 972
    bake_server_context_t *svr_ctx = (bake_server_context_t *)data;
    assert(svr_ctx);

973 974
    bake_provider_remove_all_storage_targets(svr_ctx);

975 976 977 978
    free(svr_ctx);

    return;
}
Philip Carns's avatar
Philip Carns committed
979