/*---------------------------------------------------------------------------*/ int msg_api_init(struct msg_params *msg_params, size_t hdrlen, size_t datalen, int is_server) { const char *req_hdr = "hello world request header"; const char *req_data = "hello world request data"; const char *rsp_hdr = "hello world response header"; const char *rsp_data = "hello world response data"; const char *ptr; size_t len; int pagesize = sysconf(_SC_PAGESIZE); if (pagesize < 0) return -1; msg_params->g_hdr = NULL; msg_params->g_data = NULL; if (hdrlen) { msg_params->g_hdr = (uint8_t *)memalign(pagesize, hdrlen); if (!msg_params->g_hdr) goto cleanup; ptr = (is_server) ? rsp_hdr : req_hdr; len = strlen(ptr); if (hdrlen <= len) len = hdrlen - 1; if (len) strncpy((char *)msg_params->g_hdr, ptr, len); msg_params->g_hdr[len] = 0; } if (datalen) { datalen = ALIGNHUGEPAGE(datalen); msg_params->g_data = (uint8_t *)alloc_mem_buf(datalen, &msg_params->g_shmid); if (!msg_params->g_data) goto cleanup; ptr = (is_server) ? rsp_data : req_data; len = strlen(ptr); if (datalen <= len) len = datalen - 1; if (len) strncpy((char *)msg_params->g_data, ptr, len); msg_params->g_data[len] = 0; msg_params->g_data_mr = xio_reg_mr(msg_params->g_data, datalen); if (!msg_params->g_data_mr) goto cleanup; } return 0; cleanup: msg_api_free(msg_params); return -1; }
/*---------------------------------------------------------------------------*/ struct perf_buf *xio_buf_alloc(size_t size) { struct perf_buf * pbuf; pbuf = calloc(1, sizeof(*pbuf)); pbuf->addr = alloc_mem_buf(ALIGNHUGEPAGE(size) , &pbuf->shmid); pbuf->length = size; pbuf->mr = xio_reg_mr(pbuf->addr, pbuf->length); return pbuf; }
/*---------------------------------------------------------------------------*/ struct perf_buf *xio_buf_alloc(size_t size) { struct perf_buf *pbuf; struct xio_reg_mem reg_mem; pbuf = (struct perf_buf *)calloc(1, sizeof(*pbuf)); pbuf->addr = alloc_mem_buf(ALIGNHUGEPAGE(size) , &pbuf->shmid); pbuf->length = size; xio_mem_register(pbuf->addr, pbuf->length, ®_mem); pbuf->mr = reg_mem.mr; return pbuf; }
/*---------------------------------------------------------------------------*/ struct msg_pool *msg_pool_alloc(int max, size_t out_hdrlen, size_t out_datalen, size_t in_hdrlen, size_t in_datalen) { struct msg_pool *msg_pool; struct xio_msg *msg; size_t len; size_t hdrlen; size_t datalen; int i; uint8_t *buf; uint8_t *header; uint8_t *data; /* allocate the structures */ len = sizeof(struct msg_pool)+ max*(2*sizeof(struct xio_msg *)+sizeof(struct xio_msg)); buf = calloc(len, sizeof(uint8_t)); if (!buf) { fprintf(stderr, "Couldn't allocate message pool\n"); exit(1); } datalen = max*(out_datalen + in_datalen); hdrlen = max*(out_hdrlen + in_hdrlen); /* pool */ msg_pool = (struct msg_pool *)buf; buf = buf + sizeof(struct msg_pool); /* stack */ msg_pool->stack = (struct xio_msg **)buf; buf = buf + max * sizeof(struct xio_msg *); /* array */ msg_pool->array = (struct xio_msg **)buf; buf = buf + max * sizeof(struct xio_msg *); /* header */ if (hdrlen) { msg_pool->header = calloc(hdrlen, sizeof(uint8_t)); if (!buf) { fprintf(stderr, "Couldn't allocate message pool\n"); exit(1); } } /* data */ if (datalen) { datalen = ALIGNHUGEPAGE(datalen); msg_pool->data = alloc_mem_buf(datalen, &msg_pool->shmid); if (!msg_pool->data) { fprintf(stderr, "Couldn't allocate data buffers\n"); free(buf); exit(1); } memset(msg_pool->data, 0, datalen); msg_pool->mr = xio_reg_mr(msg_pool->data, datalen); } data = msg_pool->data; header = msg_pool->header; for (i = 0; i < max; i++) { msg_pool->array[i] = (struct xio_msg *)buf; buf = buf + sizeof(struct xio_msg); msg = msg_pool->array[i]; msg_pool->stack[i] = msg; if (out_hdrlen) { msg->out.header.iov_base = header; msg->out.header.iov_len = out_hdrlen; header = header + out_hdrlen; } if (out_datalen) { msg->out.data_iov[0].iov_base = data; msg->out.data_iov[0].iov_len = out_datalen; msg->out.data_iov[0].mr = msg_pool->mr; data = data + out_datalen; msg->out.data_iovlen = 1; } if (in_hdrlen) { msg->in.header.iov_base = header; msg->in.header.iov_len = in_hdrlen; header = header + in_hdrlen; } if (in_datalen) { msg->in.data_iov[0].iov_base = data; msg->in.data_iov[0].iov_len = in_datalen; msg->in.data_iov[0].mr = msg_pool->mr; data = data + in_datalen; msg->in.data_iovlen = 1; } } msg_pool->stack_ptr = msg_pool->stack; msg_pool->stack_end = msg_pool->stack_ptr + max; msg_pool->max = max; return msg_pool; }
/*---------------------------------------------------------------------------*/ int msg_api_init(struct msg_params *msg_params, size_t hdrlen, size_t datalen, int is_server) { int pagesize = sysconf(_SC_PAGESIZE); const char *req_hdr = "hello world request header"; const char *req_data = "hello world request data"; const char *rsp_hdr = "hello world response header"; const char *rsp_data = "hello world response data"; const char *ptr; int len, i; unsigned char c; msg_params->g_hdr = NULL; msg_params->g_data = NULL; if (hdrlen) { msg_params->g_hdr = memalign(pagesize, hdrlen); if (!msg_params->g_hdr) goto cleanup; ptr = (is_server) ? rsp_hdr : req_hdr; len = strlen(ptr); if (hdrlen < len) len = hdrlen; strncpy((char *)msg_params->g_hdr, ptr, len); msg_params->g_hdr[len] = 0; len++; for (i = len, c = 65; i < hdrlen; i++) { msg_params->g_hdr[i] = c; c++; if (c > 122) c = 65; } } if (datalen) { datalen = ALIGNHUGEPAGE(datalen); msg_params->g_data = alloc_mem_buf(datalen, &msg_params->g_shmid); if (!msg_params->g_data) goto cleanup; ptr = (is_server) ? rsp_data : req_data; len = strlen(ptr); if (datalen < len) len = datalen; strncpy((char *)msg_params->g_data, ptr, len); msg_params->g_data[len] = 0; len++; for (i = len, c = 65; i < datalen; i++) { msg_params->g_data[i] = c; c++; if (c > 122) c = 65; } msg_params->g_data_mr = xio_reg_mr(msg_params->g_data, datalen); } return 0; cleanup: if (msg_params->g_hdr) { free(msg_params->g_hdr); msg_params->g_hdr = NULL; } if (msg_params->g_data) { free_mem_buf(msg_params->g_data, msg_params->g_shmid); msg_params->g_data = NULL; } return -1; }