2013-11-06 11:44:06 +01: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 18:50:00 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-11-06 11:44:06 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
2014-02-20 15:40:43 +01:00
|
|
|
#define NETMAP_WITH_LIBS
|
2013-11-06 11:44:06 +01:00
|
|
|
#include <net/netmap.h>
|
|
|
|
#include <net/netmap_user.h>
|
|
|
|
|
|
|
|
#include "net/net.h"
|
2014-02-06 17:02:20 +01:00
|
|
|
#include "net/tap.h"
|
2013-11-06 11:44:06 +01:00
|
|
|
#include "clients.h"
|
|
|
|
#include "sysemu/sysemu.h"
|
|
|
|
#include "qemu/error-report.h"
|
2016-03-31 15:08:10 +02:00
|
|
|
#include "qapi/error.h"
|
2013-11-06 11:44:06 +01:00
|
|
|
#include "qemu/iov.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 07:23:50 +02:00
|
|
|
#include "qemu/main-loop.h"
|
2013-11-06 11:44:06 +01:00
|
|
|
|
|
|
|
typedef struct NetmapState {
|
|
|
|
NetClientState nc;
|
2016-01-25 19:24:35 +01:00
|
|
|
struct nm_desc *nmd;
|
|
|
|
char ifname[IFNAMSIZ];
|
|
|
|
struct netmap_ring *tx;
|
|
|
|
struct netmap_ring *rx;
|
2013-11-06 11:44:06 +01:00
|
|
|
bool read_poll;
|
|
|
|
bool write_poll;
|
|
|
|
struct iovec iov[IOV_MAX];
|
2014-02-06 17:02:20 +01:00
|
|
|
int vnet_hdr_len; /* Current virtio-net header length. */
|
2013-11-06 11:44:06 +01: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 19:24:35 +01:00
|
|
|
static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts,
|
|
|
|
Error **errp)
|
2013-11-06 11:44:06 +01:00
|
|
|
{
|
2016-01-25 19:24:35 +01:00
|
|
|
struct nm_desc *nmd;
|
2013-11-06 11:44:06 +01:00
|
|
|
struct nmreq req;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
2016-01-25 19:24:35 +01: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 11:44:06 +01:00
|
|
|
}
|
|
|
|
|
2016-01-25 19:24:35 +01:00
|
|
|
return nmd;
|
2013-11-06 11:44:06 +01: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 19:24:35 +01: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 08:45:18 +02:00
|
|
|
s->read_poll ? netmap_send : NULL,
|
|
|
|
s->write_poll ? netmap_writable : NULL,
|
|
|
|
s);
|
2013-11-06 11:44:06 +01: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 10:28:43 +01:00
|
|
|
s->write_poll = enable;
|
|
|
|
s->read_poll = enable;
|
2013-11-06 11:44:06 +01: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_iov(NetClientState *nc,
|
|
|
|
const struct iovec *iov, int iovcnt)
|
|
|
|
{
|
|
|
|
NetmapState *s = DO_UPCAST(NetmapState, nc, nc);
|
2016-01-25 19:24:35 +01:00
|
|
|
struct netmap_ring *ring = s->tx;
|
2018-12-06 17:59:07 +01:00
|
|
|
unsigned int tail = ring->tail;
|
|
|
|
ssize_t totlen = 0;
|
2013-11-06 11:44:06 +01:00
|
|
|
uint32_t last;
|
|
|
|
uint32_t idx;
|
|
|
|
uint8_t *dst;
|
|
|
|
int j;
|
|
|
|
uint32_t i;
|
|
|
|
|
2018-12-06 17:59:07 +01:00
|
|
|
last = i = ring->head;
|
2013-11-06 11:44:06 +01:00
|
|
|
|
2014-02-20 15:40:43 +01:00
|
|
|
if (nm_ring_space(ring) < iovcnt) {
|
2018-12-06 17:59:07 +01:00
|
|
|
/* Not enough netmap slots. Tell the kernel that we have seen the new
|
|
|
|
* available slots (so that it notifies us again when it has more
|
|
|
|
* ones), but without publishing any new slots to be processed
|
|
|
|
* (e.g., we don't advance ring->head). */
|
|
|
|
ring->cur = tail;
|
2013-11-06 11:44:06 +01:00
|
|
|
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;
|
|
|
|
|
2018-12-06 17:59:07 +01:00
|
|
|
totlen += iov_frag_size;
|
|
|
|
|
2013-11-06 11:44:06 +01:00
|
|
|
/* 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);
|
|
|
|
|
2018-12-06 17:59:07 +01:00
|
|
|
if (unlikely(i == tail)) {
|
|
|
|
/* We ran out of netmap slots while splitting the
|
2013-11-06 11:44:06 +01:00
|
|
|
iovec fragments. */
|
2018-12-06 17:59:07 +01:00
|
|
|
ring->cur = tail;
|
2013-11-06 11:44:06 +01:00
|
|
|
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 15:40:43 +01:00
|
|
|
i = nm_ring_next(ring, i);
|
2013-11-06 11:44:06 +01: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;
|
|
|
|
|
2018-12-06 17:59:07 +01:00
|
|
|
/* Now update ring->head and ring->cur to publish the new slots and
|
|
|
|
* the new wakeup point. */
|
|
|
|
ring->head = ring->cur = i;
|
2013-11-06 11:44:06 +01:00
|
|
|
|
2016-01-25 19:24:35 +01:00
|
|
|
ioctl(s->nmd->fd, NIOCTXSYNC, NULL);
|
2013-11-06 11:44:06 +01:00
|
|
|
|
2018-12-06 17:59:07 +01:00
|
|
|
return totlen;
|
2013-11-06 11:44:06 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:59:06 +01:00
|
|
|
static ssize_t netmap_receive(NetClientState *nc,
|
|
|
|
const uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
struct iovec iov;
|
|
|
|
|
|
|
|
iov.iov_base = (void *)buf;
|
|
|
|
iov.iov_len = size;
|
|
|
|
|
|
|
|
return netmap_receive_iov(nc, &iov, 1);
|
|
|
|
}
|
|
|
|
|
2013-11-06 11:44:06 +01:00
|
|
|
/* 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 19:24:35 +01:00
|
|
|
struct netmap_ring *ring = s->rx;
|
2018-12-06 17:59:05 +01:00
|
|
|
unsigned int tail = ring->tail;
|
2013-11-06 11:44:06 +01:00
|
|
|
|
2018-12-06 17:59:05 +01:00
|
|
|
/* Keep sending while there are available slots in the netmap
|
2013-11-06 11:44:06 +01:00
|
|
|
RX ring and the forwarding path towards the peer is open. */
|
2018-12-06 17:59:05 +01:00
|
|
|
while (ring->head != tail) {
|
|
|
|
uint32_t i = ring->head;
|
2013-11-06 11:44:06 +01:00
|
|
|
uint32_t idx;
|
|
|
|
bool morefrag;
|
|
|
|
int iovcnt = 0;
|
|
|
|
int iovsize;
|
|
|
|
|
2018-12-06 17:59:05 +01:00
|
|
|
/* Get a (possibly multi-slot) packet. */
|
2013-11-06 11:44:06 +01:00
|
|
|
do {
|
|
|
|
idx = ring->slot[i].buf_idx;
|
|
|
|
morefrag = (ring->slot[i].flags & NS_MOREFRAG);
|
2018-12-06 17:59:05 +01:00
|
|
|
s->iov[iovcnt].iov_base = (void *)NETMAP_BUF(ring, idx);
|
2013-11-06 11:44:06 +01:00
|
|
|
s->iov[iovcnt].iov_len = ring->slot[i].len;
|
|
|
|
iovcnt++;
|
2018-12-06 17:59:05 +01:00
|
|
|
i = nm_ring_next(ring, i);
|
|
|
|
} while (i != tail && morefrag);
|
2013-11-06 11:44:06 +01:00
|
|
|
|
2018-12-06 17:59:05 +01:00
|
|
|
/* Advance ring->cur to tell the kernel that we have seen the slots. */
|
|
|
|
ring->cur = i;
|
2013-11-06 11:44:06 +01:00
|
|
|
|
2018-12-06 17:59:05 +01:00
|
|
|
if (unlikely(morefrag)) {
|
|
|
|
/* This is a truncated packet, so we can stop without releasing the
|
|
|
|
* incomplete slots by updating ring->head. We will hopefully
|
|
|
|
* re-read the complete packet the next time we are called. */
|
|
|
|
break;
|
2013-11-06 11:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iovsize = qemu_sendv_packet_async(&s->nc, s->iov, iovcnt,
|
|
|
|
netmap_send_completed);
|
|
|
|
|
2018-12-06 17:59:05 +01:00
|
|
|
/* Release the slots to the kernel. */
|
|
|
|
ring->head = i;
|
|
|
|
|
2013-11-06 11:44:06 +01:00
|
|
|
if (iovsize == 0) {
|
|
|
|
/* The peer does not receive anymore. Packet is queued, stop
|
2018-12-06 17:59:05 +01:00
|
|
|
* reading from the backend until netmap_send_completed(). */
|
2013-11-06 11:44:06 +01:00
|
|
|
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 19:24:35 +01:00
|
|
|
nm_close(s->nmd);
|
|
|
|
s->nmd = NULL;
|
2013-11-06 11:44:06 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 17:02:20 +01:00
|
|
|
/* Offloading manipulation support callbacks. */
|
2016-02-24 11:30:41 +01:00
|
|
|
static int netmap_fd_set_vnet_hdr_len(NetmapState *s, int len)
|
2014-02-06 17:02:20 +01:00
|
|
|
{
|
2016-02-24 11:30:41 +01: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 17:02:20 +01:00
|
|
|
}
|
|
|
|
|
2016-02-24 11:30:41 +01:00
|
|
|
static bool netmap_has_vnet_hdr_len(NetClientState *nc, int len)
|
2014-02-06 17:02:20 +01:00
|
|
|
{
|
2016-02-24 11:30:41 +01: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 17:02:20 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-24 11:30:41 +01: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 17:02:20 +01:00
|
|
|
{
|
2016-02-24 11:30:41 +01:00
|
|
|
return netmap_has_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
|
2014-02-06 17:02:20 +01: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 11:30:41 +01:00
|
|
|
err = netmap_fd_set_vnet_hdr_len(s, len);
|
2014-02-06 17:02:20 +01:00
|
|
|
if (err) {
|
2016-02-24 11:30:41 +01:00
|
|
|
error_report("Unable to set vnet-hdr length %d on %s: %s",
|
|
|
|
len, s->ifname, strerror(errno));
|
2014-02-06 17:02:20 +01: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 11:30:41 +01:00
|
|
|
* enables the offloadings. */
|
2014-02-06 17:02:20 +01:00
|
|
|
if (!s->vnet_hdr_len) {
|
|
|
|
netmap_set_vnet_hdr_len(nc, sizeof(struct virtio_net_hdr));
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 11:44:06 +01:00
|
|
|
|
|
|
|
/* NetClientInfo methods */
|
|
|
|
static NetClientInfo net_netmap_info = {
|
qapi: Change Netdev into a flat union
This is a mostly-mechanical conversion that creates a new flat
union 'Netdev' QAPI type that covers all the branches of the
former 'NetClientOptions' simple union, where the branches are
now listed in a new 'NetClientDriver' enum rather than generated
from the simple union. The existence of a flat union has no
change to the command line syntax accepted for new code, and
will make it possible for a future patch to switch the QMP
command to parse a boxed union for no change to valid QMP; but
it does have some ripple effect on the C code when dealing with
the new types.
While making the conversion, note that the 'NetLegacy' type
remains unchanged: it applies only to legacy command line options,
and will not be ported to QMP, so it should remain a wrapper
around a simple union; to avoid confusion, the type named
'NetClientOptions' is now gone, and we introduce 'NetLegacyOptions'
in its place. Then, in the C code, we convert from NetLegacy to
Netdev as soon as possible, so that the bulk of the net stack
only has to deal with one QAPI type, not two. Note that since
the old legacy code always rejected 'hubport', we can just omit
that branch from the new 'NetLegacyOptions' simple union.
Based on an idea originally by Zoltán Kővágó <DirtY.iCE.hu@gmail.com>:
Message-Id: <01a527fbf1a5de880091f98cf011616a78adeeee.1441627176.git.DirtY.iCE.hu@gmail.com>
although the sed script in that patch no longer applies due to
other changes in the tree since then, and I also did some manual
cleanups (such as fixing whitespace to keep checkpatch happy).
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1468468228-27827-13-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Fixup from Eric squashed in]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-07-14 05:50:23 +02:00
|
|
|
.type = NET_CLIENT_DRIVER_NETMAP,
|
2013-11-06 11:44:06 +01:00
|
|
|
.size = sizeof(NetmapState),
|
|
|
|
.receive = netmap_receive,
|
|
|
|
.receive_iov = netmap_receive_iov,
|
|
|
|
.poll = netmap_poll,
|
|
|
|
.cleanup = netmap_cleanup,
|
2016-02-24 11:30:41 +01:00
|
|
|
.has_ufo = netmap_has_vnet_hdr,
|
2014-02-06 17:02:20 +01: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 11:44:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The exported init function
|
|
|
|
*
|
|
|
|
* ... -net netmap,ifname="..."
|
|
|
|
*/
|
2016-07-14 05:50:12 +02:00
|
|
|
int net_init_netmap(const Netdev *netdev,
|
2015-05-15 13:58:50 +02:00
|
|
|
const char *name, NetClientState *peer, Error **errp)
|
2013-11-06 11:44:06 +01:00
|
|
|
{
|
qapi: Change Netdev into a flat union
This is a mostly-mechanical conversion that creates a new flat
union 'Netdev' QAPI type that covers all the branches of the
former 'NetClientOptions' simple union, where the branches are
now listed in a new 'NetClientDriver' enum rather than generated
from the simple union. The existence of a flat union has no
change to the command line syntax accepted for new code, and
will make it possible for a future patch to switch the QMP
command to parse a boxed union for no change to valid QMP; but
it does have some ripple effect on the C code when dealing with
the new types.
While making the conversion, note that the 'NetLegacy' type
remains unchanged: it applies only to legacy command line options,
and will not be ported to QMP, so it should remain a wrapper
around a simple union; to avoid confusion, the type named
'NetClientOptions' is now gone, and we introduce 'NetLegacyOptions'
in its place. Then, in the C code, we convert from NetLegacy to
Netdev as soon as possible, so that the bulk of the net stack
only has to deal with one QAPI type, not two. Note that since
the old legacy code always rejected 'hubport', we can just omit
that branch from the new 'NetLegacyOptions' simple union.
Based on an idea originally by Zoltán Kővágó <DirtY.iCE.hu@gmail.com>:
Message-Id: <01a527fbf1a5de880091f98cf011616a78adeeee.1441627176.git.DirtY.iCE.hu@gmail.com>
although the sed script in that patch no longer applies due to
other changes in the tree since then, and I also did some manual
cleanups (such as fixing whitespace to keep checkpatch happy).
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1468468228-27827-13-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Fixup from Eric squashed in]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-07-14 05:50:23 +02:00
|
|
|
const NetdevNetmapOptions *netmap_opts = &netdev->u.netmap;
|
2016-01-25 19:24:35 +01:00
|
|
|
struct nm_desc *nmd;
|
2013-11-06 11:44:06 +01:00
|
|
|
NetClientState *nc;
|
2015-11-10 10:47:22 +01:00
|
|
|
Error *err = NULL;
|
2013-11-06 11:44:06 +01:00
|
|
|
NetmapState *s;
|
|
|
|
|
2016-01-25 19:24:35 +01:00
|
|
|
nmd = netmap_open(netmap_opts, &err);
|
2015-11-10 10:47:22 +01:00
|
|
|
if (err) {
|
|
|
|
error_propagate(errp, err);
|
2013-11-06 11:44:06 +01: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 19:24:35 +01:00
|
|
|
s->nmd = nmd;
|
|
|
|
s->tx = NETMAP_TXRING(nmd->nifp, 0);
|
|
|
|
s->rx = NETMAP_RXRING(nmd->nifp, 0);
|
2014-02-06 17:02:20 +01:00
|
|
|
s->vnet_hdr_len = 0;
|
2016-01-25 19:24:35 +01:00
|
|
|
pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname);
|
2013-11-06 11:44:06 +01:00
|
|
|
netmap_read_poll(s, true); /* Initially only poll for reads. */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|