2013-11-06 14:44:06 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-01-29 20:50:00 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-11-06 14:44:06 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/mman.h>
|
2014-02-20 18:40:43 +04:00
|
|
|
#define NETMAP_WITH_LIBS
|
2013-11-06 14:44:06 +04:00
|
|
|
#include <net/netmap.h>
|
|
|
|
#include <net/netmap_user.h>
|
|
|
|
|
|
|
|
#include "net/net.h"
|
2014-02-06 20:02:20 +04:00
|
|
|
#include "net/tap.h"
|
2013-11-06 14:44:06 +04:00
|
|
|
#include "clients.h"
|
|
|
|
#include "sysemu/sysemu.h"
|
|
|
|
#include "qemu/error-report.h"
|
2016-03-31 16:08:10 +03:00
|
|
|
#include "qapi/error.h"
|
2013-11-06 14:44:06 +04:00
|
|
|
#include "qemu/iov.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2013-11-06 14:44:06 +04:00
|
|
|
|
|
|
|
typedef struct NetmapState {
|
|
|
|
NetClientState nc;
|
2016-01-25 21:24:35 +03:00
|
|
|
struct nm_desc *nmd;
|
|
|
|
char ifname[IFNAMSIZ];
|
|
|
|
struct netmap_ring *tx;
|
|
|
|
struct netmap_ring *rx;
|
2013-11-06 14:44:06 +04:00
|
|
|
bool read_poll;
|
|
|
|
bool write_poll;
|
|
|
|
struct iovec iov[IOV_MAX];
|
2014-02-06 20:02:20 +04:00
|
|
|
int vnet_hdr_len; /* Current virtio-net header length. */
|
2013-11-06 14:44:06 +04:00
|
|
|
} 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).
|
|
|
|
*/
|
2016-01-25 21:24:35 +03:00
|
|
|
static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts,
|
|
|
|
Error **errp)
|
2013-11-06 14:44:06 +04:00
|
|
|
{
|
2016-01-25 21:24:35 +03:00
|
|
|
struct nm_desc *nmd;
|
2013-11-06 14:44:06 +04:00
|
|
|
struct nmreq req;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
2016-01-25 21:24:35 +03:00
|
|
|
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;
|
2013-11-06 14:44:06 +04:00
|
|
|
}
|
|
|
|
|
2016-01-25 21:24:35 +03:00
|
|
|
return nmd;
|
2013-11-06 14:44:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-01-25 21:24:35 +03:00
|
|
|
qemu_set_fd_handler(s->nmd->fd,
|
Change qemu_set_fd_handler2(..., NULL, ...) to qemu_set_fd_handler
Done with following Coccinelle semantic patch, plus manual cosmetic changes in
net/*.c.
@@
expression E1, E2, E3, E4;
@@
- qemu_set_fd_handler2(E1, NULL, E2, E3, E4);
+ qemu_set_fd_handler(E1, E2, E3, E4);
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-id: 1433400324-7358-8-git-send-email-famz@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2015-06-04 09:45:18 +03:00
|
|
|
s->read_poll ? netmap_send : NULL,
|
|
|
|
s->write_poll ? netmap_writable : NULL,
|
|
|
|
s);
|
2013-11-06 14:44:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
2014-03-23 13:28:43 +04:00
|
|
|
s->write_poll = enable;
|
|
|
|
s->read_poll = enable;
|
2013-11-06 14:44:06 +04:00
|
|
|
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);
|
2016-01-25 21:24:35 +03:00
|
|
|
struct netmap_ring *ring = s->tx;
|
2013-11-06 14:44:06 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-20 18:40:43 +04:00
|
|
|
if (nm_ring_empty(ring)) {
|
2013-11-06 14:44:06 +04:00
|
|
|
/* 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);
|
2014-02-20 18:40:43 +04:00
|
|
|
ring->cur = ring->head = nm_ring_next(ring, i);
|
2016-01-25 21:24:35 +03:00
|
|
|
ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
|
2013-11-06 14:44:06 +04:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t netmap_receive_iov(NetClientState *nc,
|
|
|
|
const struct iovec *iov, int iovcnt)
|
|
|
|
{
|
|
|
|
NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
|
2016-01-25 21:24:35 +03:00
|
|
|
struct netmap_ring *ring = s->tx;
|
2013-11-06 14:44:06 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-02-06 20:02:20 +04:00
|
|
|
last = i = ring->cur;
|
2013-11-06 14:44:06 +04:00
|
|
|
|
2014-02-20 18:40:43 +04:00
|
|
|
if (nm_ring_space(ring) < iovcnt) {
|
2013-11-06 14:44:06 +04:00
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* Split each iovec fragment over more netmap slots, if
|
|
|
|
necessary. */
|
|
|
|
while (iov_frag_size) {
|
|
|
|
nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size);
|
|
|
|
|
2014-02-20 18:40:43 +04:00
|
|
|
if (unlikely(nm_ring_empty(ring))) {
|
2013-11-06 14:44:06 +04:00
|
|
|
/* We run out of netmap slots while splitting the
|
|
|
|
iovec fragments. */
|
|
|
|
netmap_write_poll(s, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = ring->slot[i].buf_idx;
|
|
|
|
dst = (uint8_t *)NETMAP_BUF(ring, idx);
|
|
|
|
|
|
|
|
ring->slot[i].len = nm_frag_size;
|
|
|
|
ring->slot[i].flags = NS_MOREFRAG;
|
|
|
|
pkt_copy(iov[j].iov_base + offset, dst, nm_frag_size);
|
|
|
|
|
|
|
|
last = i;
|
2014-02-20 18:40:43 +04:00
|
|
|
i = nm_ring_next(ring, i);
|
2013-11-06 14:44:06 +04:00
|
|
|
|
|
|
|
offset += nm_frag_size;
|
|
|
|
iov_frag_size -= nm_frag_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* The last slot must not have NS_MOREFRAG set. */
|
|
|
|
ring->slot[last].flags &= ~NS_MOREFRAG;
|
|
|
|
|
2014-02-20 18:40:43 +04:00
|
|
|
/* Now update ring->cur and ring->head. */
|
|
|
|
ring->cur = ring->head = i;
|
2013-11-06 14:44:06 +04:00
|
|
|
|
2016-01-25 21:24:35 +03:00
|
|
|
ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
|
2013-11-06 14:44:06 +04:00
|
|
|
|
|
|
|
return iov_size(iov, iovcnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Complete a previous send (backend --> guest) and enable the
|
|
|
|
fd_read callback. */
|
|
|
|
static void netmap_send_completed(NetClientState *nc, ssize_t len)
|
|
|
|
{
|
|
|
|
NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
|
|
|
|
|
|
|
|
netmap_read_poll(s, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netmap_send(void *opaque)
|
|
|
|
{
|
|
|
|
NetmapState *s = opaque;
|
2016-01-25 21:24:35 +03:00
|
|
|
struct netmap_ring *ring = s->rx;
|
2013-11-06 14:44:06 +04:00
|
|
|
|
|
|
|
/* Keep sending while there are available packets into the netmap
|
|
|
|
RX ring and the forwarding path towards the peer is open. */
|
2015-06-04 09:45:15 +03:00
|
|
|
while (!nm_ring_empty(ring)) {
|
2013-11-06 14:44:06 +04:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t idx;
|
|
|
|
bool morefrag;
|
|
|
|
int iovcnt = 0;
|
|
|
|
int iovsize;
|
|
|
|
|
|
|
|
do {
|
|
|
|
i = ring->cur;
|
|
|
|
idx = ring->slot[i].buf_idx;
|
|
|
|
morefrag = (ring->slot[i].flags & NS_MOREFRAG);
|
|
|
|
s->iov[iovcnt].iov_base = (u_char *)NETMAP_BUF(ring, idx);
|
|
|
|
s->iov[iovcnt].iov_len = ring->slot[i].len;
|
|
|
|
iovcnt++;
|
|
|
|
|
2014-02-20 18:40:43 +04:00
|
|
|
ring->cur = ring->head = nm_ring_next(ring, i);
|
|
|
|
} while (!nm_ring_empty(ring) && morefrag);
|
2013-11-06 14:44:06 +04:00
|
|
|
|
2014-02-20 18:40:43 +04:00
|
|
|
if (unlikely(nm_ring_empty(ring) && morefrag)) {
|
2013-11-06 14:44:06 +04:00
|
|
|
RD(5, "[netmap_send] ran out of slots, with a pending"
|
|
|
|
"incomplete packet\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
|
|
|
|
netmap_send_completed);
|
|
|
|
|
|
|
|
if (iovsize == 0) {
|
|
|
|
/* The peer does not receive anymore. Packet is queued, stop
|
|
|
|
* reading from the backend until netmap_send_completed()
|
|
|
|
*/
|
|
|
|
netmap_read_poll(s, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush and close. */
|
|
|
|
static void netmap_cleanup(NetClientState *nc)
|
|
|
|
{
|
|
|
|
NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
|
|
|
|
|
|
|
|
qemu_purge_queued_packets(nc);
|
|
|
|
|
|
|
|
netmap_poll(nc, false);
|
2016-01-25 21:24:35 +03:00
|
|
|
nm_close(s->nmd);
|
|
|
|
s->nmd = NULL;
|
2013-11-06 14:44:06 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 20:02:20 +04:00
|
|
|
/* Offloading manipulation support callbacks. */
|
2016-02-24 13:30:41 +03:00
|
|
|
static int netmap_fd_set_vnet_hdr_len(NetmapState *s, int len)
|
2014-02-06 20:02:20 +04:00
|
|
|
{
|
2016-02-24 13:30:41 +03:00
|
|
|
struct nmreq req;
|
|
|
|
|
|
|
|
/* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header
|
|
|
|
* length for the netmap adapter associated to 's->ifname'.
|
|
|
|
*/
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
pstrcpy(req.nr_name, sizeof(req.nr_name), s->ifname);
|
|
|
|
req.nr_version = NETMAP_API;
|
|
|
|
req.nr_cmd = NETMAP_BDG_VNET_HDR;
|
|
|
|
req.nr_arg1 = len;
|
|
|
|
|
|
|
|
return ioctl(s->nmd->fd, NIOCREGIF, &req);
|
2014-02-06 20:02:20 +04:00
|
|
|
}
|
|
|
|
|
2016-02-24 13:30:41 +03:00
|
|
|
static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
|
2014-02-06 20:02:20 +04:00
|
|
|
{
|
2016-02-24 13:30:41 +03:00
|
|
|
NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
|
|
|
|
int prev_len = s->vnet_hdr_len;
|
|
|
|
|
|
|
|
/* Check that we can set the new length. */
|
|
|
|
if (netmap_fd_set_vnet_hdr_len(s, len)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the previous length. */
|
|
|
|
if (netmap_fd_set_vnet_hdr_len(s, prev_len)) {
|
|
|
|
error_report("Failed to restore vnet-hdr length %d on %s: %s",
|
|
|
|
prev_len, s->ifname, strerror(errno));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2014-02-06 20:02:20 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-24 13:30:41 +03:00
|
|
|
/* A netmap interface that supports virtio-net headers always
|
|
|
|
* supports UFO, so we use this callback also for the has_ufo hook. */
|
|
|
|
static bool netmap_has_vnet_hdr(NetClientState *nc)
|
2014-02-06 20:02:20 +04:00
|
|
|
{
|
2016-02-24 13:30:41 +03:00
|
|
|
return netmap_has_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
|
2014-02-06 20:02:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void netmap_using_vnet_hdr(NetClientState *nc, bool enable)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netmap_set_vnet_hdr_len(NetClientState *nc, int len)
|
|
|
|
{
|
|
|
|
NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
|
|
|
|
int err;
|
|
|
|
|
2016-02-24 13:30:41 +03:00
|
|
|
err = netmap_fd_set_vnet_hdr_len(s, len);
|
2014-02-06 20:02:20 +04:00
|
|
|
if (err) {
|
2016-02-24 13:30:41 +03:00
|
|
|
error_report("Unable to set vnet-hdr length %d on %s: %s",
|
|
|
|
len, s->ifname, strerror(errno));
|
2014-02-06 20:02:20 +04:00
|
|
|
} else {
|
|
|
|
/* Keep track of the current length. */
|
|
|
|
s->vnet_hdr_len = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void netmap_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
|
|
|
|
int ecn, int ufo)
|
|
|
|
{
|
|
|
|
NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
|
|
|
|
|
|
|
|
/* Setting a virtio-net header length greater than zero automatically
|
2016-02-24 13:30:41 +03:00
|
|
|
* enables the offloadings. */
|
2014-02-06 20:02:20 +04:00
|
|
|
if (!s->vnet_hdr_len) {
|
|
|
|
netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 14:44:06 +04:00
|
|
|
|
|
|
|
/* NetClientInfo methods */
|
|
|
|
static NetClientInfo net_netmap_info = {
|
|
|
|
.type = NET_CLIENT_OPTIONS_KIND_NETMAP,
|
|
|
|
.size = sizeof(NetmapState),
|
|
|
|
.receive = netmap_receive,
|
|
|
|
.receive_iov = netmap_receive_iov,
|
|
|
|
.poll = netmap_poll,
|
|
|
|
.cleanup = netmap_cleanup,
|
2016-02-24 13:30:41 +03:00
|
|
|
.has_ufo = netmap_has_vnet_hdr,
|
2014-02-06 20:02:20 +04:00
|
|
|
.has_vnet_hdr = netmap_has_vnet_hdr,
|
|
|
|
.has_vnet_hdr_len = netmap_has_vnet_hdr_len,
|
|
|
|
.using_vnet_hdr = netmap_using_vnet_hdr,
|
|
|
|
.set_offload = netmap_set_offload,
|
|
|
|
.set_vnet_hdr_len = netmap_set_vnet_hdr_len,
|
2013-11-06 14:44:06 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The exported init function
|
|
|
|
*
|
|
|
|
* ... -net netmap,ifname="..."
|
|
|
|
*/
|
|
|
|
int net_init_netmap(const NetClientOptions *opts,
|
2015-05-15 14:58:50 +03:00
|
|
|
const char *name, NetClientState *peer, Error **errp)
|
2013-11-06 14:44:06 +04:00
|
|
|
{
|
qapi: Don't special-case simple union wrappers
Simple unions were carrying a special case that hid their 'data'
QMP member from the resulting C struct, via the hack method
QAPISchemaObjectTypeVariant.simple_union_type(). But by using
the work we started by unboxing flat union and alternate
branches, coupled with the ability to visit the members of an
implicit type, we can now expose the simple union's implicit
type in qapi-types.h:
| struct q_obj_ImageInfoSpecificQCow2_wrapper {
| ImageInfoSpecificQCow2 *data;
| };
|
| struct q_obj_ImageInfoSpecificVmdk_wrapper {
| ImageInfoSpecificVmdk *data;
| };
...
| struct ImageInfoSpecific {
| ImageInfoSpecificKind type;
| union { /* union tag is @type */
| void *data;
|- ImageInfoSpecificQCow2 *qcow2;
|- ImageInfoSpecificVmdk *vmdk;
|+ q_obj_ImageInfoSpecificQCow2_wrapper qcow2;
|+ q_obj_ImageInfoSpecificVmdk_wrapper vmdk;
| } u;
| };
Doing this removes asymmetry between QAPI's QMP side and its
C side (both sides now expose 'data'), and means that the
treatment of a simple union as sugar for a flat union is now
equivalent in both languages (previously the two approaches used
a different layer of dereferencing, where the simple union could
be converted to a flat union with equivalent C layout but
different {} on the wire, or to an equivalent QMP wire form
but with different C representation). Using the implicit type
also lets us get rid of the simple_union_type() hack.
Of course, now all clients of simple unions have to adjust from
using su->u.member to using su->u.member.data; while this touches
a number of files in the tree, some earlier cleanup patches
helped minimize the change to the initialization of a temporary
variable rather than every single member access. The generated
qapi-visit.c code is also affected by the layout change:
|@@ -7393,10 +7393,10 @@ void visit_type_ImageInfoSpecific_member
| }
| switch (obj->type) {
| case IMAGE_INFO_SPECIFIC_KIND_QCOW2:
|- visit_type_ImageInfoSpecificQCow2(v, "data", &obj->u.qcow2, &err);
|+ visit_type_q_obj_ImageInfoSpecificQCow2_wrapper_members(v, &obj->u.qcow2, &err);
| break;
| case IMAGE_INFO_SPECIFIC_KIND_VMDK:
|- visit_type_ImageInfoSpecificVmdk(v, "data", &obj->u.vmdk, &err);
|+ visit_type_q_obj_ImageInfoSpecificVmdk_wrapper_members(v, &obj->u.vmdk, &err);
| break;
| default:
| abort();
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1458254921-17042-13-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-03-18 01:48:37 +03:00
|
|
|
const NetdevNetmapOptions *netmap_opts = opts->u.netmap.data;
|
2016-01-25 21:24:35 +03:00
|
|
|
struct nm_desc *nmd;
|
2013-11-06 14:44:06 +04:00
|
|
|
NetClientState *nc;
|
2015-11-10 12:47:22 +03:00
|
|
|
Error *err = NULL;
|
2013-11-06 14:44:06 +04:00
|
|
|
NetmapState *s;
|
|
|
|
|
2016-01-25 21:24:35 +03:00
|
|
|
nmd = netmap_open(netmap_opts, &err);
|
2015-11-10 12:47:22 +03:00
|
|
|
if (err) {
|
|
|
|
error_propagate(errp, err);
|
2013-11-06 14:44:06 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Create the object. */
|
|
|
|
nc = qemu_new_net_client(&net_netmap_info, peer, "netmap", name);
|
|
|
|
s = DO_UPCAST(NetmapState, nc, nc);
|
2016-01-25 21:24:35 +03:00
|
|
|
s->nmd = nmd;
|
|
|
|
s->tx = NETMAP_TXRING(nmd->nifp, 0);
|
|
|
|
s->rx = NETMAP_RXRING(nmd->nifp, 0);
|
2014-02-06 20:02:20 +04:00
|
|
|
s->vnet_hdr_len = 0;
|
2016-01-25 21:24:35 +03:00
|
|
|
pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname);
|
2013-11-06 14:44:06 +04:00
|
|
|
netmap_read_poll(s, true); /* Initially only poll for reads. */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|