From patchwork Thu Feb 5 00:18:32 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 5542 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n150LVco006449 for ; Thu, 5 Feb 2009 00:21:31 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754226AbZBEAV3 (ORCPT ); Wed, 4 Feb 2009 19:21:29 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753352AbZBEAV2 (ORCPT ); Wed, 4 Feb 2009 19:21:28 -0500 Received: from g5t0007.atlanta.hp.com ([15.192.0.44]:1736 "EHLO g5t0007.atlanta.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753231AbZBEAV1 (ORCPT ); Wed, 4 Feb 2009 19:21:27 -0500 Received: from g1t0038.austin.hp.com (g1t0038.austin.hp.com [16.236.32.44]) by g5t0007.atlanta.hp.com (Postfix) with ESMTP id 842EE1441C; Thu, 5 Feb 2009 00:21:27 +0000 (UTC) Received: from ldl.fc.hp.com (ldl.fc.hp.com [15.11.146.30]) by g1t0038.austin.hp.com (Postfix) with ESMTP id 4175030078; Thu, 5 Feb 2009 00:21:27 +0000 (UTC) Received: from localhost (ldl.fc.hp.com [127.0.0.1]) by ldl.fc.hp.com (Postfix) with ESMTP id E7E5639C06E; Wed, 4 Feb 2009 17:21:26 -0700 (MST) X-Virus-Scanned: Debian amavisd-new at ldl.fc.hp.com Received: from ldl.fc.hp.com ([127.0.0.1]) by localhost (ldl.fc.hp.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0cvtrW1FrMRF; Wed, 4 Feb 2009 17:21:25 -0700 (MST) Received: from kvm.aw (lart.fc.hp.com [15.11.146.31]) by ldl.fc.hp.com (Postfix) with ESMTP id 1F88839C00E; Wed, 4 Feb 2009 17:21:25 -0700 (MST) From: Alex Williamson Subject: [PATCH v3 8/8] qemu:virtio-net: Add VLAN filtering To: anthony@codemonkey.ws, qemu-devel@nongnu.org Cc: markmc@redhat.com, kvm@vger.kernel.org, alex.williamson@hp.com Date: Wed, 04 Feb 2009 17:18:32 -0700 Message-ID: <20090205001832.27879.98500.stgit@kvm.aw> In-Reply-To: <20090205001707.27879.22745.stgit@kvm.aw> References: <20090205001707.27879.22745.stgit@kvm.aw> User-Agent: StGIT/0.14.2 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Use the control virtqueue to allow the guest to enable and manipulate a VLAN filter table. This allows us to drop more packets the guest doesn't want to see. We define a new VLAN class for the control virtqueue with commands ADD and DEL with usage defined in virtio-net.h. Signed-off-by: Alex Williamson --- hw/virtio-net.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- hw/virtio-net.h | 14 ++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/hw/virtio-net.c b/hw/virtio-net.c index 524ef37..62153e9 100644 --- a/hw/virtio-net.c +++ b/hw/virtio-net.c @@ -16,9 +16,10 @@ #include "qemu-timer.h" #include "virtio-net.h" -#define VIRTIO_NET_VM_VERSION 5 +#define VIRTIO_NET_VM_VERSION 6 #define MAC_TABLE_ENTRIES 32 +#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ typedef struct VirtIONet { @@ -38,6 +39,7 @@ typedef struct VirtIONet int in_use; uint8_t *macs; } mac_table; + uint32_t *vlans; } VirtIONet; /* TODO @@ -94,9 +96,10 @@ static void virtio_net_reset(VirtIODevice *vdev) n->promisc = 1; n->allmulti = 0; - /* Flush any MAC filter table state */ + /* Flush any MAC and VLAN filter table state */ n->mac_table.in_use = 0; memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); + memset(n->vlans, 0, MAX_VLAN >> 3); } static uint32_t virtio_net_get_features(VirtIODevice *vdev) @@ -104,7 +107,8 @@ static uint32_t virtio_net_get_features(VirtIODevice *vdev) uint32_t features = (1 << VIRTIO_NET_F_MAC) | (1 << VIRTIO_NET_F_STATUS) | (1 << VIRTIO_NET_F_CTRL_VQ) | - (1 << VIRTIO_NET_F_CTRL_RX); + (1 << VIRTIO_NET_F_CTRL_RX) | + (1 << VIRTIO_NET_F_CTRL_VLAN); return features; } @@ -185,6 +189,31 @@ static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, return VIRTIO_NET_OK; } +static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, + VirtQueueElement *elem) +{ + uint16_t vid; + + if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) { + fprintf(stderr, "virtio-net ctrl invalid vlan command\n"); + return VIRTIO_NET_ERR; + } + + vid = lduw_le_p(elem->out_sg[1].iov_base); + + if (vid >= MAX_VLAN) + return VIRTIO_NET_ERR; + + if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) + n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); + else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) + n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); + else + return VIRTIO_NET_ERR; + + return VIRTIO_NET_OK; +} + static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) { VirtIONet *n = to_virtio_net(vdev); @@ -211,6 +240,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); else if (ctrl.class == VIRTIO_NET_CTRL_MAC) status = virtio_net_handle_mac(n, ctrl.cmd, &elem); + else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) + status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem); stb_p(elem.in_sg[elem.in_num - 1].iov_base, status); @@ -285,6 +316,7 @@ static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt, static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) { static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + static const uint8_t vlan[] = {0x81, 0x00}; uint8_t *ptr = (uint8_t *)buf; int i; @@ -296,6 +328,12 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) ptr += sizeof(struct virtio_net_hdr); #endif + if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { + int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; + if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) + return 0; + } + if ((ptr[0] & 1) && n->allmulti) return 1; @@ -474,6 +512,7 @@ static void virtio_net_save(QEMUFile *f, void *opaque) qemu_put_be32(f, n->allmulti); qemu_put_be32(f, n->mac_table.in_use); qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); + qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); } static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) @@ -510,6 +549,9 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) } } + if (version_id >= 6) + qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); + if (n->tx_timer_active) { qemu_mod_timer(n->tx_timer, qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); @@ -559,6 +601,10 @@ void virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn) if (!n->mac_table.macs) return; + n->vlans = qemu_mallocz(MAX_VLAN >> 3); + if (!n->vlans) + return; + register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION, virtio_net_save, virtio_net_load, n); } diff --git a/hw/virtio-net.h b/hw/virtio-net.h index 291fa9d..95587f7 100644 --- a/hw/virtio-net.h +++ b/hw/virtio-net.h @@ -42,6 +42,7 @@ #define VIRTIO_NET_F_STATUS 16 /* virtio_net_config.status available */ #define VIRTIO_NET_F_CTRL_VQ 17 /* Control channel available */ #define VIRTIO_NET_F_CTRL_RX 18 /* Control channel RX mode support */ +#define VIRTIO_NET_F_CTRL_VLAN 19 /* Control channel VLAN filtering */ #define VIRTIO_NET_S_LINK_UP 1 /* Link is up */ @@ -135,4 +136,17 @@ struct virtio_net_ctrl_mac { #define VIRTIO_NET_CTRL_MAC 1 #define VIRTIO_NET_CTRL_MAC_TABLE_SET 0 +/* + * Control VLAN filtering + * + * The VLAN filter table is controlled via a simple ADD/DEL interface. + * VLAN IDs not added may be filterd by the hypervisor. Del is the + * opposite of add. Both commands expect an out entry containing a 2 + * byte VLAN ID. VLAN filterting is available with the + * VIRTIO_NET_F_CTRL_VLAN feature bit. + */ +#define VIRTIO_NET_CTRL_VLAN 2 + #define VIRTIO_NET_CTRL_VLAN_ADD 0 + #define VIRTIO_NET_CTRL_VLAN_DEL 1 + #endif