/* * netmap access for qemu * * Copyright (c) 2012-2013 Luigi Rizzo * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "qemu/osdep.h" #include #include #include #define NETMAP_WITH_LIBS #include #include #include "net/net.h" #include "net/tap.h" #include "clients.h" #include "sysemu/sysemu.h" #include "qemu/error-report.h" #include "qapi/error.h" #include "qemu/iov.h" #include "qemu/cutils.h" typedef struct NetmapState { NetClientState nc; struct nm_desc *nmd; char ifname[IFNAMSIZ]; struct netmap_ring *tx; struct netmap_ring *rx; bool read_poll; bool write_poll; struct iovec iov[IOV_MAX]; int vnet_hdr_len; /* Current virtio-net header length. */ } NetmapState; #ifndef __FreeBSD__ #define pkt_copy bcopy #else /* A fast copy routine only for multiples of 64 bytes, non overlapped. */ static inline void pkt_copy(const void *_src, void *_dst, int l) { const uint64_t *src = _src; uint64_t *dst = _dst; if (unlikely(l >= 1024)) { bcopy(src, dst, l); return; } for (; l > 0; l -= 64) { *dst++ = *src++; *dst++ = *src++; *dst++ = *src++; *dst++ = *src++; *dst++ = *src++; *dst++ = *src++; *dst++ = *src++; *dst++ = *src++; } } #endif /* __FreeBSD__ */ /* * Open a netmap device. We assume there is only one queue * (which is the case for the VALE bridge). */ static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts, Error **errp) { struct nm_desc *nmd; struct nmreq req; memset(&req, 0, sizeof(req)); nmd = nm_open(nm_opts->ifname, &req, NETMAP_NO_TX_POLL, NULL); if (nmd == NULL) { error_setg_errno(errp, errno, "Failed to nm_open() %s", nm_opts->ifname); return NULL; } return nmd; } static void netmap_send(void *opaque); static void netmap_writable(void *opaque); /* Set the event-loop handlers for the netmap backend. */ static void netmap_update_fd_handler(NetmapState *s) { qemu_set_fd_handler(s->nmd->fd, s->read_poll ? netmap_send : NULL, s->write_poll ? netmap_writable : NULL, s); } /* Update the read handler. */ static void netmap_read_poll(NetmapState *s, bool enable) { if (s->read_poll != enable) { /* Do nothing if not changed. */ s->read_poll = enable; netmap_update_fd_handler(s); } } /* Update the write handler. */ static void netmap_write_poll(NetmapState *s, bool enable) { if (s->write_poll != enable) { s->write_poll = enable; netmap_update_fd_handler(s); } } static void netmap_poll(NetClientState *nc, bool enable) { NetmapState *s = DO_UPCAST(NetmapState, nc, nc); if (s->read_poll != enable || s->write_poll != enable) { s->write_poll = enable; s->read_poll = enable; netmap_update_fd_handler(s); } } /* * The fd_write() callback, invoked if the fd is marked as * writable after a poll. Unregister the handler and flush any * buffered packets. */ static void netmap_writable(void *opaque) { NetmapState *s = opaque; netmap_write_poll(s, false); qemu_flush_queued_packets(&s->nc); } static ssize_t netmap_receive(NetClientState *nc, const uint8_t *buf, size_t size) { NetmapState *s = DO_UPCAST(NetmapState, nc, nc); struct netmap_ring *ring = s->tx; uint32_t i; uint32_t idx; uint8_t *dst; if (unlikely(!ring)) { /* Drop. */ return size; } if (unlikely(size > ring->nr_buf_size)) { RD(5, "[netmap_receive] drop packet of size %d > %d\n", (int)size, ring->nr_buf_size); return size; } if (nm_ring_empty(ring)) { /* No available slots in the netmap TX ring. */ netmap_write_poll(s, true); return 0; } i = ring->cur; idx = ring->slot[i].buf_idx; dst = (uint8_t *)NETMAP_BUF(ring, idx); ring->slot[i].len = size; ring->slot[i].flags = 0; pkt_copy(buf, dst, size); ring->cur = ring->head = nm_ring_next(ring, i); ioctl(s->nmd->fd, NIOCTXSYNC, NULL); return size; } static ssize_t netmap_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt) { NetmapState *s = DO_UPCAST(NetmapState, nc, nc); struct netmap_ring *ring = s->tx; uint32_t last; uint32_t idx; uint8_t *dst; int j; uint32_t i; if (unlikely(!ring)) { /* Drop the packet. */ return iov_size(iov, iovcnt); } last = i = ring->cur; if (nm_ring_space(ring) < iovcnt) { /* Not enough netmap slots. */ netmap_write_poll(s, true); return 0; } for (j = 0; j < iovcnt; j++) { int iov_frag_size = iov[j].iov_len; int offset = 0; int nm_frag_size; /* S