ssg.c 34.4 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (c) 2016 UChicago Argonne, LLC
 *
 * See COPYRIGHT in top-level directory.
 */

7
#include "ssg-config.h"
Shane Snyder's avatar
Shane Snyder committed
8

Jonathan Jenkins's avatar
Jonathan Jenkins committed
9 10 11 12
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
13
#include <errno.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
14 15
#include <stdlib.h>
#include <string.h>
16
#include <time.h>
Jonathan Jenkins's avatar
Jonathan Jenkins committed
17
#include <assert.h>
Shane Snyder's avatar
Shane Snyder committed
18
#ifdef SSG_HAVE_MPI
19 20
#include <mpi.h>
#endif
Jonathan Jenkins's avatar
Jonathan Jenkins committed
21

Shane Snyder's avatar
Shane Snyder committed
22
#include <mercury.h>
23
#include <abt.h>
Shane Snyder's avatar
Shane Snyder committed
24
#include <margo.h>
25

26
#include "ssg.h"
Shane Snyder's avatar
Shane Snyder committed
27 28 29
#ifdef SSG_HAVE_MPI
#include "ssg-mpi.h"
#endif
30
#include "ssg-internal.h"
31
#ifdef SSG_USE_SWIM_FD
32 33
#include "swim-fd/swim-fd.h"
#endif
34
#include "uthash.h"
35

36 37 38
/* arguments for group lookup ULTs */
struct ssg_group_lookup_ult_args
{
39
    ssg_group_view_t *view;
40 41 42 43 44
    ssg_member_state_t *member_state;
    hg_return_t out;
};
static void ssg_group_lookup_ult(void * arg);

45
/* SSG helper routine prototypes */
46
static ssg_group_descriptor_t * ssg_group_descriptor_create(
47
    uint64_t name_hash, const char * leader_addr_str, int owner_status);
48 49 50 51 52
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor);
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor);
static int ssg_group_view_create(
53 54 55
    const char * const group_addr_strs[], int group_size,
    hg_addr_t self_addr, ssg_member_id_t * self_id,
    ssg_group_view_t * view);
56 57
static ssg_member_id_t ssg_gen_member_id(
    const char * addr_str);
58 59 60 61 62 63 64 65
static void ssg_group_view_destroy(
    ssg_group_view_t * view);
static void ssg_group_destroy_internal(
    ssg_group_t * g);
static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag);
static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs);
66

67 68
/* XXX: i think we ultimately need per-mid ssg instances rather than 1 global? */
ssg_instance_t *ssg_inst = NULL;
69

70 71 72
/***************************************************
 *** SSG runtime intialization/shutdown routines ***
 ***************************************************/
73

74 75
int ssg_init(
    margo_instance_id mid)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
