[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [PATCH 09/24] libxl: provide libxl__datacopier_*
On Mon, 2012-04-16 at 18:17 +0100, Ian Jackson wrote: > General facility for ao operations to shovel data between fds. > > This will be used by the bootloader machinery. > > Signed-off-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx> Acked-by: Ian Campbell <ian.campbell@xxxxxxxxxx> > > Changes since v6 > * assert that the ao is non-null on _init. > --- > tools/libxl/Makefile | 3 +- > tools/libxl/libxl_aoutils.c | 189 > ++++++++++++++++++++++++++++++++++++++++++ > tools/libxl/libxl_internal.h | 40 +++++++++ > 3 files changed, 231 insertions(+), 1 deletions(-) > create mode 100644 tools/libxl/libxl_aoutils.c > > diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile > index 5ba144f..6e253b1 100644 > --- a/tools/libxl/Makefile > +++ b/tools/libxl/Makefile > @@ -52,7 +52,8 @@ LIBXL_LIBS += -lyajl > > LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ > libxl_dom.o libxl_exec.o libxl_xshelp.o > libxl_device.o \ > - libxl_internal.o libxl_utils.o libxl_uuid.o > libxl_json.o \ > + libxl_internal.o libxl_utils.o libxl_uuid.o \ > + libxl_json.o libxl_aoutils.o \ > libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y) > LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o > > diff --git a/tools/libxl/libxl_aoutils.c b/tools/libxl/libxl_aoutils.c > new file mode 100644 > index 0000000..4c60ad9 > --- /dev/null > +++ b/tools/libxl/libxl_aoutils.c > @@ -0,0 +1,189 @@ > +/* > + * Copyright (C) 2010 Citrix Ltd. > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU Lesser General Public License as published > + * by the Free Software Foundation; version 2.1 only. with the special > + * exception on linking described in file LICENSE. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU Lesser General Public License for more details. > + */ > + > +#include "libxl_osdeps.h" /* must come before any other headers */ > + > +#include "libxl_internal.h" > + > +/*----- data copier -----*/ > + > +void libxl__datacopier_init(libxl__datacopier_state *dc) > +{ > + assert(dc->ao); > + libxl__ev_fd_init(&dc->toread); > + libxl__ev_fd_init(&dc->towrite); > + LIBXL_TAILQ_INIT(&dc->bufs); > +} > + > +void libxl__datacopier_kill(libxl__datacopier_state *dc) > +{ > + STATE_AO_GC(dc->ao); > + libxl__datacopier_buf *buf, *tbuf; > + > + libxl__ev_fd_deregister(gc, &dc->toread); > + libxl__ev_fd_deregister(gc, &dc->towrite); > + LIBXL_TAILQ_FOREACH_SAFE(buf, &dc->bufs, entry, tbuf) > + free(buf); > + LIBXL_TAILQ_INIT(&dc->bufs); > +} > + > +static void datacopier_callback(libxl__egc *egc, libxl__datacopier_state *dc, > + int onwrite, int errnoval) > +{ > + libxl__datacopier_kill(dc); > + dc->callback(egc, dc, onwrite, errnoval); > +} > + > +static void datacopier_writable(libxl__egc *egc, libxl__ev_fd *ev, > + int fd, short events, short revents); > + > +static void datacopier_check_state(libxl__egc *egc, libxl__datacopier_state > *dc) > +{ > + STATE_AO_GC(dc->ao); > + int rc; > + > + if (dc->used) { > + if (!libxl__ev_fd_isregistered(&dc->towrite)) { > + rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable, > + dc->writefd, POLLOUT); > + if (rc) { > + LOG(ERROR, "unable to establish write event on %s" > + " during copy of %s", dc->writewhat, dc->copywhat); > + datacopier_callback(egc, dc, -1, 0); > + return; > + } > + } > + } else if (!libxl__ev_fd_isregistered(&dc->toread)) { > + /* we have had eof */ > + datacopier_callback(egc, dc, 0, 0); > + return; > + } else { > + /* nothing buffered, but still reading */ > + libxl__ev_fd_deregister(gc, &dc->towrite); > + } > +} > + > +static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev, > + int fd, short events, short revents) { > + libxl__datacopier_state *dc = CONTAINER_OF(ev, *dc, toread); > + STATE_AO_GC(dc->ao); > + > + if (revents & ~POLLIN) { > + LOG(ERROR, "unexpected poll event 0x%x (should be POLLIN)" > + " on %s during copy of %s", revents, dc->readwhat, dc->copywhat); > + datacopier_callback(egc, dc, -1, 0); > + return; > + } > + assert(revents & POLLIN); > + for (;;) { > + while (dc->used >= dc->maxsz) { > + libxl__datacopier_buf *rm = LIBXL_TAILQ_FIRST(&dc->bufs); > + dc->used -= rm->used; > + assert(dc->used >= 0); > + LIBXL_TAILQ_REMOVE(&dc->bufs, rm, entry); > + free(rm); > + } > + > + libxl__datacopier_buf *buf = > + LIBXL_TAILQ_LAST(&dc->bufs, libxl__datacopier_bufs); > + if (!buf || buf->used >= sizeof(buf->buf)) { > + buf = malloc(sizeof(*buf)); > + if (!buf) libxl__alloc_failed(CTX, __func__, 1, sizeof(*buf)); > + buf->used = 0; > + LIBXL_TAILQ_INSERT_TAIL(&dc->bufs, buf, entry); > + } > + int r = read(ev->fd, > + buf->buf + buf->used, > + sizeof(buf->buf) - buf->used); > + if (r < 0) { > + if (errno == EINTR) continue; > + if (errno == EWOULDBLOCK) break; > + LOGE(ERROR, "error reading %s during copy of %s", > + dc->readwhat, dc->copywhat); > + datacopier_callback(egc, dc, 0, errno); > + return; > + } > + if (r == 0) { > + libxl__ev_fd_deregister(gc, &dc->toread); > + break; > + } > + buf->used += r; > + dc->used += r; > + assert(buf->used <= sizeof(buf->buf)); > + } > + datacopier_check_state(egc, dc); > +} > + > +static void datacopier_writable(libxl__egc *egc, libxl__ev_fd *ev, > + int fd, short events, short revents) { > + libxl__datacopier_state *dc = CONTAINER_OF(ev, *dc, towrite); > + STATE_AO_GC(dc->ao); > + > + if (revents & ~POLLOUT) { > + LOG(ERROR, "unexpected poll event 0x%x (should be POLLOUT)" > + " on %s during copy of %s", revents, dc->writewhat, > dc->copywhat); > + datacopier_callback(egc, dc, -1, 0); > + return; > + } > + assert(revents & POLLOUT); > + for (;;) { > + libxl__datacopier_buf *buf = LIBXL_TAILQ_FIRST(&dc->bufs); > + if (!buf) > + break; > + if (!buf->used) { > + LIBXL_TAILQ_REMOVE(&dc->bufs, buf, entry); > + free(buf); > + continue; > + } > + int r = write(ev->fd, buf->buf, buf->used); > + if (r < 0) { > + if (errno == EINTR) continue; > + if (errno == EWOULDBLOCK) break; > + LOGE(ERROR, "error writing to %s during copy of %s", > + dc->writewhat, dc->copywhat); > + datacopier_callback(egc, dc, 1, errno); > + return; > + } > + assert(r > 0); > + assert(r <= buf->used); > + buf->used -= r; > + dc->used -= r; > + assert(dc->used >= 0); > + memmove(buf->buf, buf->buf+r, buf->used); > + } > + datacopier_check_state(egc, dc); > +} > + > +int libxl__datacopier_start(libxl__datacopier_state *dc) > +{ > + int rc; > + STATE_AO_GC(dc->ao); > + > + libxl__datacopier_init(dc); > + > + rc = libxl__ev_fd_register(gc, &dc->toread, datacopier_readable, > + dc->readfd, POLLIN); > + if (rc) goto out; > + > + rc = libxl__ev_fd_register(gc, &dc->towrite, datacopier_writable, > + dc->writefd, POLLOUT); > + if (rc) goto out; > + > + return 0; > + > + out: > + libxl__datacopier_kill(dc); > + return rc; > +} > + > diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h > index 6ceb362..20c95db 100644 > --- a/tools/libxl/libxl_internal.h > +++ b/tools/libxl/libxl_internal.h > @@ -833,6 +833,7 @@ _hidden int libxl__ev_devstate_wait(libxl__gc *gc, > libxl__ev_devstate *ds, > */ > _hidden int libxl__try_phy_backend(mode_t st_mode); > > + > /* from libxl_pci */ > > _hidden int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, > libxl_device_pci *pcidev, int starting); > @@ -1458,6 +1459,45 @@ int libxl__carefd_close(libxl__carefd*); > int libxl__carefd_fd(const libxl__carefd*); > > > +/*----- datacopier: copies data from one fd to another -----*/ > + > +typedef struct libxl__datacopier_state libxl__datacopier_state; > +typedef struct libxl__datacopier_buf libxl__datacopier_buf; > + > +/* onwrite==1 means failure happened when writing, logged, errnoval is valid > + * onwrite==0 means failure happened when reading > + * errnoval==0 means we got eof and all data was written > + * errnoval!=0 means we had a read error, logged > + * onwrite==-1 means some other internal failure, errnoval not valid, logged > + * in all cases copier is killed before calling this callback */ > +typedef void libxl__datacopier_callback(libxl__egc *egc, > + libxl__datacopier_state *dc, int onwrite, int errnoval); > + > +struct libxl__datacopier_buf { > + /* private to datacopier */ > + LIBXL_TAILQ_ENTRY(libxl__datacopier_buf) entry; > + int used; > + char buf[1000]; > +}; > + > +struct libxl__datacopier_state { > + /* caller must fill these in, and they must all remain valid */ > + libxl__ao *ao; > + int readfd, writefd; > + ssize_t maxsz; > + const char *copywhat, *readwhat, *writewhat; /* for error msgs */ > + libxl__datacopier_callback *callback; > + /* remaining fields are private to datacopier */ > + libxl__ev_fd toread, towrite; > + ssize_t used; > + LIBXL_TAILQ_HEAD(libxl__datacopier_bufs, libxl__datacopier_buf) bufs; > +}; > + > +_hidden void libxl__datacopier_init(libxl__datacopier_state *dc); > +_hidden void libxl__datacopier_kill(libxl__datacopier_state *dc); > +_hidden int libxl__datacopier_start(libxl__datacopier_state *dc); > + > + > /* > * Convenience macros. > */ > -- > 1.7.2.5 > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@xxxxxxxxxxxxx > http://lists.xen.org/xen-devel _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx http://lists.xen.org/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |