[6539] qemu:virtio-net: Add additional MACs via a filter table ( Alex Williamson)
Revision: 6539
http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6539 Author: aliguori Date: 2009-02-05 22:36:28 +0000 (Thu, 05 Feb 2009)
Log Message:
-----------
qemu:virtio-net: Add additional MACs via a filter table (Alex Williamson)
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.
Modified Paths:
--------------
trunk/hw/virtio-net.c
trunk/hw/virtio-net.h
Modified: trunk/hw/virtio-net.c
===================================================================
hw/virtio-net.c +87 -2
@@ -16,8 +16,10 @@#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 { VirtIODevice vdev;
@@ -32,6 +34,10 @@int mergeable_rx_bufs;int promisc;int allmulti; +struct { +int in_use; +uint8_t *macs; + } mac_table; } VirtIONet;/* TODO
@@ -87,13 +93,18 @@/* 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,53 @@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) || + elem->out_sg[2 ].iov_len <sizeof (mac_data)) +return VIRTIO_NET_ERR ; + + n->mac_table.in_use =0 ; +memset (n->mac_table.macs,0 ,MAC_TABLE_ENTRIES *ETH_ALEN ); + + mac_data.entries =ldl_le_p (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, elem->out_sg[1 ].iov_base +sizeof (mac_data), + mac_data.entries *ETH_ALEN ); + n->mac_table.in_use += mac_data.entries; + }else { + n->promisc =1 ; +return VIRTIO_NET_OK ; + } + + mac_data.entries =ldl_le_p (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 ), + elem->out_sg[2 ].iov_base +sizeof (mac_data), + 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);
@@ -151,6 +209,8 @@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);stb_p (elem.in_sg[elem.in_num -1 ].iov_base, status);
@@ -226,6 +286,7 @@ {static const uint8_t bcast[] = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff };uint8_t *ptr = (uint8_t *)buf; +int i;if (n->promisc)return 1 ;
@@ -244,6 +305,11 @@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 ; }
@@ -406,6 +472,8 @@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)
@@ -429,6 +497,19 @@ 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 );
@@ -474,6 +555,10 @@ 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); }
Modified: trunk/hw/virtio-net.h
===================================================================
hw/virtio-net.h +22 -0
@@ -113,4 +113,26 @@#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