76
{
77 78
    struct timespec ts;

79 80 81 82 83 84 85 86 87 88
    if (ssg_inst)
        return SSG_FAILURE;

    /* initialize an SSG instance for this margo instance */
    ssg_inst = malloc(sizeof(*ssg_inst));
    if (!ssg_inst)
        return SSG_FAILURE;
    memset(ssg_inst, 0, sizeof(*ssg_inst));
    ssg_inst->mid = mid;

89
    ssg_register_rpcs();
90

91 92 93 94
    /* seed RNG */
    clock_gettime(CLOCK_MONOTONIC, &ts);
    srand(ts.tv_nsec + getpid());

95 96
    return SSG_SUCCESS;
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
97

98
int ssg_finalize()
99
{
100 101
    ssg_group_t *g, *g_tmp;
    ssg_attached_group_t *ag, *ag_tmp;
102 103 104 105 106

    if (!ssg_inst)
        return SSG_FAILURE;

    /* destroy all active groups */
107
    HASH_ITER(hh, ssg_inst->group_table, g, g_tmp)
108 109 110 111 112
    {
        HASH_DELETE(hh, ssg_inst->group_table, g);
        ssg_group_destroy_internal(g);
    }

113 114 115 116 117 118 119
    /* detach from all attached groups */
    HASH_ITER(hh, ssg_inst->attached_group_table, ag, ag_tmp)
    {
        HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
        ssg_attached_group_destroy(ag);
    }

120 121 122
    free(ssg_inst);
    ssg_inst = NULL;

123
    return SSG_SUCCESS;
124
}
Jonathan Jenkins's avatar
Jonathan Jenkins committed
125

126 127 128
/*************************************
 *** SSG group management routines ***
 *************************************/
Jonathan Jenkins's avatar
Jonathan Jenkins committed
129

Shane Snyder's avatar
Shane Snyder committed
130
ssg_group_id_t ssg_group_create(
131 132
    const char * group_name,
    const char * const group_addr_strs[],
133 134 135
    int group_size,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
136
{
137
    uint64_t name_hash;
Shane Snyder's avatar
Shane Snyder committed
138
    hg_addr_t self_addr = HG_ADDR_NULL;
139
    ssg_group_descriptor_t *tmp_descriptor;
140
    ssg_group_t *g = NULL;
141
    hg_return_t hret;
142
    int sret;
Shane Snyder's avatar
Shane Snyder committed
143
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
144

145
    if (!ssg_inst) return group_id;
146

147
    name_hash = ssg_hash64_str(group_name);
148

149
    /* generate a unique ID for this group  */
150 151
    tmp_descriptor = ssg_group_descriptor_create(name_hash, group_addr_strs[0],
        SSG_OWNER_IS_MEMBER);
152
    if (tmp_descriptor == NULL) return group_id;
153

Shane Snyder's avatar
Shane Snyder committed
154
    /* make sure we aren't re-creating an existing group */
155
    HASH_FIND(hh, ssg_inst->group_table, &tmp_descriptor->name_hash,
Shane Snyder's avatar
Shane Snyder committed
156
        sizeof(uint64_t), g);
157 158 159 160 161
    if (g)
    {
        g = NULL;
        goto fini;
    }
162

163 164 165 166 167 168 169
    /* allocate an SSG group data structure and initialize some of it */
    g = malloc(sizeof(*g));
    if (!g) goto fini;
    memset(g, 0, sizeof(*g));
    g->name = strdup(group_name);
    if (!g->name) goto fini;
    g->descriptor = tmp_descriptor;
170 171
    g->update_cb = update_cb;
    g->update_cb_dat = update_cb_dat;
172

173 174 175 176
    /* get my address */
    hret = margo_addr_self(ssg_inst->mid, &self_addr);
    if (hret != HG_SUCCESS) goto fini;

177
    /* initialize the group view */
178 179
    sret = ssg_group_view_create(group_addr_strs, group_size, self_addr,
        &g->self_id, &g->view);
180
    if (sret != SSG_SUCCESS) goto fini;
181
    if (g->self_id == SSG_MEMBER_ID_INVALID)
182
    {
183
        /* if unable to resolve my rank within the group, error out */
184 185 186 187 188
        fprintf(stderr, "Error: SSG unable to resolve rank in group %s\n",
            group_name);
        goto fini;
    }

189
#ifdef SSG_USE_SWIM_FD
190
    /* initialize swim failure detector */
191 192
    // TODO: we should probably barrier or sync somehow to avoid rpc failures
    // due to timing skew of different ranks initializing swim
193
    g->fd_ctx = (void *)swim_init(g, 1);
194
    if (g->fd_ctx == NULL) goto fini;
195 196
#endif

Shane Snyder's avatar
Shane Snyder committed
197 198 199
    /* everything successful -- set the output group identifier, which is just
     * an opaque pointer to the group descriptor structure
     */
200 201
    group_id = (ssg_group_id_t)ssg_group_descriptor_dup(g->descriptor);
    if (group_id == SSG_GROUP_ID_NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
202

203 204 205 206
    /* add this group reference to our group table */
    HASH_ADD(hh, ssg_inst->group_table, descriptor->name_hash,
        sizeof(uint64_t), g);

Shane Snyder's avatar
Shane Snyder committed
207
    SSG_DEBUG(g, "group create successful (size=%d)\n", group_size);
208

209
    /* don't free group pointer on success */
210 211
    g = NULL;
fini:
212
    if (self_addr != HG_ADDR_NULL) margo_addr_free(ssg_inst->mid, self_addr);
213 214
    if (g)
    {
215
        ssg_group_view_destroy(&g->view);
216
        free(g->name);
217 218
        free(g);
    }
219 220
    if (group_id == SSG_GROUP_ID_NULL)
        ssg_group_descriptor_free(tmp_descriptor);
221

Shane Snyder's avatar
Shane Snyder committed
222
    return group_id;
223 224
}

Shane Snyder's avatar
Shane Snyder committed
225
ssg_group_id_t ssg_group_create_config(
226
    const char * group_name,
227 228 229
    const char * file_name,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
230 231 232 233
{
    int fd;
    struct stat st;
    char *rd_buf = NULL;
234
    ssize_t rd_buf_size;
235
    char *tok;
236 237
    void *addr_str_buf = NULL;
    int addr_str_buf_len = 0, num_addrs = 0;
238 239
    int ret;
    const char **addr_strs = NULL;
Shane Snyder's avatar
Shane Snyder committed
240
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

    /* open config file for reading */
    fd = open(file_name, O_RDONLY);
    if (fd == -1)
    {
        fprintf(stderr, "Error: SSG unable to open config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }

    /* get file size and allocate a buffer to store it */
    ret = fstat(fd, &st);
    if (ret == -1)
    {
        fprintf(stderr, "Error: SSG unable to stat config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
    rd_buf = malloc(st.st_size+1);
    if (rd_buf == NULL) goto fini;

    /* load it all in one fell swoop */
263 264
    rd_buf_size = read(fd, rd_buf, st.st_size);
    if (rd_buf_size != st.st_size)
265 266 267 268 269
    {
        fprintf(stderr, "Error: SSG unable to read config file %s for group %s\n",
            file_name, group_name);
        goto fini;
    }
270
    rd_buf[rd_buf_size]='\0';
271 272 273 274 275

    /* strtok the result - each space-delimited address is assumed to be
     * a unique mercury address
     */
    tok = strtok(rd_buf, "\r\n\t ");
Jonathan Jenkins's avatar
Jonathan Jenkins committed
276 277
    if (tok == NULL) goto fini;

278
    /* build up the address buffer */
279 280
    addr_str_buf = malloc(rd_buf_size);
    if (addr_str_buf == NULL) goto fini;
281 282
    do
    {
283 284 285
        int tok_size = strlen(tok);
        memcpy((char*)addr_str_buf + addr_str_buf_len, tok, tok_size+1);
        addr_str_buf_len += tok_size+1;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
286 287 288
        num_addrs++;
        tok = strtok(NULL, "\r\n\t ");
    } while (tok != NULL);
289
    if (addr_str_buf_len != rd_buf_size)
290 291
    {
        /* adjust buffer size if our initial guess was wrong */
292
        void *tmp = realloc(addr_str_buf, addr_str_buf_len);
293
        if (tmp == NULL) goto fini;
294
        addr_str_buf = tmp;
295
    }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
296

297
    /* set up address string array for group members */
298
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, num_addrs);
299
    if (!addr_strs) goto fini;
300

301
    /* invoke the generic group create routine using our list of addrs */
302 303
    group_id = ssg_group_create(group_name, addr_strs, num_addrs,
        update_cb, update_cb_dat);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
304 305

fini:
306
    /* cleanup before returning */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
307
    if (fd != -1) close(fd);
308
    free(rd_buf);
309
    free(addr_str_buf);
310 311
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
312
    return group_id;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
313 314
}

Shane Snyder's avatar
Shane Snyder committed
315
#ifdef SSG_HAVE_MPI
Shane Snyder's avatar
Shane Snyder committed
316
ssg_group_id_t ssg_group_create_mpi(
317
    const char * group_name,
318 319 320
    MPI_Comm comm,
    ssg_membership_update_cb update_cb,
    void * update_cb_dat)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
321
{
322
    int i;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
323
    hg_addr_t self_addr = HG_ADDR_NULL;
324
    char *self_addr_str = NULL;
325 326
    hg_size_t self_addr_str_size = 0;
    int self_addr_str_size_int = 0; /* for mpi-friendly conversion */
327
    char *addr_str_buf = NULL;
328 329 330 331
    int *sizes = NULL;
    int *sizes_psum = NULL;
    int comm_size = 0, comm_rank = 0;
    const char **addr_strs = NULL;
332
    hg_return_t hret;
Shane Snyder's avatar
Shane Snyder committed
333
    ssg_group_id_t group_id = SSG_GROUP_ID_NULL;
334

335 336
    if (!ssg_inst) goto fini;

337
    /* get my address */
338
    hret = margo_addr_self(ssg_inst->mid, &self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
339
    if (hret != HG_SUCCESS) goto fini;
340
    hret = margo_addr_to_string(ssg_inst->mid, NULL, &self_addr_str_size, self_addr);
Shane Snyder's avatar
Shane Snyder committed
341
    if (hret != HG_SUCCESS) goto fini;
342
    self_addr_str = malloc(self_addr_str_size);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
343
    if (self_addr_str == NULL) goto fini;
344
    hret = margo_addr_to_string(ssg_inst->mid, self_addr_str, &self_addr_str_size, self_addr);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
345
    if (hret != HG_SUCCESS) goto fini;
346
    self_addr_str_size_int = (int)self_addr_str_size; /* null char included in call */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
347

348
    /* gather the buffer sizes */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
349 350 351 352
    MPI_Comm_size(comm, &comm_size);
    MPI_Comm_rank(comm, &comm_rank);
    sizes = malloc(comm_size * sizeof(*sizes));
    if (sizes == NULL) goto fini;
353
    sizes[comm_rank] = self_addr_str_size_int;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
354 355
    MPI_Allgather(MPI_IN_PLACE, 0, MPI_BYTE, sizes, 1, MPI_INT, comm);

356 357 358
    /* compute a exclusive prefix sum of the data sizes, including the
     * total at the end
     */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
359 360 361
    sizes_psum = malloc((comm_size+1) * sizeof(*sizes_psum));
    if (sizes_psum == NULL) goto fini;
    sizes_psum[0] = 0;
362
    for (i = 1; i < comm_size+1; i++)
Jonathan Jenkins's avatar
Jonathan Jenkins committed
363 364
        sizes_psum[i] = sizes_psum[i-1] + sizes[i-1];

365
    /* allgather the addresses */
366 367
    addr_str_buf = malloc(sizes_psum[comm_size]);
    if (addr_str_buf == NULL) goto fini;
368
    MPI_Allgatherv(self_addr_str, self_addr_str_size_int, MPI_BYTE,
369
            addr_str_buf, sizes, sizes_psum, MPI_BYTE, comm);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
370

371
    /* set up address string array for group members */
372
    addr_strs = ssg_addr_str_buf_to_list(addr_str_buf, comm_size);
373 374 375
    if (!addr_strs) goto fini;

    /* invoke the generic group create routine using our list of addrs */
376 377
    group_id = ssg_group_create(group_name, addr_strs, comm_size,
        update_cb, update_cb_dat);
Shane Snyder's avatar
Shane Snyder committed
378 379

fini:
380
    /* cleanup before returning */
381
    if (self_addr != HG_ADDR_NULL) margo_addr_free(ssg_inst->mid, self_addr);
382
    free(self_addr_str);
383 384 385
    free(sizes);
    free(sizes_psum);
    free(addr_str_buf);
386 387
    free(addr_strs);

Shane Snyder's avatar
Shane Snyder committed
388
    return group_id;
Shane Snyder's avatar
Shane Snyder committed
389 390 391
}
#endif

392 393
int ssg_group_destroy(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
394
{
Shane Snyder's avatar
Shane Snyder committed
395
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
396
    ssg_group_t *g;
397

Shane Snyder's avatar
Shane Snyder committed
398
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
399
        return SSG_FAILURE;
400

401 402 403 404 405 406
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG unable to destroy a group it is not a member of\n");
        return SSG_FAILURE;
    }

407
    /* find the group structure and destroy it */
Shane Snyder's avatar
Shane Snyder committed
408 409
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
410 411 412 413 414
    if (!g)
    {
        fprintf(stderr, "Error: SSG unable to find expected group reference\n");
        return SSG_FAILURE;
    }
415
    HASH_DELETE(hh, ssg_inst->group_table, g);
416
    ssg_group_destroy_internal(g);
417

418
    return SSG_SUCCESS;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
419 420
}

421 422 423
int ssg_group_attach(
    ssg_group_id_t group_id)
{
Shane Snyder's avatar
Shane Snyder committed
424
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
425 426 427 428 429 430
    ssg_attached_group_t *ag = NULL;
    char *group_name = NULL;
    int group_size;
    void *view_buf = NULL;
    const char **addr_strs = NULL;
    int sret = SSG_FAILURE;
431

432
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL) goto fini;
433

434 435 436 437 438 439 440 441 442 443 444
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG unable to attach a group it is a member of\n");
        goto fini;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        fprintf(stderr, "Error: SSG unable to attach a group it is" \
            " already attached to\n");
        goto fini;
    }
445

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    /* send the attach request to a group member to initiate a bulk transfer
     * of the group's membership view
     */
    sret = ssg_group_attach_send(group_descriptor, &group_name,
        &group_size, &view_buf);
    if (sret != SSG_SUCCESS || !group_name || !view_buf) goto fini;

    /* set up address string array for group members */
    addr_strs = ssg_addr_str_buf_to_list(view_buf, group_size);
    if (!addr_strs) goto fini;

    /* allocate an SSG attached group data structure and initialize some of it */
    ag = malloc(sizeof(*ag));
    if (!ag) goto fini;
    memset(ag, 0, sizeof(*ag));
    ag->name = group_name;
    ag->descriptor = ssg_group_descriptor_dup(group_descriptor);
    if (!ag->descriptor) goto fini;
    ag->descriptor->owner_status = SSG_OWNER_IS_ATTACHER;

    /* create the view for the group */
467 468
    sret = ssg_group_view_create(addr_strs, group_size, HG_ADDR_NULL,
        NULL, &ag->view);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    if (sret != SSG_SUCCESS) goto fini;

    /* add this group reference to our group table */
    HASH_ADD(hh, ssg_inst->attached_group_table, descriptor->name_hash,
        sizeof(uint64_t), ag);

    sret = SSG_SUCCESS;

    /* don't free on success */
    ag = NULL;
fini:
    free(view_buf);
    free(addr_strs);
    if (ag)
    {
484
        ssg_group_view_destroy(&ag->view);
485 486
        free(ag->name);
        free(ag);
487
        ssg_group_descriptor_free(ag->descriptor);
488 489 490
    }

    return sret;
491 492 493 494 495
}

int ssg_group_detach(
    ssg_group_id_t group_id)
{
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_attached_group_t *ag;

    if (group_descriptor->owner_status != SSG_OWNER_IS_ATTACHER)
    {
        fprintf(stderr, "Error: SSG unable to detach from group that" \
            " was never attached\n");
        return SSG_FAILURE;
    }

    /* find the attached group structure and destroy it */
    HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), ag);
    if (!ag)
    {
        fprintf(stderr, "Error: SSG unable to find expected attached group reference\n");
        return SSG_FAILURE;
    }
    HASH_DELETE(hh, ssg_inst->attached_group_table, ag);
    ssg_attached_group_destroy(ag);

517 518 519
    return SSG_SUCCESS;
}

520 521 522
/*********************************
 *** SSG group access routines ***
 *********************************/
Shane Snyder's avatar
Shane Snyder committed
523

524
ssg_member_id_t ssg_get_group_self_id(
525
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
526
{
Shane Snyder's avatar
Shane Snyder committed
527
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
528 529
    ssg_group_t *g;

Shane Snyder's avatar
Shane Snyder committed
530
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
531
        return SSG_MEMBER_ID_INVALID;
532

533 534 535 536
    if (group_descriptor->owner_status != SSG_OWNER_IS_MEMBER)
    {
        fprintf(stderr, "Error: SSG can only obtain a self ID from a group the" \
            " caller is a member of\n");
537
        return SSG_MEMBER_ID_INVALID;
538 539
    }

Shane Snyder's avatar
Shane Snyder committed
540 541
    HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
        sizeof(uint64_t), g);
542
    if (!g)
543
        return SSG_MEMBER_ID_INVALID;
544 545

    return g->self_id;
Shane Snyder's avatar
Shane Snyder committed
546 547
}

548 549
int ssg_get_group_size(
    ssg_group_id_t group_id)
Shane Snyder's avatar
Shane Snyder committed
550
{
Shane Snyder's avatar
Shane Snyder committed
551
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
552
    ssg_group_view_t *group_view = NULL;
553

Shane Snyder's avatar
Shane Snyder committed
554
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
555 556
        return 0;

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
            group_view = &g->view;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
            group_view = &ag->view;
    }
    else
    {
        fprintf(stderr, "Error: SSG can only obtain size of groups that the caller" \
            " is a member of or an attacher of\n");
579
        return 0;
580
    }
581

582 583 584 585
    if (group_view)
        return group_view->size;
    else
        return 0;
Shane Snyder's avatar
Shane Snyder committed
586 587
}

588 589 590
hg_addr_t ssg_get_addr(
    ssg_group_id_t group_id,
    ssg_member_id_t member_id)
Shane Snyder's avatar
Shane Snyder committed
591
{
Shane Snyder's avatar
Shane Snyder committed
592
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
593
    ssg_group_view_t *group_view = NULL;
594

Shane Snyder's avatar
Shane Snyder committed
595
    if (!ssg_inst || group_id == SSG_GROUP_ID_NULL)
596 597
        return HG_ADDR_NULL;

598 599 600 601 602 603 604 605 606 607 608 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
    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
            group_view = &g->view;
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
            group_view = &ag->view;
    }
    else
    {
        fprintf(stderr, "Error: SSG can only obtain member addresses of groups" \
            " that the caller is a member of or an attacher of\n");
        return HG_ADDR_NULL;
    }

    if (group_view)
    {
        /* XXX for now we assume member ids are dense ranks and error out
         * if they are not within allowable range for the group view size
         */
        if (member_id >= group_view->size)
            return HG_ADDR_NULL;

        return group_view->member_states[member_id].addr;
    }
    else
634
        return HG_ADDR_NULL;
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
}

ssg_group_id_t ssg_group_id_dup(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *dup;

    dup = ssg_group_descriptor_dup((ssg_group_descriptor_t *)group_id);
    return (ssg_group_id_t)dup;
}

void ssg_group_id_free(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *descriptor = (ssg_group_descriptor_t *)group_id;

    ssg_group_descriptor_free(descriptor);
Shane Snyder's avatar
Shane Snyder committed
652
    descriptor = SSG_GROUP_ID_NULL;
653 654 655
    return;
}

656 657 658 659 660 661 662 663 664
char *ssg_group_id_get_addr_str(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;

    return strdup(group_descriptor->addr_str);
}


665 666 667 668 669 670 671
void ssg_group_id_serialize(
    ssg_group_id_t group_id,
    char ** buf_p,
    size_t * buf_size_p)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    size_t alloc_size;
672
    char *gid_buf, *p; 
673 674 675 676 677 678 679 680 681 682 683 684 685

    *buf_p = NULL;
    *buf_size_p = 0;

    /* determine needed buffer size */
    alloc_size = (sizeof(group_descriptor->magic_nr) + sizeof(group_descriptor->name_hash) +
        strlen(group_descriptor->addr_str) + 1);

    gid_buf = malloc(alloc_size);
    if (!gid_buf)
        return;

    /* serialize */
686 687 688 689 690 691
    p = gid_buf;
    *(uint64_t *)p = group_descriptor->magic_nr;
    p += sizeof(uint64_t);
    *(uint64_t *)p = group_descriptor->name_hash;
    p += sizeof(uint64_t);
    strcpy(p, group_descriptor->addr_str);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    /* the rest of the descriptor is stateful and not appropriate for serializing... */

    *buf_p = gid_buf;
    *buf_size_p = alloc_size;

    return;
}

void ssg_group_id_deserialize(
    const char * buf,
    size_t buf_size,
    ssg_group_id_t * group_id_p)
{
    size_t min_buf_size;
    uint64_t magic_nr;
    uint64_t name_hash;
    const char *addr_str;
    ssg_group_descriptor_t *group_descriptor;

    *group_id_p = SSG_GROUP_ID_NULL;

    /* check to ensure the buffer contains enough data to make a group ID */
    min_buf_size = (sizeof(group_descriptor->magic_nr) +
        sizeof(group_descriptor->name_hash) + 1);
    if (buf_size < min_buf_size)
    {
        fprintf(stderr, "Error: Serialized buffer does not contain a valid SSG group ID\n");
        return;
    }

    /* deserialize */
    magic_nr = *(uint64_t *)buf;
    if (magic_nr != SSG_MAGIC_NR)
    {
        fprintf(stderr, "Error: Magic number mismatch when deserializing SSG group ID\n");
        return;
    }
    buf += sizeof(uint64_t);
    name_hash = *(uint64_t *)buf;
    buf += sizeof(uint64_t);
    addr_str = buf;

    group_descriptor = ssg_group_descriptor_create(name_hash, addr_str,
        SSG_OWNER_IS_UNASSOCIATED);
    if (!group_descriptor)
        return;

    *group_id_p = (ssg_group_id_t)group_descriptor;

    return;
}

int ssg_group_id_store(
    const char * file_name,
    ssg_group_id_t group_id)
{
    int fd;
    char *buf;
    size_t buf_size;
    ssize_t bytes_written;

    fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0)
    {
        fprintf(stderr, "Error: Unable to open file %s for storing SSG group ID\n",
            file_name);
        return SSG_FAILURE;
    }

    ssg_group_id_serialize(group_id, &buf, &buf_size);
    if (buf == NULL)
    {
        fprintf(stderr, "Error: Unable to serialize SSG group ID.\n");
        close(fd);
        return SSG_FAILURE;
    }

    bytes_written = write(fd, buf, buf_size);
    if (bytes_written != (ssize_t)buf_size)
    {
        fprintf(stderr, "Error: Unable to write SSG group ID to file %s\n", file_name);
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    close(fd);
    free(buf);
    return SSG_SUCCESS;
}


int ssg_group_id_load(
    const char * file_name,
    ssg_group_id_t * group_id_p)
{
    int fd;
    struct stat fstats;
    char *buf;
    ssize_t bytes_read;
    int ret;

    *group_id_p = SSG_GROUP_ID_NULL;

    fd = open(file_name, O_RDONLY);
    if (fd < 0)
    {
        fprintf(stderr, "Error: Unable to open file %s for loading SSG group ID\n",
            file_name);
        return SSG_FAILURE;
    }

    ret = fstat(fd, &fstats);
    if (ret != 0)
    {
        fprintf(stderr, "Error: Unable to stat file %s\n", file_name);
        close(fd);
        return SSG_FAILURE;
    }
    if (fstats.st_size == 0)
    {
        fprintf(stderr, "Error: SSG group ID file %s is empty\n", file_name);
        close(fd);
        return SSG_FAILURE;
    }

    buf = malloc(fstats.st_size);
    if (buf == NULL)
    {
        close(fd);
        return SSG_FAILURE;
    }

    bytes_read = read(fd, buf, fstats.st_size);
    if (bytes_read != (ssize_t)fstats.st_size)
    {
        fprintf(stderr, "Error: Unable to read SSG group ID from file %s\n", file_name);
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    ssg_group_id_deserialize(buf, (size_t)bytes_read, group_id_p);
    if (*group_id_p == SSG_GROUP_ID_NULL)
    {
        fprintf(stderr, "Error: Unable to deserialize SSG group ID\n");
        close(fd);
        free(buf);
        return SSG_FAILURE;
    }

    close(fd);
    free(buf);
    return SSG_SUCCESS;
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
void ssg_group_dump(
    ssg_group_id_t group_id)
{
    ssg_group_descriptor_t *group_descriptor = (ssg_group_descriptor_t *)group_id;
    ssg_group_view_t *group_view = NULL;
    char *group_name = NULL;
    char group_role[32];
    char group_self_id[32];

    if (group_descriptor->owner_status == SSG_OWNER_IS_MEMBER)
    {
        ssg_group_t *g;

        HASH_FIND(hh, ssg_inst->group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), g);
        if (g)
        {
            group_view = &g->view;
            group_name = g->name;
            strcpy(group_role, "member");
            sprintf(group_self_id, "%"PRIu64, g->self_id);
        }
    }
    else if (group_descriptor->owner_status == SSG_OWNER_IS_ATTACHER)
    {
        ssg_attached_group_t *ag;

        HASH_FIND(hh, ssg_inst->attached_group_table, &group_descriptor->name_hash,
            sizeof(uint64_t), ag);
        if (ag)
        {
            group_view = &ag->view;
            group_name = ag->name;
            strcpy(group_role, "attacher");
        }
    }
    else
    {
        fprintf(stderr, "Error: SSG can only dump membership information for" \
            " groups that the caller is a member of or an attacher of\n");
        return;
    }

    if (group_view)
    {
        unsigned int i;

        printf("SSG membership information for group '%s':\n", group_name);
        printf("\trole: '%s'\n", group_role);
        if (strcmp(group_role, "member") == 0)
            printf("\tself_id: %s\n", group_self_id);
Shane Snyder's avatar
Shane Snyder committed
899
        printf("\tsize: %d\n", group_view->size);
900 901
        printf("\tview:\n");
        for (i = 0; i < group_view->size; i++)
902 903 904 905 906
        {
            if (group_view->member_states[i].is_member)
                printf("\t\tid: %d\taddr: %s\n", i,
                    group_view->member_states[i].addr_str);
        }
907 908 909 910
    }
    else
        fprintf(stderr, "Error: SSG unable to find group view associated" \
            " with the given group ID\n");
911

912
    return;
Shane Snyder's avatar
Shane Snyder committed
913 914
}

915 916 917
/************************************
 *** SSG internal helper routines ***
 ************************************/
918

919 920 921 922
void ssg_apply_membership_update(
    ssg_group_t *g,
    ssg_membership_update_t update)
{
923 924
    if(!ssg_inst || !g) return;

925 926
    if (update.type == SSG_MEMBER_REMOVE)
    {
927
        margo_addr_free(ssg_inst->mid, g->view.member_states[update.member].addr);
928 929
        free(g->view.member_states[update.member].addr_str);
        g->view.member_states[update.member].addr_str = NULL;
930
        g->view.member_states[update.member].is_member = 0;
931
        /* XXX: need to update size ... g->view.size--; */
932 933 934 935 936 937 938 939 940 941 942 943 944
    }
    else
    {
        assert(0); /* XXX: dynamic group joins aren't possible yet */
    }

    /* execute user-supplied membership update callback, if given */
    if (g->update_cb)
        g->update_cb(update, g->update_cb_dat);

    return;
}

945
static ssg_group_descriptor_t * ssg_group_descriptor_create(
946
    uint64_t name_hash, const char * leader_addr_str, int owner_status)
947
{
948
    ssg_group_descriptor_t *descriptor;
949

950 951
    descriptor = malloc(sizeof(*descriptor));
    if (!descriptor) return NULL;
Shane Snyder's avatar
Shane Snyder committed
952

953
    /* hash the group name to obtain an 64-bit unique ID */
954
    descriptor->magic_nr = SSG_MAGIC_NR;
955
    descriptor->name_hash = name_hash;
956 957
    descriptor->addr_str = strdup(leader_addr_str);
    if (!descriptor->addr_str)
Shane Snyder's avatar
Shane Snyder committed
958
    {
959
        free(descriptor);
Shane Snyder's avatar
Shane Snyder committed
960 961
        return NULL;
    }
962
    descriptor->owner_status = owner_status;
963
    descriptor->ref_count = 1;
964 965
    return descriptor;
}
966

967 968 969
static ssg_group_descriptor_t * ssg_group_descriptor_dup(
    ssg_group_descriptor_t * descriptor)
{
Shane Snyder's avatar
Shane Snyder committed
970 971
    descriptor->ref_count++;
    return descriptor;
972 973
}

974 975
static void ssg_group_descriptor_free(
    ssg_group_descriptor_t * descriptor)
976
{
977 978
    if (descriptor)
    {
Shane Snyder's avatar
Shane Snyder committed
979 980 981 982 983 984 985 986 987
        if(descriptor->ref_count == 1)
        {
            free(descriptor->addr_str);
            free(descriptor);
        }
        else
        {
            descriptor->ref_count--;
        }
988 989 990
    }
    return;
}
991

992
static int ssg_group_view_create(
993 994 995
    const char * const group_addr_strs[], int group_size,
    hg_addr_t self_addr, ssg_member_id_t * self_id,
    ssg_group_view_t * view)
996 997
{
    int i, j, r;
998 999 1000 1001
    ABT_thread *lookup_ults = NULL;
    struct ssg_group_lookup_ult_args *lookup_ult_args = NULL;
    char *self_addr_str = NULL;
    hg_size_t self_addr_str_size = 0;
1002 1003
    const char *self_addr_substr = NULL;
    const char *addr_substr = NULL;
1004 1005
    ssg_member_state_t *tmp_ms;
    hg_return_t hret;
1006
    int aret;
1007
    int sret = SSG_FAILURE;
1008

1009 1010 1011 1012
    if (self_id)
        *self_id = SSG_MEMBER_ID_INVALID;
        
    if ((self_id != NULL && self_addr == HG_ADDR_NULL) || !view) goto fini;
1013 1014 1015

    /* allocate lookup ULTs */
    lookup_ults = malloc(group_size * sizeof(*lookup_ults));
1016 1017
    if (lookup_ults == NULL) goto fini;
    for (i = 0; i < group_size; i++) lookup_ults[i] = ABT_THREAD_NULL;
1018
    lookup_ult_args = malloc(group_size * sizeof(*lookup_ult_args));
1019
    if (lookup_ult_args == NULL) goto fini;
Shane Snyder's avatar
Shane Snyder committed
1020

1021
    if(self_addr)
1022
    {
1023 1024 1025 1026 1027 1028
        hret = margo_addr_to_string(ssg_inst->mid, NULL, &self_addr_str_size, self_addr);
        if (hret != HG_SUCCESS) goto fini;
        self_addr_str = malloc(self_addr_str_size);
        if (self_addr_str == NULL) goto fini;
        hret = margo_addr_to_string(ssg_inst->mid, self_addr_str, &self_addr_str_size, self_addr);
        if (hret != HG_SUCCESS) goto fini;
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

        /* strstr is used here b/c there may be inconsistencies in whether the class
         * is included in the address or not (it should not be in HG_Addr_to_string,
         * but it's possible that it is in the list of group address strings)
         */
        self_addr_substr = strstr(self_addr_str, "://");
        if (self_addr_substr == NULL)
            self_addr_substr = self_addr_str;
        else
            self_addr_substr += 3;
    }

1041 1042
    /* construct view using ULTs to lookup the address of each group member */
    view->size = group_size;
1043 1044 1045 1046 1047 1048 1049 1050
    r = rand() % view->size;
    for (i = 0; i < group_size; i++)
    {
        /* randomize our starting index so all group members aren't looking
         * up other group members in the same order
         */
        j = (r + i) % view->size;

1051 1052
        if (group_addr_strs[j] == NULL || strlen(group_addr_strs[j]) == 0) continue;

1053 1054 1055 1056 1057 1058 1059
        tmp_ms = malloc(sizeof(*tmp_ms));
        if (!tmp_ms) goto fini;

        /* generate a unique member ID for this address */
        tmp_ms->id = ssg_gen_member_id(group_addr_strs[j]);
        tmp_ms->addr_str = strdup(group_addr_strs[j]);
        if (!tmp_ms->addr_str)
1060
        {
1061 1062
            free(tmp_ms);
            goto fini;
1063
        }
1064
        
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
        /* resolve self id in group if caller asked for it */
        if (self_addr_substr)
        {
            addr_substr = strstr(group_addr_strs[j], "://");
            if (addr_substr == NULL)
                addr_substr = group_addr_strs[j];
            else
                addr_substr += 3;

            if (strcmp(self_addr_substr, addr_substr) == 0)
            {
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
                hret = margo_addr_dup(ssg_inst->mid, self_addr, &tmp_ms->addr);
                if (hret != HG_SUCCESS)
                {
                    free(tmp_ms->addr_str);
                    free(tmp_ms);
                    goto fini;
                }

                if (self_id)               
                    *self_id = tmp_ms->id;

                /* add self state to membership view */
                LL_PREPEND(view->member_list, tmp_ms);
                HASH_ADD(hh, view->member_map, id, sizeof(ssg_member_id_t),

                /* don't look up our own address, we already know it */
                continue;
1093 1094 1095 1096
            }
        }

        /* XXX limit outstanding lookups to some max */
1097 1098
        lookup_ult_args[j].view = view;
        lookup_ult_args[j].member_state = tmp_ms;
1099 1100
        ABT_pool pool;
        margo_get_handler_pool(ssg_inst->mid, &pool);
1101 1102
        aret = ABT_thread_create(pool, &ssg_group_lookup_ult,
            &lookup_ult_args[j], ABT_THREAD_ATTR_NULL,
1103 1104 1105
            &lookup_ults[j]);
        if (aret != ABT_SUCCESS)
        {
1106 1107
            free(tmp_ms->addr_str);
            free(tmp_ms);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
            goto fini;
        }
    }

    /* wait on all lookup ULTs to terminate */
    for (i = 0; i < group_size; i++)
    {
        if (lookup_ults[i] == ABT_THREAD_NULL) continue;

        aret = ABT_thread_join(lookup_ults[i]);
        ABT_thread_free(&lookup_ults[i]);
        lookup_ults[i] = ABT_THREAD_NULL;
1120
        if (aret != ABT_SUCCESS) goto fini;
1121 1122 1123
        else if (lookup_ult_args[i].out != HG_SUCCESS)
        {
            fprintf(stderr, "Error: SSG unable to lookup HG address for member %d"
1124 1125 1126
                "(err=%d)\n", lookup_ult_args[i].member_state->id,
                lookup_ult_args[i].out);
            goto fini;
1127 1128 1129
        }
    }

1130 1131 1132
    /* clean exit */
    sret = SSG_SUCCESS;

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
fini:
    if (sret != SSG_SUCCESS)
    {
        for (i = 0; i < group_size; i++)
        {
            if (lookup_ults[i] != ABT_THREAD_NULL)
            {
                ABT_thread_cancel(lookup_ults[i]);
                ABT_thread_free(&lookup_ults[i]);
            }
        }
        ssg_group_view_destroy(view);
    }
    free(lookup_ults);
    free(lookup_ult_args);
1148
    free(self_addr_str);
1149 1150

    return sret;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
1151
}
1152

1153 1154 1155
static ssg_member_id_t ssg_gen_member_id(
    const char * addr_str)
{
1156 1157 1158 1159 1160 1161 1162 1163 1164
    char tmp[64] = {0};
    ssg_member_id_t id = (ssg_member_id_t)ssg_hash64_str(addr_str);
    while (id == SSG_MEMBER_ID_INVALID)
    {
        if (tmp[0] == 0) strncpy(tmp, addr_str, 63);
        tmp[0]++;
        id = (ssg_member_id_t)ssg_hash64_str(tmp);
    }
    return id;
1165 1166 1167
}


1168 1169 1170 1171 1172
static void ssg_group_lookup_ult(
    void * arg)
{
    struct ssg_group_lookup_ult_args *l = arg;

1173
    /* XXX: should be a timeout here? */
1174 1175
    l->out = margo_addr_lookup(ssg_inst->mid, l->member_state->addr_str,
        &l->member_state->addr);
1176 1177 1178 1179 1180 1181 1182
    if (l->out == HG_SUCCESS)
    {
        LL_APPEND(l->view->member_list, l->member_state);
        HASH_ADD(hh, l->view->member_map, id, sizeof(ssg_member_id_t),
            l->member_state);
    }

1183 1184 1185 1186 1187
    return;
}

static void ssg_group_view_destroy(
    ssg_group_view_t * view)
1188
{
1189
    ssg_member_state_t *state, *tmp;
1190

1191 1192
    /* destroy state for all group members */
    LL_FOREACH_SAFE(view->member_list, state, tmp)
1193
    {
1194 1195 1196 1197 1198
        LL_DELETE(view->member_list, state);
        HASH_DEL(view->member_map, state);
        free(state->addr_str);
        margo_addr_free(ssg_inst->mid,  state->addr);
        free(state);
1199
    }
1200 1201
    view->member_list = NULL;
    view->member_map = NULL;
1202 1203 1204 1205 1206 1207 1208

    return;
}

static void ssg_group_destroy_internal(
    ssg_group_t * g)
{
1209 1210 1211 1212 1213 1214 1215 1216 1217
    /* TODO: send a leave message to the group ? */

#ifdef SSG_USE_SWIM_FD
    /* free up failure detector state */
    if(g->fd_ctx)
        swim_finalize(g->fd_ctx);
#endif

    /* destroy group state */
1218
    ssg_group_view_destroy(&g->view);
Shane Snyder's avatar
Shane Snyder committed
1219
    g->descriptor->owner_status = SSG_OWNER_IS_UNASSOCIATED;
1220 1221
    ssg_group_descriptor_free(g->descriptor);
    free(g->name);
1222 1223
    free(g);

1224 1225 1226 1227 1228 1229
    return;
}

static void ssg_attached_group_destroy(
    ssg_attached_group_t * ag)
{
Shane Snyder's avatar
Shane Snyder committed
1230 1231
    ssg_group_view_destroy(&ag->view);
    ag->descriptor->owner_status = SSG_OWNER_IS_UNASSOCIATED;
1232 1233 1234 1235 1236 1237 1238 1239 1240
    ssg_group_descriptor_free(ag->descriptor);
    free(ag->name);
    free(ag);
    return;
}

static const char ** ssg_addr_str_buf_to_list(
    const char * buf, int num_addrs)
{
1241
    int i;
1242 1243 1244 1245
    const char **ret = malloc(num_addrs * sizeof(*ret));
    if (ret == NULL) return NULL;

    ret[0] = buf;
1246
    for (i = 1; i < num_addrs; i++)
1247 1248 1249 1250 1251
    {
        const char * a = ret[i-1];
        ret[i] = a + strlen(a) + 1;
    }
    return ret;
1252
}