From patchwork Mon Jan 25 18:24:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincenzo Maffione X-Patchwork-Id: 8113861 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 296359F440 for ; Mon, 25 Jan 2016 18:26:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 387EF202FF for ; Mon, 25 Jan 2016 18:26:53 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3E0972028D for ; Mon, 25 Jan 2016 18:26:52 +0000 (UTC) Received: from localhost ([::1]:40215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNlql-0000Bd-LU for patchwork-qemu-devel@patchwork.kernel.org; Mon, 25 Jan 2016 13:26:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33874) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNlpu-0007R9-EY for qemu-devel@nongnu.org; Mon, 25 Jan 2016 13:25:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aNlps-0002Vc-W5 for qemu-devel@nongnu.org; Mon, 25 Jan 2016 13:25:58 -0500 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:34384) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNlps-0002Uh-LA for qemu-devel@nongnu.org; Mon, 25 Jan 2016 13:25:56 -0500 Received: by mail-wm0-x244.google.com with SMTP id b14so12889288wmb.1 for ; Mon, 25 Jan 2016 10:25:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=ppUqfch5OFO8m9IgcmzwXnORfXV6pNn1g/QrTgBkZ9A=; b=1H3BIUTj5TtySvVNG2OW2WeU4PDggrdypnZAij+3QxeND4glbP++nfBUUaQNsDZFpz PJer43mNU6yYaYGR2VU8FhkMb2RfpQUBW1uVdH5ZkmEt+t+8SmIUljZwnnGbp1Bg74UI mU5Y7YpLP01MRszSCFMKUDd3lTsaaeoCRzw9MpXoVpcDZUcKTNrx3HuflOtISnwwiiPx VpP0RpYlaAajuRl2mOjLn0njxkNjS0VOYjIyj8OJwpcavcIUsfMblFuqM4SeQYbs95uN yDqHrrhk9oh23KIvh0Z1eKDNrL+aeGunBHP2UfwwriZxPVWF+zFVDC4UfOUe6fK2Wug9 mD/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=ppUqfch5OFO8m9IgcmzwXnORfXV6pNn1g/QrTgBkZ9A=; b=PXU+K7uN7QdIMpnWCzj7mUHaVxDwbhgU4NnYQJXZOlCh2DdP+2ZKd6VyCCE/eQDQMD 02hSfCkZ1N/2z4SO9OjrOA4JNslHwEFsPPKaCO2hsP1bEnfriFD1fk5ZMubcFguVaN4D OyHVhFIDhCIg3VfbUlCKzdAqlt59SqvvYoTfzkDl3wT/ldmuihRb0CKZ1u+t9294ywtp JODj97a6RhhYU3KWxYOLr9thf5rfkhCSlBNArR39DHGYW0LkL3irDVNmSfGIqvBismRB pEiyRrHZC1rQpxeomT+20H8KwbL02Sn6c4Tm4471lMkCSqthserS4an2WM6kY2YbYP7Q ziTg== X-Gm-Message-State: AG10YOSp0ywZrMx99yeMRR+O6oLW42hHs5z6p6hCXwmZFC1M7L0RGOk6OpPH/+I7q6DzdQ== X-Received: by 10.28.19.76 with SMTP id 73mr20605138wmt.24.1453746356058; Mon, 25 Jan 2016 10:25:56 -0800 (PST) Received: from localhost.localdomain (prova.iet.unipi.it. [131.114.58.86]) by smtp.gmail.com with ESMTPSA id u12sm17237757wmu.10.2016.01.25.10.25.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 25 Jan 2016 10:25:55 -0800 (PST) From: Vincenzo Maffione To: qemu-devel@nongnu.org Date: Mon, 25 Jan 2016 19:24:35 +0100 Message-Id: X-Mailer: git-send-email 2.7.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::244 Cc: g.lettieri@iet.unipi.it, rizzo@iet.unipi.it, jasowang@redhat.com, Vincenzo Maffione Subject: [Qemu-devel] [PATCH v2] net: netmap: use nm_open() to open netmap ports X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch simplifies the netmap backend code by means of the nm_open() helper function provided by netmap_user.h, which hides the details of open(), iotcl() and mmap() carried out on the netmap device. Moreover, the semantic of nm_open() makes it possible to open special netmap ports (e.g. pipes, monitors) and use special modes (e.g. host rings only, single queue mode, exclusive access). Signed-off-by: Vincenzo Maffione --- net/netmap.c | 97 ++++++++++++++++++++---------------------------------------- 1 file changed, 32 insertions(+), 65 deletions(-) diff --git a/net/netmap.c b/net/netmap.c index 5558368..27295ab 100644 --- a/net/netmap.c +++ b/net/netmap.c @@ -39,21 +39,12 @@ #include "qemu/error-report.h" #include "qemu/iov.h" -/* Private netmap device info. */ -typedef struct NetmapPriv { - int fd; - size_t memsize; - void *mem; - struct netmap_if *nifp; - struct netmap_ring *rx; - struct netmap_ring *tx; - char fdname[PATH_MAX]; /* Normally "/dev/netmap". */ - char ifname[IFNAMSIZ]; -} NetmapPriv; - typedef struct NetmapState { NetClientState nc; - NetmapPriv me; + 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]; @@ -90,44 +81,23 @@ pkt_copy(const void *_src, void *_dst, int l) * Open a netmap device. We assume there is only one queue * (which is the case for the VALE bridge). */ -static void netmap_open(NetmapPriv *me, Error **errp) +static struct nm_desc *netmap_open(const NetdevNetmapOptions *nm_opts, + Error **errp) { - int fd; - int err; - size_t l; + struct nm_desc *nmd; struct nmreq req; - me->fd = fd = open(me->fdname, O_RDWR); - if (fd < 0) { - error_setg_file_open(errp, errno, me->fdname); - return; - } memset(&req, 0, sizeof(req)); - pstrcpy(req.nr_name, sizeof(req.nr_name), me->ifname); - req.nr_ringid = NETMAP_NO_TX_POLL; - req.nr_version = NETMAP_API; - err = ioctl(fd, NIOCREGIF, &req); - if (err) { - error_setg_errno(errp, errno, "Unable to register %s", me->ifname); - goto error; - } - l = me->memsize = req.nr_memsize; - me->mem = mmap(0, l, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0); - if (me->mem == MAP_FAILED) { - error_setg_errno(errp, errno, "Unable to mmap netmap shared memory"); - me->mem = NULL; - goto error; + 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; } - me->nifp = NETMAP_IF(me->mem, req.nr_offset); - me->tx = NETMAP_TXRING(me->nifp, 0); - me->rx = NETMAP_RXRING(me->nifp, 0); - - return; - -error: - close(me->fd); + return nmd; } static void netmap_send(void *opaque); @@ -136,7 +106,7 @@ 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->me.fd, + qemu_set_fd_handler(s->nmd->fd, s->read_poll ? netmap_send : NULL, s->write_poll ? netmap_writable : NULL, s); @@ -188,7 +158,7 @@ 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->me.tx; + struct netmap_ring *ring = s->tx; uint32_t i; uint32_t idx; uint8_t *dst; @@ -218,7 +188,7 @@ static ssize_t netmap_receive(NetClientState *nc, ring->slot[i].flags = 0; pkt_copy(buf, dst, size); ring->cur = ring->head = nm_ring_next(ring, i); - ioctl(s->me.fd, NIOCTXSYNC, NULL); + ioctl(s->nmd->fd, NIOCTXSYNC, NULL); return size; } @@ -227,7 +197,7 @@ 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->me.tx; + struct netmap_ring *ring = s->tx; uint32_t last; uint32_t idx; uint8_t *dst; @@ -284,7 +254,7 @@ static ssize_t netmap_receive_iov(NetClientState *nc, /* Now update ring->cur and ring->head. */ ring->cur = ring->head = i; - ioctl(s->me.fd, NIOCTXSYNC, NULL); + ioctl(s->nmd->fd, NIOCTXSYNC, NULL); return iov_size(iov, iovcnt); } @@ -301,7 +271,7 @@ static void netmap_send_completed(NetClientState *nc, ssize_t len) static void netmap_send(void *opaque) { NetmapState *s = opaque; - struct netmap_ring *ring = s->me.rx; + struct netmap_ring *ring = s->rx; /* Keep sending while there are available packets into the netmap RX ring and the forwarding path towards the peer is open. */ @@ -349,10 +319,8 @@ static void netmap_cleanup(NetClientState *nc) qemu_purge_queued_packets(nc); netmap_poll(nc, false); - munmap(s->me.mem, s->me.memsize); - close(s->me.fd); - - s->me.fd = -1; + nm_close(s->nmd); + s->nmd = NULL; } /* Offloading manipulation support callbacks. */ @@ -383,17 +351,17 @@ static void netmap_set_vnet_hdr_len(NetClientState *nc, int len) struct nmreq req; /* Issue a NETMAP_BDG_VNET_HDR command to change the virtio-net header - * length for the netmap adapter associated to 'me->ifname'. + * length for the netmap adapter associated to 's->ifname'. */ memset(&req, 0, sizeof(req)); - pstrcpy(req.nr_name, sizeof(req.nr_name), s->me.ifname); + 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; - err = ioctl(s->me.fd, NIOCREGIF, &req); + err = ioctl(s->nmd->fd, NIOCREGIF, &req); if (err) { error_report("Unable to execute NETMAP_BDG_VNET_HDR on %s: %s", - s->me.ifname, strerror(errno)); + s->ifname, strerror(errno)); } else { /* Keep track of the current length. */ s->vnet_hdr_len = len; @@ -437,16 +405,12 @@ int net_init_netmap(const NetClientOptions *opts, const char *name, NetClientState *peer, Error **errp) { const NetdevNetmapOptions *netmap_opts = opts->u.netmap; + struct nm_desc *nmd; NetClientState *nc; Error *err = NULL; - NetmapPriv me; NetmapState *s; - pstrcpy(me.fdname, sizeof(me.fdname), - netmap_opts->has_devname ? netmap_opts->devname : "/dev/netmap"); - /* Set default name for the port if not supplied. */ - pstrcpy(me.ifname, sizeof(me.ifname), netmap_opts->ifname); - netmap_open(&me, &err); + nmd = netmap_open(netmap_opts, &err); if (err) { error_propagate(errp, err); return -1; @@ -454,8 +418,11 @@ int net_init_netmap(const NetClientOptions *opts, /* Create the object. */ nc = qemu_new_net_client(&net_netmap_info, peer, "netmap", name); s = DO_UPCAST(NetmapState, nc, nc); - s->me = me; + s->nmd = nmd; + s->tx = NETMAP_TXRING(nmd->nifp, 0); + s->rx = NETMAP_RXRING(nmd->nifp, 0); s->vnet_hdr_len = 0; + pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname); netmap_read_poll(s, true); /* Initially only poll for reads. */ return 0;