/* * The Qubes OS Project, http://www.qubes-os.org * * Copyright (C) 2010 Rafal Wojtczuk * * 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 "libvchan.h" #include #include static uint32_t rd_prod(struct libvchan *ctrl) { return ctrl->is_server ? ctrl->ring->prod_out : ctrl->ring->prod_in; } static uint32_t* _rd_cons(struct libvchan *ctrl) { return ctrl->is_server ? &ctrl->ring->cons_out : &ctrl->ring->cons_in; } #define rd_cons(x) (*_rd_cons(x)) static uint32_t* _wr_prod(struct libvchan *ctrl) { return ctrl->is_server ? &ctrl->ring->prod_in : &ctrl->ring->prod_out; } #define wr_prod(x) (*_wr_prod(x)) static uint32_t wr_cons(struct libvchan *ctrl) { return ctrl->is_server ? ctrl->ring->cons_in : ctrl->ring->cons_out; } static void* rd_ring(struct libvchan *ctrl) { return ctrl->is_server ? ctrl->ring->buf_out : ctrl->ring->buf_in; } static void* wr_ring(struct libvchan *ctrl) { return ctrl->is_server ? ctrl->ring->buf_in : ctrl->ring->buf_out; } static uint32_t wr_ring_size(struct libvchan *ctrl) { return ctrl->is_server ? sizeof(ctrl->ring->buf_in) : sizeof(ctrl->ring->buf_out); } static uint32_t rd_ring_size(struct libvchan *ctrl) { return ctrl->is_server ? sizeof(ctrl->ring->buf_out) : sizeof(ctrl->ring->buf_in); } /** \return How much data is immediately available for reading */ int libvchan_data_ready(struct libvchan *ctrl) { return rd_prod(ctrl) - rd_cons(ctrl); } /** \return How much space is available for writing, without blocking */ int libvchan_buffer_space(struct libvchan *ctrl) { return wr_ring_size(ctrl) - (wr_prod(ctrl) - wr_cons(ctrl)); } static int do_notify(struct libvchan *ctrl) { return xc_evtchn_notify(ctrl->event_fd, ctrl->event_port); } /// returns nonzero if the peer has closed connection int libvchan_is_eof(struct libvchan *ctrl) { if (ctrl->is_server) { if (ctrl->ring->client_closed) return -1; } else { if (ctrl->ring->server_closed) { ctrl->ring->client_closed = 1; do_notify(ctrl); return -1; } } return 0; } /// waits for the peer to do any action /** \return -1 return value means peer has closed */ int libvchan_wait(struct libvchan *ctrl) { int ret; ret = xc_evtchn_pending(ctrl->event_fd); if (ret == -1) return ret; ret = xc_evtchn_unmask(ctrl->event_fd, ctrl->event_port); if (ret) return ret; ret = libvchan_is_eof(ctrl); return ret; } /** * returns 0 if no buffer space is available, -1 on error, or size on success */ static int do_send(struct libvchan *ctrl, const void *data, size_t size) { int real_idx = wr_prod(ctrl) & (wr_ring_size(ctrl) - 1); int avail_contig = wr_ring_size(ctrl) - real_idx; if (avail_contig > size) avail_contig = size; memcpy(wr_ring(ctrl) + real_idx, data, avail_contig); if (avail_contig < size) { // we rolled across the end of the ring memcpy(wr_ring(ctrl), data + avail_contig, size - avail_contig); } wr_prod(ctrl) += size; if (do_notify(ctrl) < 0) return -1; return size; } /** * returns 0 if no buffer space is available, -1 on error, or size on success */ int libvchan_send(struct libvchan *ctrl, const void *data, size_t size) { int avail = libvchan_buffer_space(ctrl); if (size > avail) return 0; return do_send(ctrl, data, size); } int libvchan_write(struct libvchan *ctrl, const void *data, size_t size) { int avail = libvchan_buffer_space(ctrl); if (size > avail) size = avail; return do_send(ctrl, data, size); } static int do_recv(struct libvchan *ctrl, void *data, size_t size) { int real_idx = rd_cons(ctrl) & (rd_ring_size(ctrl) - 1); int avail_contig = rd_ring_size(ctrl) - real_idx; if (avail_contig > size) avail_contig = size; memcpy(data, rd_ring(ctrl) + real_idx, avail_contig); if (avail_contig < size) { // we rolled across the end of the ring memcpy(data + avail_contig, rd_ring(ctrl), size - avail_contig); } rd_cons(ctrl) += size; if (do_notify(ctrl) < 0) return -1; return size; } /** * reads exactly size bytes from the vchan. * returns 0 if insufficient data is available, -1 on error, or size on success */ int libvchan_recv(struct libvchan *ctrl, void *data, size_t size) { int avail = libvchan_data_ready(ctrl); if (size > avail) return 0; return do_recv(ctrl, data, size); } int libvchan_read(struct libvchan *ctrl, void *data, size_t size) { int avail = libvchan_data_ready(ctrl); if (size > avail) size = avail; return do_recv(ctrl, data, size); } /** * Notify the peer of closing. Client does not block. * Server waits for client to close prior to returning * (this is needed as the server owns the shared page). */ int libvchan_close(struct libvchan *ctrl) { if (ctrl->is_server) { ctrl->ring->server_closed = 1; do_notify(ctrl); while (!ctrl->ring->client_closed && libvchan_wait(ctrl) == 0); } else { ctrl->ring->client_closed = 1; do_notify(ctrl); } return 0; } /// The fd to use for select() set int libvchan_fd_for_select(struct libvchan *ctrl) { return ctrl->event_fd; }