void test_io(rados_ioctx_t io, rbd_image_t image) { char test_data[TEST_IO_SIZE + 1]; int i; for (i = 0; i < TEST_IO_SIZE; ++i) { test_data[i] = (char) (rand() % (126 - 33) + 33); } test_data[TEST_IO_SIZE] = '\0'; for (i = 0; i < 5; ++i) write_test_data(image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE); for (i = 5; i < 10; ++i) aio_write_test_data(image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE); for (i = 0; i < 5; ++i) read_test_data(image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE); for (i = 5; i < 10; ++i) aio_read_test_data(image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE); rbd_image_info_t info; rbd_completion_t comp; assert(rbd_stat(image, &info, sizeof(info)) == 0); assert(rbd_write(image, info.size, 1, test_data) == -EINVAL); assert(rbd_read(image, info.size, 1, test_data) == -EINVAL); rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp); assert(rbd_aio_write(image, info.size, 1, test_data, comp) == -EINVAL); assert(rbd_aio_read(image, info.size, 1, test_data, comp) == -EINVAL); }
void aio_write_test_data(rbd_image_t image, const char *test_data, uint64_t off, size_t len) { rbd_completion_t comp; rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp); printf("created completion\n"); rbd_aio_write(image, off, len, test_data, comp); printf("started write\n"); rbd_aio_wait_for_complete(comp); int r = rbd_aio_get_return_value(comp); printf("return value is: %d\n", r); assert(r == 0); printf("finished write\n"); rbd_aio_release(comp); }
void aio_read_test_data(rbd_image_t image, const char *expected, uint64_t off, size_t len) { rbd_completion_t comp; char *result; assert((result = malloc(sizeof(result) * (len + 1))) != 0); rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp); printf("created completion\n"); rbd_aio_read(image, off, len, result, comp); printf("started read\n"); rbd_aio_wait_for_complete(comp); int r = rbd_aio_get_return_value(comp); printf("return value is: %d\n", r); assert(r == (ssize_t)len); rbd_aio_release(comp); printf("read: %s\nexpected: %s\n", result, expected); assert(memcmp(result, expected, len) == 0); free(result); }
static void app_run() { char *buf; int ret; rados_completion_t comp; buf = (char *)malloc(128); /** * rados 支持异步操作, 当执行大量 I/O 的时候, 不需要等待每一个操作完成, * 只需要传递一个回调函数给读写函数, 这样, 操作完成, librados 会自动条用 * 我们的回调函数. */ ret = rbd_aio_create_completion(buf, (rbd_callback_t)app_finish_aiocb, &comp); if (ret < 0) { fprintf(stderr, "could not create aio completion: %s\n", strerror(-ret)); return; } /** * 异步读的 API: rbd_aio_read(), 分别传入 image 的句柄, 读的开始位置 * 读多少字节, 存储的指针, 最后是上面的异步句柄 */ ret = rbd_aio_read(image, 0, 128, buf, comp); if (ret < 0) { fprintf(stderr, "error reading image:%s, %s\n", imagename, strerror(-ret)); return; } for (; ;) { if (done) { break; } /* Waiting for the reading operation to finish */ usleep(100000); } }
static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque, RBDAIOCmd cmd) { RBDAIOCB *acb; RADOSCB *rcb; rbd_completion_t c; int64_t off, size; char *buf; int r; BDRVRBDState *s = bs->opaque; acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); acb->cmd = cmd; acb->qiov = qiov; if (cmd == RBD_AIO_DISCARD) { acb->bounce = NULL; } else { acb->bounce = qemu_blockalign(bs, qiov->size); } acb->ret = 0; acb->error = 0; acb->s = s; acb->cancelled = 0; acb->bh = NULL; if (cmd == RBD_AIO_WRITE) { qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); } buf = acb->bounce; off = sector_num * BDRV_SECTOR_SIZE; size = nb_sectors * BDRV_SECTOR_SIZE; s->qemu_aio_count++; /* All the RADOSCB */ rcb = g_malloc(sizeof(RADOSCB)); rcb->done = 0; rcb->acb = acb; rcb->buf = buf; rcb->s = acb->s; rcb->size = size; r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); if (r < 0) { goto failed; } switch (cmd) { case RBD_AIO_WRITE: r = rbd_aio_write(s->image, off, size, buf, c); break; case RBD_AIO_READ: r = rbd_aio_read(s->image, off, size, buf, c); break; case RBD_AIO_DISCARD: r = rbd_aio_discard_wrapper(s->image, off, size, c); break; default: r = -EINVAL; } if (r < 0) { goto failed; } return &acb->common; failed: g_free(rcb); s->qemu_aio_count--; qemu_aio_release(acb); return NULL; }
static BlockAIOCB *rbd_start_aio(BlockDriverState *bs, int64_t off, QEMUIOVector *qiov, int64_t size, BlockCompletionFunc *cb, void *opaque, RBDAIOCmd cmd) { RBDAIOCB *acb; RADOSCB *rcb = NULL; rbd_completion_t c; int r; BDRVRBDState *s = bs->opaque; acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); acb->cmd = cmd; acb->qiov = qiov; assert(!qiov || qiov->size == size); rcb = g_new(RADOSCB, 1); if (!LIBRBD_USE_IOVEC) { if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { acb->bounce = NULL; } else { acb->bounce = qemu_try_blockalign(bs, qiov->size); if (acb->bounce == NULL) { goto failed; } } if (cmd == RBD_AIO_WRITE) { qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); } rcb->buf = acb->bounce; } acb->ret = 0; acb->error = 0; acb->s = s; rcb->acb = acb; rcb->s = acb->s; rcb->size = size; r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); if (r < 0) { goto failed; } switch (cmd) { case RBD_AIO_WRITE: #ifdef LIBRBD_SUPPORTS_IOVEC r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c); #else r = rbd_aio_write(s->image, off, size, rcb->buf, c); #endif break; case RBD_AIO_READ: #ifdef LIBRBD_SUPPORTS_IOVEC r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c); #else r = rbd_aio_read(s->image, off, size, rcb->buf, c); #endif break; case RBD_AIO_DISCARD: r = rbd_aio_discard_wrapper(s->image, off, size, c); break; case RBD_AIO_FLUSH: r = rbd_aio_flush_wrapper(s->image, c); break; default: r = -EINVAL; } if (r < 0) { goto failed_completion; } return &acb->common; failed_completion: rbd_aio_release(c); failed: g_free(rcb); if (!LIBRBD_USE_IOVEC) { qemu_vfree(acb->bounce); } qemu_aio_unref(acb); return NULL; }
static BlockDriverAIOCB *rbd_aio_rw_vector(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque, int write) { RBDAIOCB *acb; RADOSCB *rcb; rbd_completion_t c; int64_t off, size; char *buf; int r; BDRVRBDState *s = bs->opaque; acb = qemu_aio_get(&rbd_aio_pool, bs, cb, opaque); if (!acb) { return NULL; } acb->write = write; acb->qiov = qiov; acb->bounce = qemu_blockalign(bs, qiov->size); acb->ret = 0; acb->error = 0; acb->s = s; acb->cancelled = 0; acb->bh = NULL; if (write) { qemu_iovec_to_buffer(acb->qiov, acb->bounce); } buf = acb->bounce; off = sector_num * BDRV_SECTOR_SIZE; size = nb_sectors * BDRV_SECTOR_SIZE; s->qemu_aio_count++; /* All the RADOSCB */ rcb = g_malloc(sizeof(RADOSCB)); rcb->done = 0; rcb->acb = acb; rcb->buf = buf; rcb->s = acb->s; rcb->size = size; r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); if (r < 0) { goto failed; } if (write) { r = rbd_aio_write(s->image, off, size, buf, c); } else { r = rbd_aio_read(s->image, off, size, buf, c); } if (r < 0) { goto failed; } return &acb->common; failed: g_free(rcb); s->qemu_aio_count--; qemu_aio_release(acb); return NULL; }
int main() { int ret,len = 0; rados_t cluster; char *buf, *out_buf = NULL; char *pool = "rbd"; char *rbd_name = "vol1"; int fd = -1; int i = 0; rados_completion_t read_comp, write_comp; rbd_image_t rbd_image; time_t start_t, end_t, total_t = 0; unsigned char *cipher_key = (unsigned char *) "7190c8bc27ac4a1bbe1ab1cf55cf3b097190c8bc27ac4a1bbe1ab1cf55cf3b09"; unsigned char *iv = (unsigned char *) "dcbfdd41e40f74a2"; ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); ret = rados_create(&cluster, NULL); if (ret) { printf("error creating rados_t object\n"); return -1; } ret = rados_conf_read_file(cluster,"/home/shishir/repos/ceph/src/ceph.conf"); if (ret) { printf("Error reading conf files \n"); return -1; } ret = rados_connect(cluster); if (ret) { printf("Could not connect to cluster\n"); return -1; } rados_ioctx_t io; ret = rados_ioctx_create(cluster, pool, &io); if (ret) { printf("Could not open connection to cluster\n"); return -1; } ret = rbd_open(io, rbd_name, &rbd_image, NULL); if (ret) { printf("Failed to open image %s\n",rbd_name); return -1; } posix_memalign((void**)&buf, 4096, 65536); if (!buf) { printf("Failed to malloc\n"); return -1; } fd = open("/tmp/input_file", O_RDONLY); if (fd > 0) { read(fd, buf, 65536); close(fd); } posix_memalign((void**)&out_buf, 4096, 65536); if (!out_buf) { printf("Failed to malloc\n"); return -1; } len = strlen((char*)buf); //printf("plain text length %lld\n",strlen((char*)buf)); //printf("===========plain text========\n"); //BIO_dump_fp(stdout,(const char*)buf,len); //printf("=================\n"); start_t = clock(); len = encrypt (buf, strlen((char*)buf), cipher_key, iv, out_buf); end_t = clock(); total_t = (double)(end_t - start_t)/ CLOCKS_PER_SEC; //printf("encrypted len is %d timetaken is %fsec\n",len,total_t); //printf("===========encrypted text========\n"); //BIO_dump_fp(stdout,(const char*)out_buf,len); //printf("=================\n"); fd = open("/tmp/encrypted_file", O_CREAT|O_RDWR); if (fd > 0) { write(fd, out_buf, 65536); close(fd); } ret = rbd_aio_create_completion(NULL, NULL, &write_comp); if (ret) { printf ("Failed to create aio completion\n"); return -1; } ret = rbd_aio_write(rbd_image, 0, 65536, out_buf, write_comp); if (ret < 0) { printf ("Failed to write aio completion\n"); return -1; } rbd_aio_wait_for_complete(write_comp); rbd_aio_release(write_comp); ret = rbd_aio_create_completion(NULL, NULL, &read_comp); if (ret) { printf ("Failed to create aio completion\n"); return -1; } memset(buf, 0, 65536); memset(out_buf, 0, 65536); ret = rbd_aio_read(rbd_image, 0, 65536, buf, read_comp); if (ret < 0) { printf ("Failed to read aio completion\n"); return -1; } rbd_aio_wait_for_complete(read_comp); rbd_aio_release(read_comp); len=65536; start_t = clock(); len = decrypt(buf, len, cipher_key, iv, out_buf); end_t = clock(); total_t = (double)(end_t - start_t)/ CLOCKS_PER_SEC; //printf("decrypted len is %d time taken: %fs\n",len,total_t); //printf("===========decrypted text========\n"); //BIO_dump_fp(stdout,(const char*)out_buf,len); //printf("=================\n"); fd = open("/tmp/decrypted_file", O_CREAT|O_RDWR); if (fd > 0) { write(fd, out_buf, 65536); close(fd); } rados_ioctx_destroy(io); rados_shutdown(cluster); free(buf); free(out_buf); return 0; }