From patchwork Thu Jun 30 08:41:19 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 932402 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.4) with ESMTP id p5U8k8CO028649 for ; Thu, 30 Jun 2011 08:46:08 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758945Ab1F3IqF (ORCPT ); Thu, 30 Jun 2011 04:46:05 -0400 Received: from mail-iy0-f174.google.com ([209.85.210.174]:55348 "EHLO mail-iy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758942Ab1F3IqD (ORCPT ); Thu, 30 Jun 2011 04:46:03 -0400 Received: by iyb12 with SMTP id 12so1723856iyb.19 for ; Thu, 30 Jun 2011 01:46:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=cGeU4fXeTJPdnmrVdQn8W9rk9q7VrY53hoBqs01TpgI=; b=oF2+j4sHu9/Go+1J/bTV3toJj6uH6vXuGYJlP+vaasTXZnW+RqRiRkucl6CAvxtReU BA5LUzqh8NF2MbQTnWSfMWdsKT9B40DPxiFIMYC4fgbMUNJ0Mx+gknTr1C10Pywp/jdp 0DPsgcY+/Iga4y/Gzw96LShGQwWjH8cOEB6xk= Received: by 10.42.97.136 with SMTP id o8mr1421181icn.222.1309423562435; Thu, 30 Jun 2011 01:46:02 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id d6sm1967338icx.1.2011.06.30.01.45.58 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 30 Jun 2011 01:46:02 -0700 (PDT) From: Asias He To: Pekka Enberg Cc: Cyrill Gorcunov , Ingo Molnar , Sasha Levin , Prasad Joshi , kvm@vger.kernel.org, Asias He Subject: [PATCH v2 31/31] kvm tools: Introduce struct net_dev_operations Date: Thu, 30 Jun 2011 16:41:19 +0800 Message-Id: <1309423279-3093-32-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1309423279-3093-1-git-send-email-asias.hejun@gmail.com> References: <1309423279-3093-1-git-send-email-asias.hejun@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Thu, 30 Jun 2011 08:46:08 +0000 (UTC) virtio_net_{tx, rx}_thread can use ops->{tx, rx} to send and recevie network package in both tap mode and user mode. Suggested-by: Pekka Enberg Signed-off-by: Asias He --- tools/kvm/virtio/net.c | 56 ++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 45 insertions(+), 11 deletions(-) diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c index 98d4120..3a9216d 100644 --- a/tools/kvm/virtio/net.c +++ b/tools/kvm/virtio/net.c @@ -41,6 +41,13 @@ static struct pci_device_header pci_header = { .subsys_id = VIRTIO_ID_NET, }; +struct net_dev; + +struct net_dev_operations { + int (*rx)(struct iovec *iov, u16 in, struct net_dev *ndev); + int (*tx)(struct iovec *iov, u16 in, struct net_dev *ndev); +}; + struct net_dev { pthread_mutex_t mutex; @@ -68,9 +75,9 @@ struct net_dev { int mode; struct uip_info info; + struct net_dev_operations *ops; }; - static struct net_dev ndev = { .mutex = PTHREAD_MUTEX_INITIALIZER, @@ -117,10 +124,7 @@ static void *virtio_net_rx_thread(void *p) head = virt_queue__get_iov(vq, iov, &out, &in, kvm); - if (ndev.mode == NET_MODE_TAP) - len = readv(ndev.tap_fd, iov, in); - else - len = uip_rx(iov, in, &ndev.info); + len = ndev.ops->rx(iov, in, &ndev); virt_queue__set_used_elem(vq, head, len); @@ -157,10 +161,7 @@ static void *virtio_net_tx_thread(void *p) head = virt_queue__get_iov(vq, iov, &out, &in, kvm); - if (ndev.mode == NET_MODE_TAP) - len = writev(ndev.tap_fd, iov, out); - else - len = uip_tx(iov, out, &ndev.info); + len = ndev.ops->tx(iov, out, &ndev); virt_queue__set_used_elem(vq, head, len); } @@ -412,6 +413,36 @@ static void virtio_net__io_thread_init(struct kvm *kvm) pthread_create(&ndev.io_tx_thread, NULL, virtio_net_tx_thread, (void *)kvm); } +static inline int tap_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev) +{ + return writev(ndev->tap_fd, iov, out); +} + +static inline int tap_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev) +{ + return readv(ndev->tap_fd, iov, in); +} + +static inline int uip_ops_tx(struct iovec *iov, u16 out, struct net_dev *ndev) +{ + return uip_tx(iov, out, &ndev->info); +} + +static inline int uip_ops_rx(struct iovec *iov, u16 in, struct net_dev *ndev) +{ + return uip_rx(iov, in, &ndev->info); +} + +static struct net_dev_operations tap_ops = { + .rx = tap_ops_rx, + .tx = tap_ops_tx, +}; + +static struct net_dev_operations uip_ops = { + .rx = uip_ops_rx, + .tx = uip_ops_tx, +}; + void virtio_net__init(const struct virtio_net_parameters *params) { struct ioevent ioevent; @@ -430,10 +461,13 @@ void virtio_net__init(const struct virtio_net_parameters *params) pci__register(&pci_header, dev); ndev.mode = params->mode; - if (ndev.mode == NET_MODE_TAP) + if (ndev.mode == NET_MODE_TAP) { virtio_net__tap_init(params); - else + ndev.ops = &tap_ops; + } else { uip_init(&ndev.info); + ndev.ops = &uip_ops; + } virtio_net__io_thread_init(params->kvm);