Commit | Line | Data |
---|---|---|
fc5c0345 DG |
1 | /* |
2 | * rbd engine | |
3 | * | |
4 | * IO engine using Ceph's librbd to test RADOS Block Devices. | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <rbd/librbd.h> | |
9 | ||
10 | #include "../fio.h" | |
11 | ||
12 | struct fio_rbd_iou { | |
13 | struct io_u *io_u; | |
14 | int io_complete; | |
15 | }; | |
16 | ||
17 | struct rbd_data { | |
18 | rados_t cluster; | |
19 | rados_ioctx_t io_ctx; | |
20 | rbd_image_t image; | |
21 | struct io_u **aio_events; | |
22 | }; | |
23 | ||
24 | struct rbd_options { | |
25 | struct thread_data *td; | |
26 | char *rbd_name; | |
27 | char *pool_name; | |
28 | char *client_name; | |
29 | }; | |
30 | ||
31 | static struct fio_option options[] = { | |
32 | { | |
33 | .name = "rbdname", | |
34 | .lname = "rbd engine rbdname", | |
35 | .type = FIO_OPT_STR_STORE, | |
36 | .help = "RBD name for RBD engine", | |
37 | .off1 = offsetof(struct rbd_options, rbd_name), | |
38 | .category = FIO_OPT_C_ENGINE, | |
39 | .group = FIO_OPT_G_RBD, | |
40 | }, | |
41 | { | |
42 | .name = "pool", | |
43 | .lname = "rbd engine pool", | |
44 | .type = FIO_OPT_STR_STORE, | |
45 | .help = "Name of the pool hosting the RBD for the RBD engine", | |
46 | .off1 = offsetof(struct rbd_options, pool_name), | |
47 | .category = FIO_OPT_C_ENGINE, | |
48 | .group = FIO_OPT_G_RBD, | |
49 | }, | |
50 | { | |
51 | .name = "clientname", | |
52 | .lname = "rbd engine clientname", | |
53 | .type = FIO_OPT_STR_STORE, | |
54 | .help = "Name of the ceph client to access the RBD for the RBD engine", | |
55 | .off1 = offsetof(struct rbd_options, client_name), | |
56 | .category = FIO_OPT_C_ENGINE, | |
57 | .group = FIO_OPT_G_RBD, | |
58 | }, | |
59 | { | |
60 | .name = NULL, | |
61 | }, | |
62 | }; | |
63 | ||
64 | static int _fio_setup_rbd_data(struct thread_data *td, | |
65 | struct rbd_data **rbd_data_ptr) | |
66 | { | |
67 | struct rbd_data *rbd_data; | |
68 | ||
69 | if (td->io_ops->data) | |
70 | return 0; | |
71 | ||
72 | rbd_data = malloc(sizeof(struct rbd_data)); | |
73 | if (!rbd_data) | |
74 | goto failed; | |
75 | ||
76 | memset(rbd_data, 0, sizeof(struct rbd_data)); | |
77 | ||
78 | rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *)); | |
79 | if (!rbd_data->aio_events) | |
80 | goto failed; | |
81 | ||
82 | memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *)); | |
83 | ||
84 | *rbd_data_ptr = rbd_data; | |
85 | ||
86 | return 0; | |
87 | ||
88 | failed: | |
89 | return 1; | |
90 | ||
91 | } | |
92 | ||
93 | static int _fio_rbd_connect(struct thread_data *td) | |
94 | { | |
95 | struct rbd_data *rbd_data = td->io_ops->data; | |
96 | struct rbd_options *o = td->eo; | |
97 | int r; | |
98 | ||
99 | r = rados_create(&(rbd_data->cluster), o->client_name); | |
100 | if (r < 0) { | |
101 | log_err("rados_create failed.\n"); | |
102 | goto failed_early; | |
103 | } | |
104 | ||
105 | r = rados_conf_read_file(rbd_data->cluster, NULL); | |
106 | if (r < 0) { | |
107 | log_err("rados_conf_read_file failed.\n"); | |
108 | goto failed_early; | |
109 | } | |
110 | ||
111 | r = rados_connect(rbd_data->cluster); | |
112 | if (r < 0) { | |
113 | log_err("rados_connect failed.\n"); | |
114 | goto failed_shutdown; | |
115 | } | |
116 | ||
117 | r = rados_ioctx_create(rbd_data->cluster, o->pool_name, | |
118 | &(rbd_data->io_ctx)); | |
119 | if (r < 0) { | |
120 | log_err("rados_ioctx_create failed.\n"); | |
121 | goto failed_shutdown; | |
122 | } | |
123 | ||
124 | r = rbd_open(rbd_data->io_ctx, o->rbd_name, &(rbd_data->image), | |
125 | NULL /*snap */ ); | |
126 | if (r < 0) { | |
127 | log_err("rbd_open failed.\n"); | |
128 | goto failed_open; | |
129 | } | |
130 | return 0; | |
131 | ||
132 | failed_open: | |
133 | rados_ioctx_destroy(rbd_data->io_ctx); | |
086f9935 | 134 | rbd_data->io_ctx = NULL; |
fc5c0345 DG |
135 | failed_shutdown: |
136 | rados_shutdown(rbd_data->cluster); | |
086f9935 | 137 | rbd_data->cluster = NULL; |
fc5c0345 DG |
138 | failed_early: |
139 | return 1; | |
140 | } | |
141 | ||
142 | static void _fio_rbd_disconnect(struct rbd_data *rbd_data) | |
143 | { | |
144 | if (!rbd_data) | |
145 | return; | |
146 | ||
147 | /* shutdown everything */ | |
148 | if (rbd_data->image) { | |
149 | rbd_close(rbd_data->image); | |
150 | rbd_data->image = NULL; | |
151 | } | |
152 | ||
153 | if (rbd_data->io_ctx) { | |
154 | rados_ioctx_destroy(rbd_data->io_ctx); | |
155 | rbd_data->io_ctx = NULL; | |
156 | } | |
157 | ||
158 | if (rbd_data->cluster) { | |
159 | rados_shutdown(rbd_data->cluster); | |
160 | rbd_data->cluster = NULL; | |
161 | } | |
162 | } | |
163 | ||
164 | static void _fio_rbd_finish_write_aiocb(rbd_completion_t comp, void *data) | |
165 | { | |
166 | struct io_u *io_u = (struct io_u *)data; | |
167 | struct fio_rbd_iou *fio_rbd_iou = | |
168 | (struct fio_rbd_iou *)io_u->engine_data; | |
169 | ||
170 | fio_rbd_iou->io_complete = 1; | |
171 | ||
172 | /* if write needs to be verified - we should not release comp here | |
173 | without fetching the result */ | |
174 | ||
175 | rbd_aio_release(comp); | |
176 | /* TODO handle error */ | |
177 | ||
178 | return; | |
179 | } | |
180 | ||
181 | static void _fio_rbd_finish_read_aiocb(rbd_completion_t comp, void *data) | |
182 | { | |
183 | struct io_u *io_u = (struct io_u *)data; | |
184 | struct fio_rbd_iou *fio_rbd_iou = | |
185 | (struct fio_rbd_iou *)io_u->engine_data; | |
186 | ||
187 | fio_rbd_iou->io_complete = 1; | |
188 | ||
189 | /* if read needs to be verified - we should not release comp here | |
190 | without fetching the result */ | |
191 | rbd_aio_release(comp); | |
192 | ||
193 | /* TODO handle error */ | |
194 | ||
195 | return; | |
196 | } | |
197 | ||
82340a9f HW |
198 | static void _fio_rbd_finish_sync_aiocb(rbd_completion_t comp, void *data) |
199 | { | |
200 | struct io_u *io_u = (struct io_u *)data; | |
201 | struct fio_rbd_iou *fio_rbd_iou = | |
202 | (struct fio_rbd_iou *)io_u->engine_data; | |
203 | ||
204 | fio_rbd_iou->io_complete = 1; | |
205 | ||
206 | /* if sync needs to be verified - we should not release comp here | |
207 | without fetching the result */ | |
208 | rbd_aio_release(comp); | |
209 | ||
210 | /* TODO handle error */ | |
211 | ||
212 | return; | |
213 | } | |
214 | ||
fc5c0345 DG |
215 | static struct io_u *fio_rbd_event(struct thread_data *td, int event) |
216 | { | |
217 | struct rbd_data *rbd_data = td->io_ops->data; | |
218 | ||
219 | return rbd_data->aio_events[event]; | |
220 | } | |
221 | ||
222 | static int fio_rbd_getevents(struct thread_data *td, unsigned int min, | |
223 | unsigned int max, struct timespec *t) | |
224 | { | |
225 | struct rbd_data *rbd_data = td->io_ops->data; | |
226 | unsigned int events = 0; | |
227 | struct io_u *io_u; | |
228 | int i; | |
229 | struct fio_rbd_iou *fov; | |
230 | ||
231 | do { | |
232 | io_u_qiter(&td->io_u_all, io_u, i) { | |
233 | if (!(io_u->flags & IO_U_F_FLIGHT)) | |
234 | continue; | |
235 | ||
236 | fov = (struct fio_rbd_iou *)io_u->engine_data; | |
237 | ||
238 | if (fov->io_complete) { | |
239 | fov->io_complete = 0; | |
240 | rbd_data->aio_events[events] = io_u; | |
241 | events++; | |
242 | } | |
243 | ||
244 | } | |
245 | if (events < min) | |
246 | usleep(100); | |
247 | else | |
248 | break; | |
249 | ||
250 | } while (1); | |
251 | ||
252 | return events; | |
253 | } | |
254 | ||
255 | static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u) | |
256 | { | |
257 | int r = -1; | |
258 | struct rbd_data *rbd_data = td->io_ops->data; | |
259 | rbd_completion_t comp; | |
260 | ||
261 | fio_ro_check(td, io_u); | |
262 | ||
263 | if (io_u->ddir == DDIR_WRITE) { | |
264 | r = rbd_aio_create_completion(io_u, | |
265 | (rbd_callback_t) | |
266 | _fio_rbd_finish_write_aiocb, | |
267 | &comp); | |
268 | if (r < 0) { | |
269 | log_err | |
270 | ("rbd_aio_create_completion for DDIR_WRITE failed.\n"); | |
271 | goto failed; | |
272 | } | |
273 | ||
274 | r = rbd_aio_write(rbd_data->image, io_u->offset, | |
275 | io_u->xfer_buflen, io_u->xfer_buf, comp); | |
276 | if (r < 0) { | |
277 | log_err("rbd_aio_write failed.\n"); | |
278 | goto failed; | |
279 | } | |
280 | ||
281 | } else if (io_u->ddir == DDIR_READ) { | |
282 | r = rbd_aio_create_completion(io_u, | |
283 | (rbd_callback_t) | |
284 | _fio_rbd_finish_read_aiocb, | |
285 | &comp); | |
286 | if (r < 0) { | |
287 | log_err | |
288 | ("rbd_aio_create_completion for DDIR_READ failed.\n"); | |
289 | goto failed; | |
290 | } | |
291 | ||
292 | r = rbd_aio_read(rbd_data->image, io_u->offset, | |
293 | io_u->xfer_buflen, io_u->xfer_buf, comp); | |
294 | ||
295 | if (r < 0) { | |
296 | log_err("rbd_aio_read failed.\n"); | |
297 | goto failed; | |
298 | } | |
299 | ||
300 | } else if (io_u->ddir == DDIR_SYNC) { | |
82340a9f HW |
301 | r = rbd_aio_create_completion(io_u, |
302 | (rbd_callback_t) | |
303 | _fio_rbd_finish_sync_aiocb, | |
304 | &comp); | |
305 | if (r < 0) { | |
306 | log_err | |
307 | ("rbd_aio_create_completion for DDIR_SYNC failed.\n"); | |
308 | goto failed; | |
309 | } | |
310 | ||
311 | r = rbd_aio_flush(rbd_data->image, comp); | |
fc5c0345 DG |
312 | if (r < 0) { |
313 | log_err("rbd_flush failed.\n"); | |
314 | goto failed; | |
315 | } | |
316 | ||
fc5c0345 DG |
317 | } else { |
318 | dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__, | |
319 | io_u->ddir); | |
320 | return FIO_Q_COMPLETED; | |
321 | } | |
322 | ||
323 | return FIO_Q_QUEUED; | |
324 | ||
325 | failed: | |
326 | io_u->error = r; | |
327 | td_verror(td, io_u->error, "xfer"); | |
328 | return FIO_Q_COMPLETED; | |
329 | } | |
330 | ||
331 | static int fio_rbd_init(struct thread_data *td) | |
332 | { | |
333 | int r; | |
334 | ||
335 | r = _fio_rbd_connect(td); | |
336 | if (r) { | |
337 | log_err("fio_rbd_connect failed, return code: %d .\n", r); | |
338 | goto failed; | |
339 | } | |
340 | ||
341 | return 0; | |
342 | ||
343 | failed: | |
344 | return 1; | |
345 | ||
346 | } | |
347 | ||
348 | static void fio_rbd_cleanup(struct thread_data *td) | |
349 | { | |
350 | struct rbd_data *rbd_data = td->io_ops->data; | |
351 | ||
352 | if (rbd_data) { | |
353 | _fio_rbd_disconnect(rbd_data); | |
354 | free(rbd_data->aio_events); | |
355 | free(rbd_data); | |
356 | } | |
357 | ||
358 | } | |
359 | ||
360 | static int fio_rbd_setup(struct thread_data *td) | |
361 | { | |
362 | int r = 0; | |
363 | rbd_image_info_t info; | |
364 | struct fio_file *f; | |
365 | struct rbd_data *rbd_data = NULL; | |
366 | int major, minor, extra; | |
367 | ||
368 | /* log version of librbd. No cluster connection required. */ | |
369 | rbd_version(&major, &minor, &extra); | |
370 | log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra); | |
371 | ||
372 | /* allocate engine specific structure to deal with librbd. */ | |
373 | r = _fio_setup_rbd_data(td, &rbd_data); | |
374 | if (r) { | |
375 | log_err("fio_setup_rbd_data failed.\n"); | |
376 | goto cleanup; | |
377 | } | |
378 | td->io_ops->data = rbd_data; | |
379 | ||
380 | /* librbd does not allow us to run first in the main thread and later in a | |
381 | * fork child. It needs to be the same process context all the time. | |
382 | */ | |
383 | td->o.use_thread = 1; | |
384 | ||
385 | /* connect in the main thread to determine to determine | |
386 | * the size of the given RADOS block device. And disconnect | |
387 | * later on. | |
388 | */ | |
389 | r = _fio_rbd_connect(td); | |
390 | if (r) { | |
391 | log_err("fio_rbd_connect failed.\n"); | |
392 | goto cleanup; | |
393 | } | |
394 | ||
395 | /* get size of the RADOS block device */ | |
396 | r = rbd_stat(rbd_data->image, &info, sizeof(info)); | |
397 | if (r < 0) { | |
398 | log_err("rbd_status failed.\n"); | |
399 | goto disconnect; | |
400 | } | |
401 | dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size); | |
402 | ||
403 | /* taken from "net" engine. Pretend we deal with files, | |
404 | * even if we do not have any ideas about files. | |
405 | * The size of the RBD is set instead of a artificial file. | |
406 | */ | |
407 | if (!td->files_index) { | |
5903e7b7 | 408 | add_file(td, td->o.filename ? : "rbd", 0, 0); |
fc5c0345 | 409 | td->o.nr_files = td->o.nr_files ? : 1; |
b53f2c54 | 410 | td->o.open_files++; |
fc5c0345 DG |
411 | } |
412 | f = td->files[0]; | |
413 | f->real_file_size = info.size; | |
414 | ||
415 | /* disconnect, then we were only connected to determine | |
416 | * the size of the RBD. | |
417 | */ | |
418 | _fio_rbd_disconnect(rbd_data); | |
419 | return 0; | |
420 | ||
421 | disconnect: | |
422 | _fio_rbd_disconnect(rbd_data); | |
423 | cleanup: | |
424 | fio_rbd_cleanup(td); | |
425 | return r; | |
426 | } | |
427 | ||
428 | static int fio_rbd_open(struct thread_data *td, struct fio_file *f) | |
429 | { | |
430 | return 0; | |
431 | } | |
432 | ||
d9b100fc JA |
433 | static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f) |
434 | { | |
435 | return 0; | |
436 | } | |
437 | ||
fc5c0345 DG |
438 | static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u) |
439 | { | |
440 | struct fio_rbd_iou *o = io_u->engine_data; | |
441 | ||
442 | if (o) { | |
443 | io_u->engine_data = NULL; | |
444 | free(o); | |
445 | } | |
446 | } | |
447 | ||
448 | static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u) | |
449 | { | |
450 | struct fio_rbd_iou *o; | |
451 | ||
452 | o = malloc(sizeof(*o)); | |
453 | o->io_complete = 0; | |
454 | o->io_u = io_u; | |
455 | io_u->engine_data = o; | |
456 | return 0; | |
457 | } | |
458 | ||
10aa136b | 459 | static struct ioengine_ops ioengine = { |
d9b100fc JA |
460 | .name = "rbd", |
461 | .version = FIO_IOOPS_VERSION, | |
462 | .setup = fio_rbd_setup, | |
463 | .init = fio_rbd_init, | |
464 | .queue = fio_rbd_queue, | |
465 | .getevents = fio_rbd_getevents, | |
466 | .event = fio_rbd_event, | |
467 | .cleanup = fio_rbd_cleanup, | |
468 | .open_file = fio_rbd_open, | |
469 | .invalidate = fio_rbd_invalidate, | |
470 | .options = options, | |
471 | .io_u_init = fio_rbd_io_u_init, | |
472 | .io_u_free = fio_rbd_io_u_free, | |
473 | .option_struct_size = sizeof(struct rbd_options), | |
fc5c0345 DG |
474 | }; |
475 | ||
476 | static void fio_init fio_rbd_register(void) | |
477 | { | |
478 | register_ioengine(&ioengine); | |
479 | } | |
480 | ||
481 | static void fio_exit fio_rbd_unregister(void) | |
482 | { | |
483 | unregister_ioengine(&ioengine); | |
484 | } |