From patchwork Tue Feb 3 19:30:09 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 5294 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 n13JX7BI032002 for ; Tue, 3 Feb 2009 19:33:07 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752407AbZBCTdE (ORCPT ); Tue, 3 Feb 2009 14:33:04 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752430AbZBCTdE (ORCPT ); Tue, 3 Feb 2009 14:33:04 -0500 Received: from g5t0007.atlanta.hp.com ([15.192.0.44]:33503 "EHLO g5t0007.atlanta.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752407AbZBCTdC (ORCPT ); Tue, 3 Feb 2009 14:33:02 -0500 Received: from g4t0009.houston.hp.com (g4t0009.houston.hp.com [16.234.32.26]) by g5t0007.atlanta.hp.com (Postfix) with ESMTP id 0CA011442B; Tue, 3 Feb 2009 19:32:59 +0000 (UTC) Received: from ldl.fc.hp.com (ldl.fc.hp.com [15.11.146.30]) by g4t0009.houston.hp.com (Postfix) with ESMTP id C18EBC09C; Tue, 3 Feb 2009 19:32:58 +0000 (UTC) Received: from localhost (ldl.fc.hp.com [127.0.0.1]) by ldl.fc.hp.com (Postfix) with ESMTP id 5DE4E39C043; Tue, 3 Feb 2009 12:32:58 -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 JWWiXyS8EFhN; Tue, 3 Feb 2009 12:32:58 -0700 (MST) Received: from kvm.aw (lart.fc.hp.com [15.11.146.31]) by ldl.fc.hp.com (Postfix) with ESMTP id 0123839C00D; Tue, 3 Feb 2009 12:32:58 -0700 (MST) From: Alex Williamson Subject: [PATCH v2 7/8] qemu:virtio-net: Add additional MACs via a filter table To: anthony@codemonkey.ws, qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, markmc@redhat.com Date: Tue, 03 Feb 2009 12:30:09 -0700 Message-ID: <20090203193009.19598.20537.stgit@kvm.aw> In-Reply-To: <20090203192932.19598.50925.stgit@kvm.aw> References: <20090203192932.19598.50925.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 Create a filter table and allow the guest to populate it with the MAC class control commands. We manage the size and usage of the filter table including enabling promiscuous and all-multi modes as necessary. The guest should therefore assume the table is infinite. Eventually this might allow us to bind directly to a hardware NIC and manipulate a physical MAC filter. The specifics of the TABLE_SET command are documented in virtio-net.h. Separate buffers in the same command are used for unicaste and multicast addresses for priority and sychronization. With this we can export the VIRTIO_NET_F_CTRL_RX feature bit. Signed-off-by: Alex Williamson --- hw/virtio-net.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- hw/virtio-net.h | 22 ++++++++++++++ 2 files changed, 108 insertions(+), 2 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 d01da22..40050ff 100644 --- a/hw/virtio-net.c +++ b/hw/virtio-net.c @@ -16,7 +16,9 @@ #include "qemu-timer.h" #include "virtio-net.h" -#define VIRTIO_NET_VM_VERSION 4 +#define VIRTIO_NET_VM_VERSION 5 + +#define MAC_TABLE_ENTRIES 32 typedef struct VirtIONet { @@ -32,6 +34,10 @@ typedef struct VirtIONet int mergeable_rx_bufs; int promisc; int allmulti; + struct { + int in_use; + uint8_t *macs; + } mac_table; } VirtIONet; /* TODO @@ -87,13 +93,18 @@ static void virtio_net_reset(VirtIODevice *vdev) /* Reset back to compatibility mode */ n->promisc = 1; n->allmulti = 0; + + /* Flush any MAC filter table state */ + n->mac_table.in_use = 0; + memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); } 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_VQ) | + (1 << VIRTIO_NET_F_CTRL_RX); return features; } @@ -127,6 +138,52 @@ static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, return VIRTIO_NET_OK; } +static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, + VirtQueueElement *elem) +{ + struct virtio_net_ctrl_mac *mac_data; + + if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 || + elem->out_sg[1].iov_len < sizeof(mac_data->entries) || + elem->out_sg[2].iov_len < sizeof(mac_data->entries)) + return VIRTIO_NET_ERR; + + n->mac_table.in_use = 0; + memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); + + mac_data = (void *)elem->out_sg[1].iov_base; + + if (sizeof(mac_data->entries) + + (mac_data->entries * ETH_ALEN) > elem->out_sg[1].iov_len) + return VIRTIO_NET_ERR; + + if (mac_data->entries <= MAC_TABLE_ENTRIES) { + memcpy(n->mac_table.macs, &mac_data->macs, + mac_data->entries * ETH_ALEN); + n->mac_table.in_use += mac_data->entries; + } else { + n->promisc = 1; + return VIRTIO_NET_OK; + } + + mac_data = (void *)elem->out_sg[2].iov_base; + + if (sizeof(mac_data->entries) + + (mac_data->entries * ETH_ALEN) > elem->out_sg[2].iov_len) + return VIRTIO_NET_ERR; + + if (mac_data->entries) { + if (n->mac_table.in_use + mac_data->entries <= MAC_TABLE_ENTRIES) { + memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN), + &mac_data->macs, mac_data->entries * ETH_ALEN); + n->mac_table.in_use += mac_data->entries; + } else + n->allmulti = 1; + } + + return VIRTIO_NET_OK; +} + static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) { VirtIONet *n = to_virtio_net(vdev); @@ -152,6 +209,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) if (ctrl->class == VIRTIO_NET_CTRL_RX_MODE) *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); virtqueue_push(vq, &elem, sizeof(*status)); virtio_notify(vdev, vq); @@ -225,6 +284,7 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) { static uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; uint8_t *ptr = (uint8_t *)buf; + int i; if (n->promisc) return 1; @@ -243,6 +303,11 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) if (!memcmp(ptr, n->mac, ETH_ALEN)) return 1; + for (i = 0; i < n->mac_table.in_use; i++) { + if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) + return 1; + } + return 0; } @@ -405,6 +470,8 @@ static void virtio_net_save(QEMUFile *f, void *opaque) qemu_put_be16(f, n->status); qemu_put_be32(f, n->promisc); 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); } static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) @@ -428,6 +495,19 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) n->allmulti = qemu_get_be32(f); } + if (version_id >= 5) { + n->mac_table.in_use = qemu_get_be32(f); + /* MAC_TABLE_ENTRIES may be different from the saved image */ + if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { + qemu_get_buffer(f, n->mac_table.macs, + n->mac_table.in_use * ETH_ALEN); + } else if (n->mac_table.in_use) { + qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR); + n->promisc = 1; + n->mac_table.in_use = 0; + } + } + if (n->tx_timer_active) { qemu_mod_timer(n->tx_timer, qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); @@ -473,6 +553,10 @@ void virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn) n->mergeable_rx_bufs = 0; n->promisc = 1; /* for compatibility */ + n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN); + if (!n->mac_table.macs) + 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 ce8e4b4..291fa9d 100644 --- a/hw/virtio-net.h +++ b/hw/virtio-net.h @@ -113,4 +113,26 @@ typedef uint8_t virtio_net_ctrl_ack; #define VIRTIO_NET_CTRL_RX_MODE_PROMISC 0 #define VIRTIO_NET_CTRL_RX_MODE_ALLMULTI 1 +/* + * Control the MAC filter table. + * + * The MAC filter table is managed by the hypervisor, the guest should + * assume the size is infinite. Filtering should be considered + * non-perfect, ie. based on hypervisor resources, the guest may + * received packets from sources not specified in the filter list. + * + * In addition to the class/cmd header, the TABLE_SET command requires + * two out scatterlists. Each contains a 4 byte count of entries followed + * by a concatenated byte stream of the ETH_ALEN MAC addresses. The + * first sg list contains unicast addresses, the second is for multicast. + * This functionality is present if the VIRTIO_NET_F_CTRL_RX feature + * is available. + */ +struct virtio_net_ctrl_mac { + uint32_t entries; + uint8_t macs[][ETH_ALEN]; +}; +#define VIRTIO_NET_CTRL_MAC 1 + #define VIRTIO_NET_CTRL_MAC_TABLE_SET 0 + #endif