blktap3/sring: shared ring between tapdisk and the front-end
PATCH RESEND v2 1/1 blktap3/sring: shared ring between tapdisk and the front-end
This patch series introduces the shared ring used by the front-end to pass request descriptors to tapdisk, as well as responses from tapdisk to the front-end. Requests from this ring end up in tapdisk's standard request queue.
When the tapback daemon detects that the front-end tries to connect to the back-end, it spawns a tapdisk and tells it to connect to the shared ring. The shared ring is created by the tapdisk using the grant references and the event channel port, supplied by tapback. Once the ring is created, tapdisk watches the event channel for notifications. When a notification is received, tapdisk extracts the request, parses it, and passes it to the standard tapdisk request queue for processing.
PATCH RESEND v2 1/1 blktap3/sring: Headers required for compiling the shared ring
This patch series introduces the headers required for the shared ring library to compile. The files are:
* io-optimize.h: imported from blktap2
* scheduler.h: imported from blktap2, contains definitions for the tapdisk scheduler, the component that processes events, be it I/O requests or control commands
* tapdisk.h: imported from blktap2, core header file that contains the definition of the I/O request
* tapdisk-image.h: imported from blktap2, contains the definition of the Virtual Disk Image (VDI).
* tapdisk-log.h: imported from blktap2, contains logging stuff
* tapdisk-queue.h: imported from blktap2, contains the tapdisk I/O request and event queue.
* tapdisk-server.h: imported from blktap2
* tapdisk-stats.h: imported from blktap2.5, contains stats stuff
* tapdisk-utils.h: imported from blktap2.5, contains assorted utility functions
* tapdisk-vbd.h: imported from blktap2, contains the definition of the Virtual Block Device (VBD)
Also, the patch series contains the following blktap3-related changes:
* Update declaration of function tapdisk_image_open_chain as it now uses the parent /path/to/file instead of the parent minor number.
* Update declaration of function tapdisk_server_get_vbd as it now uses type:/path/to/file instead of the minor number.
tools/blktap3/drivers/io-optimize.h new file +68 -0
@@ -0,0 +1,68 @@ +/* +* Copyright (c) 2008, XenSource Inc. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of XenSource Inc. nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef __IO_OPTIMIZE_H__ +#define __IO_OPTIMIZE_H__ + +#include <libaio.h> + +struct opio ; + +struct opio_list { +struct opio *head; +struct opio *tail; +}; + +struct opio { +char *buf; +unsigned long nbytes; +long long offset; +void *data; +struct iocb *iocb; +struct io_event event; +struct opio *head; +struct opio *next; +struct opio_list list; +}; + +struct opioctx { +int num_opios; +int free_opio_cnt; +struct opio *opios; +struct opio **free_opios; +struct iocb **iocb_queue; +struct io_event *event_queue; +}; + +int opio_init (struct opioctx *ctx,int num_iocbs); +void opio_free (struct opioctx *ctx); +int io_merge (struct opioctx *ctx,struct iocb **queue,int num); +int io_split (struct opioctx *ctx,struct io_event *events,int num); +int io_expand_iocbs (struct opioctx *ctx,struct iocb **queue,int idx,int num); + +#endif
tools/blktap3/drivers/scheduler.h new file +88 -0
88 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-blktap.h new file +84 -0
84 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-image.h new file +119 -0
119 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-log.h new file +69 -0
69 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-queue.h new file +125 -0
125 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-server.h new file +76 -0
76 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-stats.h new file +70 -0
70 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-utils.h new file +49 -0
49 lines not on this page · raw message
tools/blktap3/drivers/tapdisk-vbd.h renamed from tools/blktap2/drivers/tapdisk-vbd.h +107 -92
287 lines not on this page · raw message
tools/blktap3/drivers/tapdisk.h new file +251 -0
251 lines not on this page · raw message
PATCH RESEND v2 1/1 blktap3/sring: connect to/disconnect from the shared ring
In reply to Thanos MakatosThis patch introduces the functions that allows tapdisk to connect to/disconnect from the shared ring. They are message handlers executed when the tapback daemon sends a TAPDISK_MESSAGE_XENBLKIF_CONNECT/DISCONNECT message to the tapdisk, as a result of running the Xenbus protocol. The connection to the ring is effectively established by mapping the grant references and binding to the guest domain's event channel port (both the grant references and the port are supplied by the tapback daemon). After the connection is established, a callback is registered to be executed when a notification for the ring arrives.
---
Changed since v1:
* Make the VBD also point to the internal sring structure when the tapdisk connects to the sring and clear it when it disconnects. The VBD needs to point to the internal sring structure in order to retrieve stats.
tools/blktap3/drivers/sring/blkif.h new file +87 -0
@@ -0,0 +1,87 @@ +/* +* Copyright (C) 2012 Citrix Ltd. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +* USA. +*/ + +#ifndef __SRING_BLKIF_H__ +#define __SRING_BLKIF_H__ + +#include <xen/io/blkif.h> + +/* Not a real protocol. Used to generate ring structs which contain +* the elements common to all protocols only. This way we get a +* compiler-checkable way to use common struct elements, so we can +* avoid using switch(protocol) in a number of places. */ +struct blkif_common_request { +char dummy; +}; +struct blkif_common_response { +char dummy; +}; + +/* i386 protocol version */ +#pragma pack(push, 4) +struct blkif_x86_32_request { +uint8_t operation;/* BLKIF_OP_??? */ +uint8_t nr_segments;/* number of segments */ +blkif_vdev_t handle;/* only for read/write requests */ +uint64_t id;/* private guest value, echoed in resp */ +blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ +struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST ]; +}; +struct blkif_x86_32_response { +uint64_t id;/* copied from request */ +uint8_t operation;/* copied from request */ +int16_t status;/* BLKIF_RSP_??? */ +}; +typedef struct blkif_x86_32_request blkif_x86_32_request_t ; +typedef struct blkif_x86_32_response blkif_x86_32_response_t ; +#pragma pack(pop) + +/* x86_64 protocol version */ +struct blkif_x86_64_request { +uint8_t operation;/* BLKIF_OP_??? */ +uint8_t nr_segments;/* number of segments */ +blkif_vdev_t handle;/* only for read/write requests */ +uint64_t __attribute__ ((__aligned__ (8 ))) id; +blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ +struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST ]; +}; +struct blkif_x86_64_response { +uint64_t __attribute__ ((__aligned__ (8 ))) id; +uint8_t operation;/* copied from request */ +int16_t status;/* BLKIF_RSP_??? */ +}; +typedef struct blkif_x86_64_request blkif_x86_64_request_t ; +typedef struct blkif_x86_64_response blkif_x86_64_response_t ; + +DEFINE_RING_TYPES(blkif_common, struct blkif_common_request, + struct blkif_common_response); +DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request, + struct blkif_x86_32_response); +DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request, + struct blkif_x86_64_response); + +union blkif_back_rings { +blkif_back_ring_t native; +blkif_common_back_ring_t common; +blkif_x86_32_back_ring_t x86_32; +blkif_x86_64_back_ring_t x86_64; +}; +typedef union blkif_back_rings blkif_back_rings_t ; + +#endif /* __SRING_BLKIF_H__ */
tools/blktap3/drivers/sring/td-blkif.c new file +231 -0
231 lines not on this page · raw message
tools/blktap3/drivers/sring/td-blkif.h new file +168 -0
168 lines not on this page · raw message
PATCH RESEND v2 1/1 blktap3/sring: extract requests from the ring, grouping of VBDs
In reply to Thanos MakatosThis patch introduces the functions that extract requests from the shared ring. When a VBD is created, the file descriptor of the event channel is added to the set of the file descriptors watched by the main select(). When a notification arrives, the callback associated with it extracts requests from the ring. Parsing of the requests and passing them to the tapdisk standard request queue is introduced by another patch in this series.
---
Changed since v1:
* Clarified use of grouping of event channels.
tools/blktap3/drivers/sring/td-ctx.c new file +443 -0
@@ -0,0 +1,443 @@ +/* +* Copyright (C) 2012 Citrix Ltd. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +* USA. +*/ + +#include <assert.h> +#include <errno.h> +#include <syslog.h> + +#include "tapdisk-server.h" + +#include "td-ctx.h" + +struct tqh_td_xenio_ctx _td_xenio_ctxs + =TAILQ_HEAD_INITIALIZER (_td_xenio_ctxs); + +/** +* TODO releases a pool? +*/ +static void +tapdisk_xenio_ctx_close (struct td_xenio_ctx *const ctx) +{ +assert (ctx); + +if (ctx->ring_event >=0 ) { +tapdisk_server_unregister_event (ctx->ring_event); + ctx->ring_event =-1 ; + } + +if (ctx->xce_handle) { +xc_evtchn_close (ctx->xce_handle); + ctx->xce_handle =NULL ; + } + +if (ctx->xcg_handle) { +xc_evtchn_close (ctx->xcg_handle); + ctx->xcg_handle =NULL ; + } + +TAILQ_REMOVE (&_td_xenio_ctxs, ctx, entry); + +/* TODO when do we free it? */ +} + +/* +* XXX only called by tapdisk_xenio_ctx_ring_event +*/ +static inline struct td_xenblkif * +xenio_pending_blkif (struct td_xenio_ctx *const ctx) +{ +evtchn_port_or_error_t port; +struct td_xenblkif *blkif; +int err; + +assert (ctx); + +/* +* Get the local port for which there is a pending event. +*/ + port =xc_evtchn_pending (ctx->xce_handle); +if (port ==-1 ) { +/* TODO log error */ +return NULL ; + } + +/* +* Find the block interface with that local port. +*/ +tapdisk_xenio_ctx_find_blkif (ctx, blkif, + blkif->port == port); +if (blkif) { + err =xc_evtchn_unmask (ctx->xce_handle, port); +if (err) { +/* TODO log error */ +return NULL ; + } + } +/* +* TODO Is it possible to have an pending event channel but no block +* interface associated with it? +*/ + +return blkif; +} + +#define blkif_get_req (dst, src) \ +{ \ + int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST; \ + dst->operation = src->operation; \ + dst->nr_segments = src->nr_segments; \ + dst->handle = src->handle; \ + dst->id = src->id; \ + dst->sector_number = src->sector_number; \ + xen_rmb(); \ + if (n > dst->nr_segments) \ + n = dst->nr_segments; \ + for (i = 0; i < n; i++) \ + dst->seg[i] = src->seg[i]; \ +} + +/** +* Utility function that retrieves a request using @idx as the ring index, +* copying it to the @dst in a H/W independent way. +* +* @param blkif the block interface +* @param dst address that receives the request +* @param rc the index of the request in the ring +*/ +static inline void +xenio_blkif_get_request (struct td_xenblkif *const blkif, +blkif_request_t *const dst,const RING_IDX idx) +{ +blkif_back_rings_t * rings; + +assert (blkif); +assert (dst); + + rings = &blkif->rings; + +switch (blkif->proto) { +case BLKIF_PROTOCOL_NATIVE : + { +blkif_request_t *src; + src =RING_GET_REQUEST (&rings->native, idx); +memcpy (dst, src,sizeof (blkif_request_t)); +break ; + } + +case BLKIF_PROTOCOL_X86_32 : + { +blkif_x86_32_request_t *src; + src =RING_GET_REQUEST (&rings->x86_32, idx); +blkif_get_req (dst, src); +break ; + } + +case BLKIF_PROTOCOL_X86_64 : + { +blkif_x86_64_request_t *src; + src =RING_GET_REQUEST (&rings->x86_64, idx); +blkif_get_req (dst, src); +break ; + } + +default : +/* +* TODO log error +*/ +assert (0 ); + } +} + +/** +* Retrieves at most @count request descriptors from the ring, copying them to +* @reqs. +* +* @param blkif the block interface +* @param reqs array of pointers where each element points to sufficient memory +* space that receives each request descriptor +* @param count retrieve at most that many request descriptors +* @returns the number of retrieved request descriptors +* +* XXX only called by xenio_blkif_get_requests +*/ +static inline int +__xenio_blkif_get_requests (struct td_xenblkif *const blkif, +blkif_request_t *reqs[],const unsigned int count) +{ +blkif_common_back_ring_t * ring; +RING_IDX rp, rc; +unsigned int n; + +assert (blkif); +assert (reqs); + +if (!count) +return 0 ; + + ring = &blkif->rings.common; + + rp = ring->sring->req_prod; +xen_rmb ();/* TODO why? */ + +for (rc = ring->req_cons, n =0 ; rc != rp; rc++) { +blkif_request_t *dst = reqs[n]; + +if (n++ >= count) +break ; + +xenio_blkif_get_request (blkif, dst, rc); + } + + ring->req_cons = rc; + +return n; +} + +/** +* Retrieves at most @count request descriptors. +* +* @param blkif the block interface +* @param reqs array of pointers where each pointer points to sufficient +* memory to hold a request descriptor +* @count maximum number of request descriptors to retrieve +* @param final re-enable notifications before it stops reading +* @returns the number of request descriptors retrieved +* +* TODO change name +*/ +static inline int +xenio_blkif_get_requests (struct td_xenblkif *const blkif, +blkif_request_t *reqs[],const int count,const int final) +{ +blkif_common_back_ring_t * ring; +int n =0 ; +int work =0 ; + +assert (blkif); +assert (reqs); +assert (count >0 ); + + ring = &blkif->rings.common; + +do { +if (final) +RING_FINAL_CHECK_FOR_REQUESTS (ring, work); +else + work =RING_HAS_UNCONSUMED_REQUESTS (ring); + +if (!work) +break ; + +if (n >= count) +break ; + + n +=__xenio_blkif_get_requests (blkif, reqs + n, count - n); + }while (1 ); + +return n; +} + +/** +* Callback executed when there is a request descriptor in the ring. Copies as +* many request descriptors as possible (limited by local buffer space) to the +* td_blkif's local request buffer and queues them to the tapdisk queue. +*/ +static inline void +tapdisk_xenio_ctx_ring_event (event_id_t id __attribute__((unused)), +char mode __attribute__((unused)),void *private) +{ +struct td_xenio_ctx *ctx = private; +struct td_xenblkif *blkif =NULL ; +int n_reqs; +int final =0 ; +int start; +blkif_request_t **reqs; + +assert (ctx); + + blkif =xenio_pending_blkif (ctx); +if (!blkif) { +/* TODO log error */ +return ; + } + + start = blkif->n_reqs_free; + blkif->stats.kicks.in++; + +/* +* In each iteration, copy as many request descriptors from the shared ring +* that can fit in the td_blkif's buffer. +*/ +do { + reqs = &blkif->reqs_free[blkif->ring_size - blkif->n_reqs_free]; + +assert (reqs); + + n_reqs =xenio_blkif_get_requests (blkif, reqs, blkif->n_reqs_free, + final); +assert (n_reqs >=0 ); +if (!n_reqs) +break ; + + blkif->n_reqs_free -= n_reqs; + final =1 ; + + }while (1 ); + + n_reqs = start - blkif->n_reqs_free; +if (!n_reqs) +/* TODO If there are no requests to be copied, why was there a +* notification in the first place? +*/ +return ; + blkif->stats.reqs.in += n_reqs; + reqs = &blkif->reqs_free[blkif->ring_size - start]; +tapdisk_xenblkif_queue_requests (blkif, reqs, n_reqs); +} + +/* NB. may be NULL, but then the image must be bouncing I/O */ +#define TD_XENBLKIF_DEFAULT_POOL "td-xenio-default" + +/** +* Opens a context on the specified pool. +* +* @param pool the pool, it can either be NULL or a non-zero length string +* @returns 0 in success +* +* TODO The pool is ignored, we always open the default pool. +*/ +static inline int +tapdisk_xenio_ctx_open (const char *pool) +{ +struct td_xenio_ctx *ctx; +int fd, err; + +/* zero-length pool names are not allowed */ +if (pool && !strlen (pool)) +return EINVAL ; + + ctx =calloc (1 ,sizeof (*ctx)); +if (!ctx) { + err = errno; +syslog (LOG_ERR ,"cannot allocate memory" ); + goto fail; + } + + ctx->ring_event =-1 ;/* TODO is there a special value? */ + ctx->pool =TD_XENBLKIF_DEFAULT_POOL ; +TAILQ_INIT (&ctx->blkifs); +TAILQ_INSERT_HEAD (&_td_xenio_ctxs, ctx, entry); + + ctx->xce_handle =xc_evtchn_open (NULL ,0 ); +if (ctx->xce_handle ==NULL ) { + err = errno; +syslog (LOG_ERR ,"failed to open the event channel driver: %s\n" , +strerror (err)); + goto fail; + } + + ctx->xcg_handle =xc_gnttab_open (NULL ,0 ); +if (ctx->xcg_handle ==NULL ) { + err = errno; +syslog (LOG_ERR ,"failed to open the grant table driver: %s\n" , +strerror (err)); + goto fail; + } + + fd =xc_evtchn_fd (ctx->xce_handle); +if (fd <0 ) { + err = errno; +syslog (LOG_ERR ,"failed to get the event channel file descriptor: %s\n" , +strerror (err)); + goto fail; + } + + ctx->ring_event =tapdisk_server_register_event (SCHEDULER_POLL_READ_FD , + fd,0 , tapdisk_xenio_ctx_ring_event, ctx); +if (ctx->ring_event <0 ) { + err = -ctx->ring_event; +syslog (LOG_ERR ,"failed to register event: %s\n" ,strerror (err)); + goto fail; + } + +return 0 ; + +fail: +tapdisk_xenio_ctx_close (ctx); +return err; +} + + +/** +* Tells whether @ctx belongs to @pool. +* +* If no @pool is not specified and a default pool is set, @ctx is compared +* against the default pool. Note that NULL is valid pool name value. +*/ +static inline int +__td_xenio_ctx_match (struct td_xenio_ctx * ctx,const char *pool) +{ +if (unlikely (!pool)) { +if (NULL !=TD_XENBLKIF_DEFAULT_POOL ) +return !strcmp (ctx->pool,TD_XENBLKIF_DEFAULT_POOL ); +else +return !ctx->pool; + } + +return !strcmp (ctx->pool, pool); +} + +#define tapdisk_xenio_find_ctx (_ctx, _cond) \ + do { \ + int found = 0; \ + tapdisk_xenio_for_each_ctx(_ctx) { \ + if (_cond) { \ + found = 1; \ + break; \ + } \ + } \ + if (!found) \ + _ctx = NULL; \ + } while (0) + +int +tapdisk_xenio_ctx_get (const char *pool,struct td_xenio_ctx ** _ctx) +{ +struct td_xenio_ctx *ctx; +int err =0 ; + +do { +tapdisk_xenio_find_ctx (ctx,__td_xenio_ctx_match (ctx, pool)); +if (ctx) { + *_ctx = ctx; +return 0 ; + } + + err =tapdisk_xenio_ctx_open (pool); + }while (!err); + +return err; +} + +void +tapdisk_xenio_ctx_put (struct td_xenio_ctx * ctx) +{ +if (TAILQ_EMPTY (&ctx->blkifs)) +tapdisk_xenio_ctx_close (ctx); +}
tools/blktap3/drivers/sring/td-ctx.h new file +113 -0
113 lines not on this page · raw message
PATCH RESEND v2 1/1 blktap3/sring: parse ring requests and handle them over to tapdisk
In reply to Thanos MakatosThis patch introduces the functionality of parsing shared ring requests and adding them to tapdisk's standard request queue. Also, it provides the functionality of producing responses.
tools/blktap3/drivers/sring/td-req.c new file +505 -0
@@ -0,0 +1,505 @@ +/* +* Copyright (C) 2012 Citrix Ltd. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +* USA. +*/ + +#include "td-req.h" +#include "td-blkif.h" +#include <assert.h> +#include <stdlib.h> +#include <errno.h> +#include "td-ctx.h" +#include <syslog.h> +#include <inttypes.h> +#include "tapdisk-vbd.h" + +/* +* TODO needed for PROT_READ/PROT_WRITE, probably some xen header supplies them +* too +*/ +#include <sys/mman.h> + +/** +* Puts the request back to the free list of this block interface. +* +* @param blkif the block interface +* @param tapreq the request to give back +*/ +static void +tapdisk_xenblkif_free_request (struct td_xenblkif *const blkif, +struct td_xenblkif_req *const tapreq) +{ +assert (blkif); +assert (tapreq); +assert (blkif->n_reqs_free <= blkif->ring_size); + + blkif->reqs_free[blkif->ring_size - (++blkif->n_reqs_free)] = &tapreq->msg; +} + +/** +* Returns the size, in request descriptors, of the shared ring +* +* @param blkif the block interface +* @returns the size, in request descriptors, of the shared ring +*/ +static int +td_blkif_ring_size (const struct td_xenblkif *const blkif) +{ +assert (blkif); + +switch (blkif->proto) { +case BLKIF_PROTOCOL_NATIVE : +return RING_SIZE (&blkif->rings.native); + +case BLKIF_PROTOCOL_X86_32 : +return RING_SIZE (&blkif->rings.x86_32); + +case BLKIF_PROTOCOL_X86_64 : +return RING_SIZE (&blkif->rings.x86_64); + +default : +return -EPROTONOSUPPORT ; + } +} + +/** +* Unmaps a request's data. +* +* @param blkif the block interface the request belongs to +* @param req the request to unmap +*/ +static int +xenio_blkif_munmap_one (struct td_xenblkif *const blkif, +struct td_xenblkif_req *const req) +{ +struct td_xenio_ctx *ctx; +int err; + +assert (blkif); +assert (req); + + ctx = blkif->ctx; +assert (ctx); + + err =xc_gnttab_munmap (ctx->xcg_handle, req->vma, req->nr_segments); +if (err) { + err = errno; +/* TODO don't use syslog for error on the data path */ +syslog (LOG_ERR ,"failed to unmap pages: %s\n" ,strerror (err)); +return err; + } + + req->vma =NULL ; +return 0 ; +} + +/** +* Get the response that corresponds to the specified ring index in a H/W +* independent way. +* +* TODO use function pointers instead of switch +* XXX only called by xenio_blkif_put_response +*/ +static inline +blkif_response_t *xenio_blkif_get_response (struct td_xenblkif *const blkif, +const RING_IDX rp) +{ +blkif_back_rings_t *const rings = &blkif->rings; +blkif_response_t * p =NULL ; + +switch (blkif->proto) { +case BLKIF_PROTOCOL_NATIVE : + p = (blkif_response_t *)RING_GET_RESPONSE (&rings->native, rp); +break ; +case BLKIF_PROTOCOL_X86_32 : + p = (blkif_response_t *)RING_GET_RESPONSE (&rings->x86_32, rp); +break ; +case BLKIF_PROTOCOL_X86_64 : + p = (blkif_response_t *)RING_GET_RESPONSE (&rings->x86_64, rp); +break ; +default : +/* TODO gracefully fail? */ +abort (); + } + +return p; +} + +/** +* Puts a response in the ring. +* +* @param blkif the VBD +* @param req the request for which the response should be put +* @param status the status of the response (success or an error code) +* @param final controls whether the front-end will be notified, if necessary +* +* TODO @req can be NULL so the function will only notify the other end. This +* is used in the error path of tapdisk_xenblkif_queue_requests. The point is +* that the other will just be notified, does this make sense? +*/ +static int +xenio_blkif_put_response (struct td_xenblkif *const blkif, +struct td_xenblkif_req *req,int const status,int const final) +{ +blkif_common_back_ring_t *const ring = &blkif->rings.common; + +if (req) { +blkif_response_t * msg =xenio_blkif_get_response (blkif, + ring->rsp_prod_pvt); +assert (msg); + +assert (status ==BLKIF_RSP_EOPNOTSUPP || status ==BLKIF_RSP_ERROR + || status ==BLKIF_RSP_OKAY ); + + msg->id = req->id; + +/* TODO Why do we have to set this? */ + msg->operation = req->op; + + msg->status = status; + + ring->rsp_prod_pvt++; + } + +if (final) { +int notify; +RING_PUSH_RESPONSES_AND_CHECK_NOTIFY (ring, notify); +if (notify) { +int err =xc_evtchn_notify (blkif->ctx->xce_handle, blkif->port); +if (err <0 ) { +/* TODO log error */ +return errno; + } + } + } + +return 0 ; +} + +/** +* Completes a request. +* +* @blkif the VBD the request belongs belongs to +* @tapreq the request to complete +* @error completion status of the request +* @final controls whether the other end should be notified +*/ +static void +tapdisk_xenblkif_complete_request (struct td_xenblkif *const blkif, +struct td_xenblkif_req * tapreq,const int error,const int final) +{ +assert (blkif); +assert (tapreq); + +if (tapreq->vma) { +int err =0 ; + err =xenio_blkif_munmap_one (blkif, tapreq); +/* TODO How do we deal with errors here? */ + } + +xenio_blkif_put_response (blkif, tapreq, + (error ?BLKIF_RSP_ERROR :BLKIF_RSP_OKAY ), final); + +tapdisk_xenblkif_free_request (blkif, tapreq); + + blkif->stats.reqs.out++; +if (final) + blkif->stats.kicks.out++; +} + +/** +* Request completion callback, executed when the tapdisk has finished +* processing the request. +* +* @param vreq the completed request +* @param error status of the request +* @param token token previously associated with this request +* @param final TODO ? +*/ +static inline void +__tapdisk_xenblkif_request_cb (struct td_vbd_request *const vreq, +const int error,void *const token,const int final) +{ +struct td_xenblkif_req *tapreq; +struct td_xenblkif *const blkif = token; + +assert (vreq); +assert (blkif); + + tapreq = containerof(vreq, struct td_xenblkif_req, vreq); + +tapdisk_xenblkif_complete_request (blkif, tapreq, error, final); +if (error) + blkif->stats.errors.img++; +} + +/** +* Initialises the standard tapdisk request (td_vbd_request_t) from the +* intermediate ring request (td_xenblkif_req) in order to prepare it +* processing. +* +* @param blkif the block interface +* @param tapreq the request to prepare +* @returns 0 on success +* +* TODO only called by tapdisk_xenblkif_queue_request +*/ +static inline int +tapdisk_xenblkif_make_vbd_request (struct td_xenblkif *const blkif, +struct td_xenblkif_req *const tapreq) +{ +td_vbd_request_t *vreq; +int i; +struct td_iovec *iov; +void *page, *next, *last; +int prot; +grant_ref_t gref[BLKIF_MAX_SEGMENTS_PER_REQUEST ]; + +assert (tapreq); + + vreq = &tapreq->vreq; +assert (vreq); + +switch (tapreq->msg.operation) { +case BLKIF_OP_READ : + tapreq->op =BLKIF_OP_READ ; + vreq->op =TD_OP_READ ; + prot =PROT_WRITE ; +break ; +case BLKIF_OP_WRITE : + tapreq->op =BLKIF_OP_WRITE ; + vreq->op =TD_OP_WRITE ; + prot =PROT_READ ; +break ; +default : +/* TODO log error */ +return EOPNOTSUPP ; + } + +/* TODO there should be at least one segment, right? */ +if (tapreq->msg.nr_segments <1 + || tapreq->msg.nr_segments >BLKIF_MAX_SEGMENTS_PER_REQUEST ) { +/* TODO log error */ +return EINVAL ; + } + +for (i =0 ; i < tapreq->msg.nr_segments; i++) { +struct blkif_request_segment *seg = &tapreq->msg.seg[i]; + gref[i] = seg->gref; + +/* +* Note that first and last may be equal, which means only one sector +* must be transferred. +*/ +if (seg->last_sect < seg->first_sect) { +/* TODO log error */ +return EINVAL ; + } + } + + tapreq->nr_segments = tapreq->msg.nr_segments; + +/* +* Map the request's data. +*/ + tapreq->vma =xc_gnttab_map_domain_grant_refs (blkif->ctx->xcg_handle, + tapreq->nr_segments, blkif->domid, gref, prot); +if (!tapreq->vma) { +/* TODO log error */ +return errno; + } + + tapreq->id = tapreq->msg.id; + +/* +* Vectorizes the request: creates the struct iovec (in tapreq->iov) that +* describes each segment to be transferred. Also, merges consecutive +* segments. +* TODO The following piece of code would be much simpler if we didn't +* merge segments, right? +* +* In each loop, iov points to the previous scatter/gather element in order +* to reuse it if the current and previous segments are consecutive. +*/ + iov = tapreq->iov -1 ; + last =NULL ; + page = tapreq->vma; + +for (i =0 ; i < tapreq->nr_segments; i++) {/* for each segment */ +struct blkif_request_segment *seg = &tapreq->msg.seg[i]; +size_t size; + +/* TODO check that first_sect/last_sect are within page */ + + next = page + (seg->first_sect <<SECTOR_SHIFT ); + size = seg->last_sect - seg->first_sect +1 ; + +if (next != last) { + iov++; + iov->base = next; + iov->secs = size; + }else /* The "else" is true if fist_sect is 0. */ + iov->secs += size; + + last = iov->base + (iov->secs <<SECTOR_SHIFT ); + page +=XC_PAGE_SIZE ; + } + + vreq->iov = tapreq->iov; + vreq->iovcnt = iov - tapreq->iov +1 ; + vreq->sec = tapreq->msg.sector_number; + +/* +* TODO Isn't this kind of expensive to do for each requests? Why does the +* tapdisk need this in the first place? +*/ +snprintf (tapreq->name,sizeof (tapreq->name),"xenvbd-%d-%d.%" SCNx64"" , + blkif->domid, blkif->devid, tapreq->msg.id); + + vreq->name = tapreq->name; + vreq->token = blkif; + vreq->cb = __tapdisk_xenblkif_request_cb; + +return 0 ; +} + +#define msg_to_tapreq (_req) \ + containerof(_req, struct td_xenblkif_req, msg) + +/** +* Queues a ring request, after it prepares it, to the standard taodisk queue +* for processing. +* +* @param blkif the block interface +* @param msg the ring request +* @param tapreq the intermediate request +* +* TODO don't really need to supply the ring request since it's either way +* contained in the tapreq +* +* XXX only called by tapdisk_xenblkif_queue_requests +*/ +static inline int +tapdisk_xenblkif_queue_request (struct td_xenblkif *const blkif, +blkif_request_t *msg,struct td_xenblkif_req *tapreq) +{ +int err; + +assert (blkif); +assert (msg); +assert (tapreq); + + err =tapdisk_xenblkif_make_vbd_request (blkif, tapreq); +if (err) { +/* TODO log error */ + blkif->stats.errors.map++; +return err; + } + + err =tapdisk_vbd_queue_request (blkif->vbd, &tapreq->vreq); +if (err) { +/* TODO log error */ + blkif->stats.errors.vbd++; +return err; + } + +return 0 ; +} + +void +tapdisk_xenblkif_queue_requests (struct td_xenblkif *const blkif, +blkif_request_t *reqs[],const int nr_reqs) +{ +int i; +int err; +int nr_errors =0 ; + +assert (blkif); +assert (reqs); +assert (nr_reqs >=0 ); + +for (i =0 ; i < nr_reqs; i++) {/* for each request from the ring... */ +blkif_request_t *msg = reqs[i]; +struct td_xenblkif_req *tapreq; + +assert (msg); + + tapreq =msg_to_tapreq (msg); + +assert (tapreq); + + err =tapdisk_xenblkif_queue_request (blkif, msg, tapreq); +if (err) { +/* TODO log error */ + nr_errors++; +tapdisk_xenblkif_complete_request (blkif, tapreq, err,1 ); + } + } + +if (nr_errors) +xenio_blkif_put_response (blkif,NULL ,0 ,1 ); +} + +void +tapdisk_xenblkif_reqs_free (struct td_xenblkif *const blkif) +{ +assert (blkif); + +free (blkif->reqs); + blkif->reqs =NULL ; + +free (blkif->reqs_free); + blkif->reqs_free =NULL ; +} + +int +tapdisk_xenblkif_reqs_init (struct td_xenblkif *td_blkif) +{ +int i =0 ; +int err =0 ; + +assert (td_blkif); + + td_blkif->ring_size =td_blkif_ring_size (td_blkif); +assert (td_blkif->ring_size >0 ); + + td_blkif->reqs = +malloc (td_blkif->ring_size *sizeof (struct td_xenblkif_req )); +if (!td_blkif->reqs) { +/* TODO log error */ + err = -errno; + goto fail; + } + + td_blkif->reqs_free = +malloc (td_blkif->ring_size *sizeof (struct xenio_blkif_req *)); +if (!td_blkif->reqs_free) { +/* TODO log error */ + err = -errno; + goto fail; + } + + td_blkif->n_reqs_free =0 ; +for (i =0 ; i < td_blkif->ring_size; i++) +tapdisk_xenblkif_free_request (td_blkif, &td_blkif->reqs[i]); + +return 0 ; + +fail: +tapdisk_xenblkif_reqs_free (td_blkif); +return err; +}
tools/blktap3/drivers/sring/td-req.h new file +131 -0
131 lines not on this page · raw message
PATCH RESEND v2 1/1 blktap3/sring: stats for the shared ring between tapdisk and the front-end
In reply to Thanos MakatosThis patch introduces stats for the shared ring between tapdisk and the front-end. I suspect that the stats regarding the ring between tapdisk and blktap don't make sense any more, so the stats introduced by this patch could be consolidated into the existing ones.
---
Changed since v1:
* Simplify sring stats procuring since the VBD now points to the internal sring structure.
* Introduce declaration of function tapdisk_xenblkif_stats.
tools/blktap3/drivers/sring/td-stats.c new file +60 -0
@@ -0,0 +1,60 @@ +/* +* Copyright (C) 2012 Citrix Ltd. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* 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 General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +* USA. +*/ + +#include "td-stats.h" +#include "td-ctx.h" + +static inline void +__tapdisk_xenblkif_stats (struct td_xenblkif * blkif,td_stats_t * st) +{ +assert (blkif); +assert (st); +assert (blkif->ctx); + +tapdisk_stats_field (st,"pool" , blkif->ctx->pool); +tapdisk_stats_field (st,"domid" ,"d" , blkif->domid); +tapdisk_stats_field (st,"devid" ,"d" , blkif->devid); + +tapdisk_stats_field (st,"reqs" ,"[" ); +tapdisk_stats_val (st,"llu" , blkif->stats.reqs.in); +tapdisk_stats_val (st,"llu" , blkif->stats.reqs.out); +tapdisk_stats_leave (st,']' ); + +tapdisk_stats_field (st,"kicks" ,"[" ); +tapdisk_stats_val (st,"llu" , blkif->stats.kicks.in); +tapdisk_stats_val (st,"llu" , blkif->stats.kicks.out); +tapdisk_stats_leave (st,']' ); + +tapdisk_stats_field (st,"errors" ,"{" ); +tapdisk_stats_field (st,"msg" ,"llu" , blkif->stats.errors.msg); +tapdisk_stats_field (st,"map" ,"llu" , blkif->stats.errors.map); +tapdisk_stats_field (st,"vbq" ,"llu" , blkif->stats.errors.vbd); +tapdisk_stats_field (st,"img" ,"llu" , blkif->stats.errors.img); +tapdisk_stats_leave (st,']' ); +} + +void +tapdisk_xenblkif_stats (struct td_xenblkif * blkif,td_stats_t * st) +{ +tapdisk_stats_field (st,"xen-blkifs" ,"[" ); +tapdisk_stats_enter (st,'{' ); +__tapdisk_xenblkif_stats (blkif, st); +tapdisk_stats_leave (st,'}' ); +tapdisk_stats_leave (st,']' ); +}
tools/blktap3/drivers/sring/td-stats.h new file +46 -0
46 lines not on this page · raw message
PATCH RESEND v2 1/1 blktap3/sring: Compile the shared ring into a static library
In reply to Thanos Makatos---
Changed since v1:
* Introduces sring stats in compilation.
tools/blktap3/drivers/sring/Makefile new file +34 -0
@@ -0,0 +1,34 @@ +XEN_ROOT=$(CURDIR)/../../../.. +include $(XEN_ROOT)/tools/Rules.mk + +BLKTAP_ROOT = ../.. + +override CFLAGS += \ + -fno-strict-aliasing \ + -I$(BLKTAP_ROOT)/include \ + -I$(BLKTAP_ROOT)/drivers \ + $(CFLAGS_libxenctrl) \ + $(CFLAGS_libxenstore) \ + $(CFLAGS_xeninclude) \ + -Werror \ + -Wall \ + -Wextra + +override LDFLAGS += \ + $(LDLIBS_libxenstore) \ + $(LDFLAGS_libxenctrl) + +SRING-OBJS := td-blkif.o +SRING-OBJS += td-req.o +SRING-OBJS += td-ctx.o +SRING-OBJS += td-stats.o + +libsring.a: $(SRING-OBJS) + ar rcs libsring.a $(SRING-OBJS) + +all: libsring.a + +clean: + rm -rf $(SRING-OBJS) libsring.a + +.PHONY: all clean