t/run-fio-tests: integrate t/nvmept.py
[fio.git] / engines / nfs.c
1 #include <stdlib.h>
2 #include <poll.h>
3 #include <nfsc/libnfs.h>
4 #include <nfsc/libnfs-raw.h>
5 #include <nfsc/libnfs-raw-mount.h>
6
7 #include "../fio.h"
8 #include "../optgroup.h"
9
10 enum nfs_op_type {
11         NFS_READ_WRITE = 0,
12         NFS_STAT_MKDIR_RMDIR,
13         NFS_STAT_TOUCH_RM,
14 };
15
16 struct fio_libnfs_options {
17         struct nfs_context *context;
18         char *nfs_url;
19         /* nfs_callback needs this info, but doesn't have fio td structure to
20          * pull it from
21          */
22         unsigned int queue_depth;
23
24         /* the following implement a circular queue of outstanding IOs */
25
26         /* IOs issued to libnfs, that have not returned yet */
27         int outstanding_events;
28         /* event last returned via fio_libnfs_event */
29         int prev_requested_event_index;
30         int next_buffered_event; /* round robin-pointer within events[] */
31         int buffered_event_count; /* IOs completed by libnfs, waiting for FIO */
32         int free_event_buffer_index; /* next free buffer */
33         struct io_u**events;
34 };
35
36 struct nfs_data {
37         struct nfsfh *nfsfh;
38         struct fio_libnfs_options *options;
39 };
40
41 static struct fio_option options[] = {
42         {
43                 .name   = "nfs_url",
44                 .lname  = "nfs_url",
45                 .type   = FIO_OPT_STR_STORE,
46                 .help   = "URL in libnfs format, eg nfs://<server|ipv4|"
47                           "ipv6>/path[?arg=val[&arg=val]*]",
48                 .off1   = offsetof(struct fio_libnfs_options, nfs_url),
49                 .category = FIO_OPT_C_ENGINE,
50                 .group  = __FIO_OPT_G_NFS,
51         },
52         {
53                 .name     = NULL,
54         },
55 };
56
57 static struct io_u *fio_libnfs_event(struct thread_data *td, int event)
58 {
59         struct fio_libnfs_options *o = td->eo;
60         struct io_u *io_u = o->events[o->next_buffered_event];
61
62         assert(o->events[o->next_buffered_event]);
63         o->events[o->next_buffered_event] = NULL;
64         o->next_buffered_event = (o->next_buffered_event + 1) % td->o.iodepth;
65
66         /* validate our state machine */
67         assert(o->buffered_event_count);
68         o->buffered_event_count--;
69         assert(io_u);
70
71         /* assert that fio_libnfs_event is being called in sequential fashion */
72         assert(event == 0 || o->prev_requested_event_index + 1 == event);
73         if (o->buffered_event_count == 0)
74                 o->prev_requested_event_index = -1;
75         else
76                 o->prev_requested_event_index = event;
77         return io_u;
78 }
79
80 /*
81  * fio core logic seems to stop calling this event-loop if we ever return with
82  * 0 events
83  */
84 #define SHOULD_WAIT(td, o, flush)                       \
85         ((o)->outstanding_events == (td)->o.iodepth ||  \
86                 (flush && (o)->outstanding_events))
87
88 static int nfs_event_loop(struct thread_data *td, bool flush)
89 {
90         struct fio_libnfs_options *o = td->eo;
91         struct pollfd pfds[1]; /* nfs:0 */
92
93         /* we already have stuff queued for fio, no need to waste cpu on poll() */
94         if (o->buffered_event_count)
95                 return o->buffered_event_count;
96
97         do {
98                 int timeout = SHOULD_WAIT(td, o, flush) ? -1 : 0;
99                 int ret = 0;
100
101                 pfds[0].fd = nfs_get_fd(o->context);
102                 pfds[0].events = nfs_which_events(o->context);
103                 ret = poll(&pfds[0], 1, timeout);
104                 if (ret < 0) {
105                         if (errno == EINTR || errno == EAGAIN)
106                                 continue;
107                         log_err("nfs: failed to poll events: %s\n", strerror(errno));
108                         break;
109                 }
110
111                 ret = nfs_service(o->context, pfds[0].revents);
112                 if (ret < 0) {
113                         log_err("nfs: socket is in an unrecoverable error state.\n");
114                         break;
115                 }
116         } while (SHOULD_WAIT(td, o, flush));
117
118         return o->buffered_event_count;
119 }
120
121 static int fio_libnfs_getevents(struct thread_data *td, unsigned int min,
122                                 unsigned int max, const struct timespec *t)
123 {
124         return nfs_event_loop(td, false);
125 }
126
127 static void nfs_callback(int res, struct nfs_context *nfs, void *data,
128                          void *private_data)
129 {
130         struct io_u *io_u = private_data;
131         struct nfs_data *nfs_data = io_u->file->engine_data;
132         struct fio_libnfs_options *o = nfs_data->options;
133         if (res < 0) {
134                 log_err("Failed NFS operation(code:%d): %s\n", res,
135                                                 nfs_get_error(o->context));
136                 io_u->error = -res;
137                 /* res is used for read math below, don't want to pass negative
138                  * there
139                  */
140                 res = 0;
141         } else if (io_u->ddir == DDIR_READ) {
142                 memcpy(io_u->buf, data, res);
143                 if (res == 0)
144                         log_err("Got NFS EOF, this is probably not expected\n");
145         }
146         /* fio uses resid to track remaining data */
147         io_u->resid = io_u->xfer_buflen - res;
148
149         assert(!o->events[o->free_event_buffer_index]);
150         o->events[o->free_event_buffer_index] = io_u;
151         o->free_event_buffer_index = (o->free_event_buffer_index + 1) % o->queue_depth;
152         o->outstanding_events--;
153         o->buffered_event_count++;
154 }
155
156 static int queue_write(struct fio_libnfs_options *o, struct io_u *io_u)
157 {
158         struct nfs_data *nfs_data = io_u->engine_data;
159
160         return nfs_pwrite_async(o->context, nfs_data->nfsfh, io_u->offset,
161                                 io_u->buflen, io_u->buf, nfs_callback, io_u);
162 }
163
164 static int queue_read(struct fio_libnfs_options *o, struct io_u *io_u)
165 {
166         struct nfs_data *nfs_data = io_u->engine_data;
167
168         return nfs_pread_async(o->context, nfs_data->nfsfh, io_u->offset,
169                                 io_u->buflen, nfs_callback, io_u);
170 }
171
172 static enum fio_q_status fio_libnfs_queue(struct thread_data *td,
173                                           struct io_u *io_u)
174 {
175         struct nfs_data *nfs_data = io_u->file->engine_data;
176         struct fio_libnfs_options *o = nfs_data->options;
177         struct nfs_context *nfs = o->context;
178         enum fio_q_status ret = FIO_Q_QUEUED;
179         int err;
180
181         io_u->engine_data = nfs_data;
182         switch (io_u->ddir) {
183         case DDIR_WRITE:
184                 err = queue_write(o, io_u);
185                 break;
186         case DDIR_READ:
187                 err = queue_read(o, io_u);
188                 break;
189         case DDIR_TRIM:
190                 log_err("nfs: trim is not supported");
191                 err = -1;
192                 break;
193         default:
194                 log_err("nfs: unhandled io %d\n", io_u->ddir);
195                 err = -1;
196         }
197         if (err) {
198                 log_err("nfs: Failed to queue nfs op: %s\n", nfs_get_error(nfs));
199                 td->error = 1;
200                 return FIO_Q_COMPLETED;
201         }
202         o->outstanding_events++;
203         return ret;
204 }
205
206 /*
207  * Do a mount if one has not been done before 
208  */
209 static int do_mount(struct thread_data *td, const char *url)
210 {
211         size_t event_size = sizeof(struct io_u **) * td->o.iodepth;
212         struct fio_libnfs_options *options = td->eo;
213         struct nfs_url *nfs_url = NULL;
214         int ret = 0;
215         int path_len = 0;
216         char *mnt_dir = NULL;
217
218         if (options->context)
219                 return 0;
220
221         options->context = nfs_init_context();
222         if (!options->context) {
223                 log_err("nfs: failed to init nfs context\n");
224                 return -1;
225         }
226
227         options->events = calloc(1, event_size);
228
229         options->prev_requested_event_index = -1;
230         options->queue_depth = td->o.iodepth;
231
232         nfs_url = nfs_parse_url_full(options->context, url);
233         path_len = strlen(nfs_url->path);
234         mnt_dir = malloc(path_len + strlen(nfs_url->file) + 1);
235         strcpy(mnt_dir, nfs_url->path);
236         strcpy(mnt_dir + strlen(nfs_url->path), nfs_url->file);
237         ret = nfs_mount(options->context, nfs_url->server, mnt_dir);
238         free(mnt_dir);
239         nfs_destroy_url(nfs_url);
240         return ret;
241 }
242
243 static int fio_libnfs_setup(struct thread_data *td)
244 {
245         /* Using threads with libnfs causes fio to hang on exit, lower
246          * performance
247          */
248         td->o.use_thread = 0;
249         return 0;
250 }
251
252 static void fio_libnfs_cleanup(struct thread_data *td)
253 {
254         struct fio_libnfs_options *o = td->eo;
255
256         nfs_umount(o->context);
257         nfs_destroy_context(o->context);
258         free(o->events);
259 }
260
261 static int fio_libnfs_open(struct thread_data *td, struct fio_file *f)
262 {
263         struct fio_libnfs_options *options = td->eo;
264         struct nfs_data *nfs_data = NULL;
265         int flags = 0;
266         int ret;
267
268         if (!options->nfs_url) {
269                 log_err("nfs: nfs_url is a required parameter\n");
270                 return -1;
271         }
272
273         ret = do_mount(td, options->nfs_url);
274
275         if (ret) {
276                 log_err("nfs: Failed to mount %s with code %d: %s\n",
277                         options->nfs_url, ret, nfs_get_error(options->context));
278                 return ret;
279         }
280         nfs_data = calloc(1, sizeof(struct nfs_data));
281         nfs_data->options = options;
282
283         if (td->o.td_ddir == TD_DDIR_WRITE)
284                 flags |= O_CREAT | O_RDWR;
285         else
286                 flags |= O_RDWR;
287
288         ret = nfs_open(options->context, f->file_name, flags, &nfs_data->nfsfh);
289
290         if (ret)
291                 log_err("Failed to open %s: %s\n", f->file_name,
292                                         nfs_get_error(options->context));
293         f->engine_data = nfs_data;
294         return ret;
295 }
296
297 static int fio_libnfs_close(struct thread_data *td, struct fio_file *f)
298 {
299         struct nfs_data *nfs_data = f->engine_data;
300         struct fio_libnfs_options *o = nfs_data->options;
301         int ret = 0;
302
303         if (nfs_data->nfsfh)
304                 ret = nfs_close(o->context, nfs_data->nfsfh);
305
306         free(nfs_data);
307         f->engine_data = NULL;
308         return ret;
309 }
310
311 struct ioengine_ops ioengine = {
312         .name           = "nfs",
313         .version        = FIO_IOOPS_VERSION,
314         .setup          = fio_libnfs_setup,
315         .queue          = fio_libnfs_queue,
316         .getevents      = fio_libnfs_getevents,
317         .event          = fio_libnfs_event,
318         .cleanup        = fio_libnfs_cleanup,
319         .open_file      = fio_libnfs_open,
320         .close_file     = fio_libnfs_close,
321         .flags          = FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
322         .options        = options,
323         .option_struct_size     = sizeof(struct fio_libnfs_options),
324 };
325
326 static void fio_init fio_nfs_register(void)
327 {
328         register_ioengine(&ioengine);
329 }
330
331 static void fio_exit fio_nfs_unregister(void)
332 {
333         unregister_ioengine(&ioengine);
334 }